使窗体能够接受文件拖拽
DragAcceptFiles(hwnd, TRUE);
文件拖拽在win32窗体过程函数中对WM_DROPFILES消息进行处理
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{switch (uMsg){case WM_DESTROY:PostQuitMessage(0);return 0;case WM_DROPFILES:{HDROP hDrop = (HDROP)wParam;// 获取拖拽的文件数量UINT count = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);// 逐个处理拖拽的文件for (UINT i = 0; i < count; i++){// 获取文件名长度UINT len = DragQueryFile(hDrop, i, NULL, 0) + 1;// 获取文件名LPSTR filename = new char[len];DragQueryFile(hDrop, i, filename, len);// 处理文件,这里可以根据需要进行操作std::cout<<filename;//简单的输出文件名delete[] filename;}// 释放拖拽句柄DragFinish(hDrop);break;}case WM_PAINT:{PAINTSTRUCT ps;HDC hdc = BeginPaint(hwnd, &ps);// All painting occurs here, between BeginPaint and EndPaint.FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));EndPaint(hwnd, &ps);}return 0;}return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
窗体内鼠标拖拽操作
POINT g_ptMouseDown
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)case WM_LBUTTONDOWN:
{// 鼠标左键按下,开始拖拽SetCapture(hwnd);// 获取鼠标当前位置POINT pt;GetCursorPos(&pt);// 将鼠标位置转换为窗口内的坐标ScreenToClient(hwnd, &pt);// 存储鼠标按下时的起始位置// 这个起始位置将用于计算鼠标的偏移量g_ptMouseDown = pt;break;
}
case WM_MOUSEMOVE:
{if (GetCapture() == hwnd){// 鼠标正在拖拽中// 获取鼠标当前位置POINT pt;GetCursorPos(&pt);// 将鼠标位置转换为窗口内的坐标ScreenToClient(hwnd, &pt);// 计算鼠标的偏移量int deltaX = pt.x - g_ptMouseDown.x;int deltaY = pt.y - g_ptMouseDown.y;// TODO: 根据鼠标偏移量进行相应的处理std::cout<<"offset "<<deltaX<<" "<<deltaY<<"\n";}break;
}
case WM_LBUTTONUP:
{// 鼠标左键释放,结束拖拽ReleaseCapture();break;
}
显示滚轮滚动操作
case WM_MOUSEWHEEL:{// 获取滚轮旋转的距离//使用GET_WHEEL_DELTA_WPARAM宏来获取滚轮旋转的距离。wParam参数包含滚轮的操作信息。WHEEL_DELTA是一个常量,表示滚轮每次旋转的距离short distance = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;// TODO: 根据滚轮的距离进行相应的处理std::cout<<" distance:"<<distance<<"\n";break;}