c++ - 当样式为 WS_CHILD 时,通过 ps.rcPaint 查找脏区域不起作用

标签 c++ winapi visual-studio-2013 gdi

我用C++写过用WinApi画的程序。 我的回调函数:

/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage(0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_ERASEBKGND:
            {
                elWidget *widget = (elWidget *)GetWindowLong(hwnd, GWL_USERDATA);
                if (widget)
                {
                    PAINTSTRUCT ps;
                    HDC hdc = BeginPaint(hwnd, &ps);
                    HBRUSH hBrush = CreateSolidBrush(widget->color.ColorRef());
                    FillRect((HDC)wParam, &ps.rcPaint, hBrush);
                    DeleteObject(hBrush);
                    EndPaint(hwnd, &ps);
                }
            }
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

它适用于独立窗口(样式为 WS_OVERLAPPED),但当样式为 WS_CHILDWS_CHILD | WS_VISIBLE 那么 ps.rcPaint 总是 (0,0,0,0)。我不知道如何修复它。

elButton::elButton(elWidget *owner)
        : elWidget(owner)
{
    WNDCLASSEX winclChild;        /* Data structure for the windowclass */
    /* The Window structure */
    winclChild.hInstance = gThisInstance; //global variable instance
    winclChild.lpszClassName = L"Child";
    winclChild.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    winclChild.style = CS_DBLCLKS;                 /* Catch double-clicks */
    winclChild.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    winclChild.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    winclChild.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    winclChild.hCursor = LoadCursor (NULL, IDC_ARROW);
    winclChild.lpszMenuName = NULL;                 /* No menu */
    winclChild.cbClsExtra = 0;                      /* No extra bytes after the window class */
    winclChild.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    winclChild.hbrBackground = 0;// CreateSolidBrush(RGB(255, 200, 200));//(HBRUSH)COLOR_WINDOW;//COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx(&winclChild))
        return;
    hwnd = CreateWindowEx(
        0,                   /* Extended possibilites for variation */
        L"Child",         /* Classname */
        L"Title",       /* Title Text */
        WS_CHILD | WS_VISIBLE,
        100,
        100,
        40,
        40,
        owner->getHwnd(),        /* The window is a child-window to desktop */
        NULL,                /* No menu */
        gThisInstance,       /* Program Instance handler */
        this                 /* to lParam */
       );
    SetWindowLong(hwnd, GWL_USERDATA, (long)this);
}

我可以在 Google 磁盘上添加整个项目的链接,但我不能保证它会永久存在多年。

最佳答案

BeginPaint 应该只在 WM_PAINT 上调用,

要获取 WM_ERASEBKGND 上的剪辑框,请调用 GetClipBox((HDC)wParam,&rect); 而不是

关于c++ - 当样式为 WS_CHILD 时,通过 ps.rcPaint 查找脏区域不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33976115/

相关文章:

c++ - 2D 几何库 : LGPL alternative to CGAL?

c++ - cmath 精度误差中的底函数

batch-file - 是否有 API 公开启动命令搜索的路径?

c++ - __vfptr 表在未删除的对象中损坏

c++ - 当我的代码到达时我可以得到 "Automatic Breakpoints"吗?

c++ - Uniform Initialization with curly brace 被误认为是 Initializer List

c++ - 如何在 llvm 中创建命名局部变量?

windows - 当远程桌面断开连接时,GetForegroundWindow 返回 Null

windows - 覆盖默认的 F1 Windows 帮助行为

c# - C# 中的电子邮件通知系统