15,979
社区成员
发帖
与我相关
我的任务
分享
/****************************************************************************
* 包含文件
#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
#include <shlobj.h>
#include <atlbase.h>
static 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)
{
ASSERT(-1 != GetFileAttributes(paths[i].c_str())); // check if the file exist
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);
}
/*****************************************************************
win10 + vs2017 编译通过
代码仅供参考!
******************************************************************/
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <windows.h>
#include <tchar.h>
#include <tchar.h>
#include <string>
#include <vector>
using namespace std;
#ifndef ASSERT
#include <assert.h>
#define ASSERT(X) assert(X) //不能有分号
#endif
#include "E:\MyProjects\Library\DragDropTo.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_LBUTTONDOWN:
{
vector<string> paths;
paths.push_back(_T("D:\\s.txt"));
OleInitialize(NULL); //一般应该放在程序初始化时,调用
DragDropTo(hWnd, paths, DROPEFFECT_MOVE | DROPEFFECT_COPY);
OleUninitialize();
}
break;
case WM_CLOSE:
::DestroyWindow(hWnd);
PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.lpszMenuName = NULL;
wndcls.hInstance = NULL;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
wndcls.lpszClassName = _T("Demo_MainWnd");
wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hIcon = LoadIcon(::GetModuleHandle(NULL), NULL);
wndcls.lpfnWndProc = WndProc;
if (!RegisterClass(&wndcls))
return 0;
HWND hWnd = CreateWindowEx(NULL, _T("Demo_MainWnd"), _T("Demo"), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
400, 200, 500, 300, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}