-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclipBoard.cpp
161 lines (143 loc) · 4.66 KB
/
clipBoard.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
#include "clipBoard.h"
namespace acm {
////////////////////////////////////////////////////////////////////////////////////////////////////////
// clipBoard 类 2015-2-26
// 剪切板操作类
////////////////////////////////////////////////////////////////////////////////////////////////////////
UINT clipBoard::enumFormat(FormatETCArrayType &vecFormatETC)
{
IDataObject *pDataObj = NULL;
IEnumFORMATETC *pEnumFormat = NULL;
HRESULT hr = S_OK;
FORMATETC formatETC = {0};
ULONG fetched = 0;
__try
{
vecFormatETC.clear();
if(FAILED(OleGetClipboard(&pDataObj))) __leave;
if(FAILED(pDataObj->EnumFormatEtc(DATADIR_GET, &pEnumFormat))) __leave;
while(SUCCEEDED(pEnumFormat->Next(1, &formatETC, &fetched)) && fetched == 1)
{
BSFormatETC fmETC = {0};
fmETC.fetc = formatETC;
wchar_t pName[MAX_PATH] = {0};
int len = GetClipboardFormatName(formatETC.cfFormat, fmETC.pFormatName, MAX_PATH);
fmETC.pFormatName[len] = L'\0';
vecFormatETC.push_back(fmETC);
memset(&formatETC, 0, sizeof(formatETC));
}
}
__except(gtc::app::sehFilterDefault(GetExceptionInformation(), L"clipBoard::enumFormat"))
{
WRITE_LOG(L"clipBoard::enumFormat SEH异常 ");
}
//
if(pEnumFormat) pEnumFormat->Release();
if(pDataObj) pDataObj->Release();
return vecFormatETC.size();
}
void clipBoard::free(ItemIDArrayType &vecItemID)
{
for (ItemIDArrayType::iterator iter = vecItemID.begin(); iter != vecItemID.end(); ++iter)
{
if(iter->pIIDL) ILFree(iter->pIIDL);
}
vecItemID.clear();
}
UINT clipBoard::enumFile(StringArrayType *pvecFiles /*= NULL*/, HWND hWnd /*= NULL*/)
{
//IDataObject *pDataObj = NULL;
//IEnumFORMATETC *pEnumFormat = NULL;
//HRESULT hr = S_OK;
//ULONG fetched = 0;
UINT fileCount = 0;
HDROP hDrop = NULL;
wchar_t pFile[MAX_PATH] = {0};
//__try
//{
if(pvecFiles) pvecFiles->clear();
if(!OpenClipboard(hWnd)) return 0;
//UINT fm = RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT);
hDrop = (HDROP)GetClipboardData(CF_HDROP);
if(!hDrop)
{
CloseClipboard();
return 0;
}
fileCount = DragQueryFile(hDrop, -1, NULL, 0);
// 得到文件列表
if (pvecFiles)
{
for (UINT idx = 0; idx < fileCount; ++idx)
{
if (DragQueryFile(hDrop, idx, pFile, _countof(pFile)))
{
pvecFiles->push_back(pFile);
}
}
}
//UINT fm = RegisterClipboardFormat(CFSTR_SHELLIDLIST);
//if(FAILED(OleGetClipboard(&pDataObj))) __leave;
//if(FAILED(pDataObj->EnumFormatEtc(DATADIR_GET, &pEnumFormat))) __leave;
//FORMATETC formatETC = {fm, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
//if(SUCCEEDED(pDataObj->QueryGetData(&formatETC)))
//{
// STGMEDIUM stgm = {0};
// if (SUCCEEDED(pDataObj->GetData(&formatETC, &stgm)))
// {
// if (stgm.tymed == TYMED_HGLOBAL && stgm.hGlobal != NULL)
// {
// CIDA *pcida = (CIDA *)::GlobalLock(stgm.hGlobal);
// fileCount = pcida->cidl;
// if(fileCount > 0 && pvecItemID)
// {
// LPITEMIDLIST pidl = (LPITEMIDLIST)((LPBYTE)pcida + pcida->aoffset[0]);
// IShellFolder *pshFolderRoot = NULL;
// IShellFolder2 *pshFolder2 = NULL;
// hr = SHBindToObject(NULL, pidl, NULL, IID_IShellFolder, (void**)(&pshFolderRoot));
// for (int idx = 0; idx < pcida->cidl; ++idx)
// {
// STRRET strr = {0};
// PCUITEMID_CHILD puidlLast = NULL;
// LPCITEMIDLIST pidlChild = (LPCITEMIDLIST)((LPBYTE)pcida + pcida->aoffset[idx+1]);
// hr = pshFolderRoot->GetDisplayNameOf(pidlChild, SHGDN_NORMAL | SHGDN_FORPARSING, &strr);
// //hr = SHBindToParent(pidlChild, IID_PPV_ARGS(&pshFolder2), &puidlLast); // 这个地方也能直接解析到
// hr = SHBindToFolderIDListParent(pshFolderRoot, pidlChild, IID_IShellFolder2, (void**)&pshFolder2, &puidlLast); // 这个也可以,XP不支持
// if (SUCCEEDED(hr))
// {
// _BSItemIDList itemID = {0};
// itemID.pIIDL = ILClone(puidlLast);
//
// // 全路径 或 ::{CLSID}
// hr = pshFolder2->GetDisplayNameOf(puidlLast, SHGDN_NORMAL | SHGDN_FORPARSING, &strr);
// if (SUCCEEDED(hr))
// {
// hr = StrRetToBuf(&strr, puidlLast, itemID.pFullPath, _countof(itemID.pFullPath));
// //if(SUCCEEDED(hr))
// //{
// // std::wcsncpy(itemID.pFullPath, pName, _countof(itemID.pFullPath));
// //}
// if(strr.uType == STRRET_WSTR) CoTaskMemFree(strr.pOleStr);
// }
// pvecItemID->push_back(itemID);
// }
// }
// }
// ::GlobalUnlock(stgm.hGlobal);
// }
// ::ReleaseStgMedium(&stgm);
// }
//}
//}
//__except(gtc::app::sehFilterDefault(GetExceptionInformation(), L"clipBoard::enumFormat"))
//{
// fileCount = 0;
// WRITE_LOG(L"clipBoard::enumFormat SEH异常 ");
//}
////
//if(pEnumFormat) pEnumFormat->Release();
//if(pDataObj) pDataObj->Release();
CloseClipboard();
return fileCount;
}
} // end acm