ClipboardSaveRestore.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include ".\clipboardsaverestore.h"
  4. CClipboardSaveRestore::CClipboardSaveRestore(void)
  5. {
  6. }
  7. CClipboardSaveRestore::~CClipboardSaveRestore(void)
  8. {
  9. }
  10. bool CClipboardSaveRestore::Save(BOOL textOnly)
  11. {
  12. m_Clipboard.RemoveAll();
  13. bool bRet = false;
  14. COleDataObjectEx oleData;
  15. CClipFormat cf;
  16. if(::OpenClipboard(theApp.m_MainhWnd))
  17. {
  18. int nFormat = EnumClipboardFormats(0);
  19. while(nFormat != 0)
  20. {
  21. if(textOnly == false || (nFormat == CF_TEXT || nFormat == CF_UNICODETEXT || nFormat == CF_HDROP))
  22. {
  23. HGLOBAL hGlobal = ::GetClipboardData(nFormat);
  24. LPVOID pvData = GlobalLock(hGlobal);
  25. if(pvData)
  26. {
  27. INT_PTR size = GlobalSize(hGlobal);
  28. if(size > 0)
  29. {
  30. //Copy the data locally
  31. cf.m_hgData = NewGlobalP(pvData, size);
  32. cf.m_cfType = nFormat;
  33. m_Clipboard.Add(cf);
  34. //m_Clipboard owns the data now
  35. cf.m_hgData = NULL;
  36. }
  37. GlobalUnlock(hGlobal);
  38. }
  39. }
  40. nFormat = EnumClipboardFormats(nFormat);
  41. }
  42. ::CloseClipboard();
  43. bRet = true;
  44. }
  45. return bRet;
  46. }
  47. bool CClipboardSaveRestore::Restore()
  48. {
  49. bool bRet = false;
  50. if(::OpenClipboard(theApp.m_MainhWnd))
  51. {
  52. ::EmptyClipboard();
  53. SetClipboardData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  54. INT_PTR size = m_Clipboard.GetSize();
  55. for(int nPos = 0; nPos < size; nPos++)
  56. {
  57. CClipFormat *pCF = &m_Clipboard.ElementAt(nPos);
  58. if(pCF && pCF->m_hgData)
  59. {
  60. ::SetClipboardData(pCF->m_cfType, pCF->m_hgData);
  61. pCF->m_hgData = NULL;//clipboard now owns the data
  62. }
  63. }
  64. bRet = TRUE;
  65. ::CloseClipboard();
  66. }
  67. m_Clipboard.RemoveAll();
  68. if(bRet == FALSE)
  69. {
  70. Log(_T("CClipboardSaveRestore::Restore failed to restore clipboard"));
  71. }
  72. return bRet;
  73. }
  74. bool CClipboardSaveRestore::RestoreTextOnly()
  75. {
  76. bool bRet = false;
  77. if(::OpenClipboard(theApp.m_MainhWnd))
  78. {
  79. ::EmptyClipboard();
  80. SetClipboardData(theApp.m_cfIgnoreClipboard, NewGlobalP("Ignore", sizeof("Ignore")));
  81. bool foundText = false;
  82. int hDropIndex = -1;
  83. INT_PTR size = m_Clipboard.GetSize();
  84. for(int pos = 0; pos < size; pos++)
  85. {
  86. CClipFormat *pCF = &m_Clipboard.ElementAt(pos);
  87. if(pCF && pCF->m_hgData)
  88. {
  89. if(pCF->m_cfType == CF_TEXT || pCF->m_cfType == CF_UNICODETEXT)
  90. {
  91. //Make a copy of the data we are putting on the clipboard so we can still
  92. //restore all clips later in Restore()
  93. LPVOID localData = ::GlobalLock(pCF->m_hgData);
  94. HGLOBAL newData = NewGlobalP(localData, ::GlobalSize(pCF->m_hgData));
  95. ::SetClipboardData(pCF->m_cfType, newData);
  96. ::GlobalUnlock(pCF->m_hgData);
  97. foundText = true;
  98. }
  99. else if(pCF->m_cfType == CF_HDROP)
  100. {
  101. hDropIndex = pos;
  102. }
  103. }
  104. }
  105. //if we didn't place text on the clipboard and we have a hdrop then convert the hdrop to text only with contents of hdrop
  106. if(foundText == false &&
  107. hDropIndex > -1)
  108. {
  109. CString hDropString;
  110. CClipFormat *pCF = &m_Clipboard.ElementAt(hDropIndex);
  111. if(pCF && pCF->m_hgData)
  112. {
  113. HDROP drop = (HDROP)GlobalLock(pCF->m_hgData);
  114. int nNumFiles = DragQueryFile(drop, -1, NULL, 0);
  115. TCHAR file[MAX_PATH];
  116. for(int nFile = 0; nFile < nNumFiles; nFile++)
  117. {
  118. if(DragQueryFile(drop, nFile, file, sizeof(file)) > 0)
  119. {
  120. if(PathIsDirectory(file) == FALSE)
  121. {
  122. hDropString += file;
  123. hDropString += _T("\r\n");
  124. }
  125. }
  126. }
  127. GlobalUnlock(pCF->m_hgData);
  128. HGLOBAL newData = NewGlobalP(hDropString.GetBuffer(), ((hDropString.GetLength() + 1) * sizeof(TCHAR)));
  129. ::SetClipboardData(CF_UNICODETEXT, newData);
  130. }
  131. }
  132. bRet = TRUE;
  133. ::CloseClipboard();
  134. }
  135. if(bRet == FALSE)
  136. {
  137. Log(_T("CClipboardSaveRestore::Restore failed to restore clipboard"));
  138. }
  139. return bRet;
  140. }