16,473
社区成员
发帖
与我相关
我的任务
分享
// from D:\MyProjects\例子\所有 vc Sample vs Sample\WinSDKSamples 7.1\Samples v7.1\winui\shell\appplatform\DragDropVisuals
// x, y in client coordinates
// http://blog.csdn.net/xzhdy/article/details/8970017
BOOL CheckForDragBegin(HWND hwnd, int x, int y)
{
int const cxDrag = GetSystemMetrics(SM_CXDRAG);
int const cyDrag = GetSystemMetrics(SM_CYDRAG);
// See if the user moves a certain number of pixels in any direction
RECT rcDragRadius;
SetRect(&rcDragRadius, x - cxDrag, y - cyDrag, x + cxDrag, y + cyDrag);
MapWindowRect(hwnd, NULL, &rcDragRadius); // client -> screen
SetCapture(hwnd);
do
{
// Sleep the thread waiting for mouse input. Prevents pegging the CPU in a
// PeekMessage loop.
MSG msg;
switch (MsgWaitForMultipleObjectsEx(0, NULL, INFINITE, QS_MOUSE, MWMO_INPUTAVAILABLE))
{
case WAIT_OBJECT_0:
{
if (PeekMessage(&msg, NULL, WM_MOUSEFIRST, WM_MOUSELAST, PM_REMOVE))
{
// See if the application wants to process the message...
if (CallMsgFilter(&msg, MSGF_COMMCTRL_BEGINDRAG) == 0)
{
switch (msg.message)
{
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
// Released the mouse without moving outside the
// drag radius, not beginning a drag.
ReleaseCapture();
return FALSE;
case WM_MOUSEMOVE:
if (!PtInRect(&rcDragRadius, msg.pt))
{
// Moved outside the drag radius, beginning a drag.
ReleaseCapture();
return TRUE;
}
break;
default:
TranslateMessage(&msg);
DispatchMessage(&msg);
break;
}
}
}
break;
}
default:
break;
}
// WM_CANCELMODE messages will unset the capture, in that
// case I want to exit this loop
}
while (GetCapture() == hwnd);
return FALSE;
}
void BeginDrag(HWND hwndDragBegin)
{
POINT pt;
GetCursorPos(&pt);
MapWindowPoints(HWND_DESKTOP, hwndDragBegin, &pt, 1); // screen -> client
if (CheckForDragBegin(hwndDragBegin, pt.x, pt.y))
{
vector<string> paths;
paths.push_back(_T("D:\\MyProjects\\VC笔记.doc"));
paths.push_back(_T("D:\\MyProjects\\驱动编程.7z"));
OleInitialize(NULL);
DragDropTo(hWnd,paths,DROPEFFECT_MOVE | DROPEFFECT_COPY);
OleUninitialize();
}
}
case WM_LBUTTONDOWN:
BeginDrag(hWnd);
/****************************************************************************
* 包含文件
#include <shlobj.h>
#include <atlbase.h>
* 记得
OleInitialize(NULL); // needed for Drag and Drop
OleUninitialize();
* 示例:
vector<string> paths;
paths.push_back(_T("D:\\MyProjects\\VC笔记.doc"));
paths.push_back(_T("D:\\MyProjects\\驱动编程.7z"));
OleInitialize(NULL);
DragDropTo(hWnd,paths,DROPEFFECT_MOVE | DROPEFFECT_COPY);
OleUninitialize();
****************************************************************************/
//兼容ANSI和UNICODE
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
bool DragDropTo(HWND hWnd,const vector<tstring>& paths,int DropStyle)
{
// OleInitialize(NULL); // needed for Drag and Drop
PCIDLIST_ABSOLUTE* pIdlArray = new PCIDLIST_ABSOLUTE[paths.size()];
for (UINT i = 0; i<paths.size(); ++i)
{
pIdlArray[i] = ILCreateFromPath(paths[i].c_str());
ASSERT(pIdlArray[i]);
}
HRESULT hr;
CComPtr<IShellItemArray> pIshellItemArray;
hr = SHCreateShellItemArrayFromIDLists(paths.size(), pIdlArray, &pIshellItemArray);
if (SUCCEEDED(hr))
{
IDataObject *pdtobj;
hr = pIshellItemArray->BindToHandler(NULL, BHID_DataObject, IID_PPV_ARGS(&pdtobj));
if (SUCCEEDED(hr))
{
DWORD dwEffectResult;
hr = SHDoDragDrop(hWnd, pdtobj, NULL, DropStyle , &dwEffectResult);
ASSERT(SUCCEEDED(hr));
pdtobj->Release();
}
}
for (UINT i = 0; i<paths.size(); ++i)
{
CoTaskMemFree((LPVOID)pIdlArray[i]);
}
delete pIdlArray;
// OleUninitialize();
return SUCCEEDED(hr);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
{
vector<string> paths;
paths.push_back(_T("D:\\MyProjects\\VC笔记.doc"));
paths.push_back(_T("D:\\MyProjects\\驱动编程.7z"));
OleInitialize(NULL);
DragDropTo(hWnd,paths,DROPEFFECT_MOVE | DROPEFFECT_COPY);
OleUninitialize();
}
break;