1

我正在开发一个 win32 项目,该项目使用WM_MOVING来自的消息WndProc来检测窗口移动的新位置。但是当我尝试将窗口捕捉到左侧时,LPARAM 会在一毫秒内给出错误的值,然后给出实际值。

请查看输出:

Left: 0, Right:683
Left: -205, Right:295
Left: 0, Right:683
Left: -205, Right:295
Left: 0, Right:683
Left: -205, Right:295

实际输出是 -205,但在两者之间我得到零,这导致我的窗口内容闪烁。在我的情况下,内容的位置取决于窗口位置,因此如果我收到错误的值,它会导致闪烁。

这是我想要实现的目标:手动创建丙烯酸效果

IMG 航空按扣

当我在这种模式下(就在航空捕捉模式之前)移动窗口时,该值将变为零。

这只会影响窗口的TopLeft坐标,并且在航空捕捉到任何侧面时也会闪烁。

这是一个简单的可复制示例:

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
RECT* hostRect;
char buffer[200];

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{

    const wchar_t CLASS_NAME[] = L"Sample Window Class";
    WNDCLASS wc = { };

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(0,CLASS_NAME,L"Learn to Program Windows",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_MOVING:
    {
        hostRect = reinterpret_cast<RECT*>(lParam);
        sprintf_s(buffer, "Left: %d, Top:%d, Right:%d, Bottom:%d\n", hostRect->left, hostRect->top, hostRect->right, hostRect->bottom);
        OutputDebugStringA(buffer);
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
        EndPaint(hwnd, &ps);
    }
    return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

运行此程序并检查输出中的值,同时将窗口移动到任何一侧以进行航空捕捉。

注意: 在我的情况下,我将无法使用GetWindowRect()in WM_MOVING,因为在我的窗口中调用会GetWindowRect()减慢 render() 函数(只是一个 directx 绘画)。

4

1 回答 1

1
于 2021-01-18T09:42:18.873 回答