[英]Forcing the recall of WM_PAINT
I've created a window using CreateWindowEx
and that is working fine.我已经使用
CreateWindowEx
创建了一个 window 并且工作正常。 The window opens. window 打开。
In the function where I process messages, specifically WM_PAINT
, I grab the cursor position using GetCursorPos
and then draw a rectangle there.在我处理消息的 function 中,特别是
WM_PAINT
,我使用GetCursorPos
抓取 cursor position 然后在那里绘制一个矩形。 However, I would like the window to continuously update and redraw this rectangle, acting like a cursor.但是,我希望 window 不断更新和重绘这个矩形,就像 cursor 一样。
I've created a thread like the following to try and force this:我创建了一个类似以下的线程来尝试强制执行此操作:
DWORD WINAPI RedrawLoop(LPVOID lpParam) {
HWND handle = (HWND)lpParam;
while (true) {
RECT lpRect;
GetClientRect(handle, &lpRect);
InvalidateRect(handle, &lpRect, TRUE);
UpdateWindow(handle);
}
return 0;
}
However, this is not working.但是,这是行不通的。 I've already checked that the handle passed in is the same as the window handle outside the thread.
我已经检查过传入的句柄是否与线程外的 window 句柄相同。
I've also tried sending SendMessage(handle, WM_PAINT, 0, 0);
我也试过发送
SendMessage(handle, WM_PAINT, 0, 0);
and RedrawWindow(handle, NULL, NULL, 0);
和
RedrawWindow(handle, NULL, NULL, 0);
continuously with no luck.连续没有运气。
First, you can get the current cursor position by processing the WM_MOUSEMOVE
message and invalidate the current window.首先,通过处理
WM_MOUSEMOVE
消息,可以得到当前的cursor position,并使当前的window失效。
Then draw a rectangle through the Rectangle
function in the WM_PAINT
message.然后通过
WM_PAINT
消息中的Rectangle
function 绘制一个矩形。 (Note: The origin of coordinates is the upper left corner of the screen) (注:坐标原点为屏幕左上角)
Here is the sample:这是示例:
#include <Windows.h>
#include <commctrl.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("hello windows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
}
hwnd = CreateWindow(szAppName,
TEXT("the hello program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static POINT pt;
switch (message)
{
case WM_CREATE:
{
}
case WM_MOUSEMOVE:
{
Sleep(100);
GetCursorPos(&pt);
InvalidateRect(hwnd, NULL, FALSE);
return 0;
}
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
Rectangle(hdc, pt.x-rect.right/4 , pt.y-rect.bottom/4, pt.x + rect.right / 4, pt.y + rect.bottom / 4);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.