gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
实现客户区域拖动使用wm_mousemove和setwindowpos
- User
实现客户区域拖动使用wm_mousemove和setwindowpos
- Assistant
WM_MOUSEMOVE消息用于在鼠标光标移动时接收通知。而SetWindowPos函数用于改变窗口的位置、大小、Z序等属性。我们可以通过结合这两种方法实现客户区域的拖动。
实现步骤如下:
1.在窗口的消息回调函数中处理WM_MOUSEMOVE消息,记录鼠标光标的位置。
2.当鼠标左键按下时,调用SetCapture()方法将鼠标光标捕获到当前窗口中,并记录拖动开始时客户区域的位置。
3.在鼠标移动过程中不断计算鼠标移动的距离,通过SetWindowPos()函数将窗口的位置按照鼠标移动的距离进行调整。
4.当鼠标左键键抬起时,释放鼠标光标的捕获。
下面是一个示例代码:
- 定义变量记录拖动开始时客户区域的位置
POINT mDragPos;- 实现WM_MOUSEMOVE消息处理函数
case WM_MOUSEMOVE: { if (wParam & MK_LBUTTON) { POINT currentPos; GetCursorPos(¤tPos); int offsetX = currentPos.x - mDragPos.x; int offsetY = currentPos.y - mDragPos.y; SetWindowPos(hWnd, NULL, rect.left + offsetX, rect.top + offsetY, 0, 0, SWP_NOZORDER | SWP_NOSIZE); } break; }- 实现鼠标左键按下处理函数
case WM_LBUTTONDOWN: { SetCapture(hWnd); GetCursorPos(&mDragPos); break; }- 实现鼠标左键抬起处理函数
case WM_LBUTTONUP: { ReleaseCapture(); break; } - User
完整代码
- Assistant
以下是一个简单的完整示例,演示如何使用WM_MOUSEMOVE和SetWindowPos()函数实现客户区域拖动。
#include <windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpszClassName = L"MainWindowClass"; RegisterClass(&wc); HWND hWnd = CreateWindow(L"MainWindowClass", L"Draggable Client Area Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static POINT mDragPos; static RECT rect; switch (message) { case WM_CREATE: GetClientRect(hWnd, &rect); break; case WM_LBUTTONDOWN: SetCapture(hWnd); GetCursorPos(&mDragPos); break; case WM_LBUTTONUP: ReleaseCapture(); break; case WM_MOUSEMOVE: { if (wParam & MK_LBUTTON) { POINT currentPos; GetCursorPos(¤tPos); int offsetX = currentPos.x - mDragPos.x; int offsetY = currentPos.y - mDragPos.y; SetWindowPos(hWnd, NULL, rect.left + offsetX, rect.top + offsetY, 0, 0, SWP_NOZORDER | SWP_NOSIZE); } break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } - User
由bug,窗口会回退闪烁一下
- Assistant
出现窗口回退闪烁的问题,可能是因为鼠标移动的速度过快,导致WM_MOUSEMOVE消息的处理出现了延迟。这时可以使用SetTimer()函数设置一个定时器,定时触发窗口位置的更新,避免出现延迟。
下面是修改后的代码示例:
#include <windows.h> LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpszClassName = L"MainWindowClass"; RegisterClass(&wc); HWND hWnd = CreateWindow(L"MainWindowClass", L"Draggable Client Area Demo", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static POINT mDragPos; static RECT rect; switch (message) { case WM_CREATE: GetClientRect(hWnd, &rect); SetTimer(hWnd, 1, 10, NULL); // 设置定时器,每10ms触发一次 break; case WM_LBUTTONDOWN: SetCapture(hWnd); GetCursorPos(&mDragPos); break; case WM_LBUTTONUP: ReleaseCapture(); break; case WM_MOUSEMOVE: break; case WM_TIMER: { POINT currentPos; GetCursorPos(¤tPos); int offsetX = currentPos.x - mDragPos.x; int offsetY = currentPos.y - mDragPos.y; SetWindowPos(hWnd, NULL, rect.left + offsetX, rect.top + offsetY, 0, 0, SWP_NOZORDER | SWP_NOSIZE); break; } case WM_DESTROY: KillTimer(hWnd, 1); // 销毁定时器 PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.