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