简体   繁体   中英

Forcing the recall of WM_PAINT

I've created a window using CreateWindowEx and that is working fine. The window opens.

In the function where I process messages, specifically WM_PAINT , I grab the cursor position using GetCursorPos and then draw a rectangle there. However, I would like the window to continuously update and redraw this rectangle, acting like a 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.

I've also tried sending SendMessage(handle, WM_PAINT, 0, 0); and 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.

Then draw a rectangle through the Rectangle function in the WM_PAINT message. (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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM