目前,通过鼠标钩子判断是否在拖拽,如果在拖拽,就通过 OleGetClipboard 函数从剪贴板获取文件信息,但是剪贴板里面没有文件相关的信息。代码如下:
#include
#include
#include
#include
#include [i]
#include
#include
HHOOK g_mouseHook;
bool g_isDragging = false;
POINT g_dragStartPos = { 0, 0 };
const int DRAG_THRESHOLD = 3; // 拖动阈值(像素)
void ExtractFileInfoFromDropClipboard();
void CheckOtherDataFormats(IDataObject* pDataObject);
void ExtractFileTypeInfo(const std::wstring& filePath);
LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
MSLLHOOKSTRUCT* pMouse = (MSLLHOOKSTRUCT*)lParam;
if (wParam == WM_LBUTTONDOWN) {
//std::cout pt.x pt.y pt;
g_isDragging = false;
}
if (wParam == WM_MOUSEMOVE) {
// 检测鼠标是否有拖拽操作
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) {
// 检查是否超过拖动阈值
int deltaX = abs(pMouse->pt.x - g_dragStartPos.x);
int deltaY = abs(pMouse->pt.y - g_dragStartPos.y);
if (!g_isDragging && (deltaX > DRAG_THRESHOLD || deltaY > DRAG_THRESHOLD)) {
g_isDragging = true;
std::cout QueryGetData(&fmtetc))) {
STGMEDIUM stgmed;
// 查询 HDROP 数据
hr = pDataObject->GetData(&fmtetc, &stgmed);
if (SUCCEEDED(hr)) {
HDROP hDrop = static_cast(GlobalLock(stgmed.hGlobal));
if (hDrop) {
// 获取文件数量
UINT fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, nullptr, 0);
std::cout 0) {
std::vector buffer(pathLength + 1);
DragQueryFile(hDrop, i, buffer.data(), pathLength + 1);
std::wstring filePath(buffer.data());
std::wcout Release();
}
else {
std::wcerr EnumFormatEtc(DATADIR_GET, &pEnumFormat);
if (SUCCEEDED(hr) && pEnumFormat) {
FORMATETC fmtetc;
ULONG fetched = 0;
std::cout Next(1, &fmtetc, &fetched) == S_OK && fetched == 1) {
TCHAR formatName[256];
if (GetClipboardFormatName(fmtetc.cfFormat, formatName, 256) > 0) {
std::wcout Release();
}
}
// 提取文件类型信息
void ExtractFileTypeInfo(const std::wstring& filePath) {
// 获取文件属性
DWORD attr = GetFileAttributes(filePath.c_str());
if (attr != INVALID_FILE_ATTRIBUTES) {
if (attr & FILE_ATTRIBUTE_DIRECTORY) {
std::wcout

