【问题标题】:How do I paint a child window with FillRect()?如何使用 FillRect() 绘制子窗口?
【发布时间】:2013-08-13 06:08:05
【问题描述】:

我有一个使用以下样式创建的主窗口
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_TABSTOP | WS_GROUP | WS_VISIBLE
和前任
WS_EX_ACCEPTFILES | WS_EX_CONTROLPARENT | WS_EX_LEFT | WS_EX_LTRREADING.

这个主窗口上有一个子窗口,它是一个用样式创建的编辑控件
WS_VISIBLE | WS_CHILD | ES_READONLY
和前风格
WS_EX_CLIENTEDGE.

我打算将此编辑控件用作进度条控件。我不想使用标准的 Wind32 进度条控件 (PROGRESS_CLASS),因为我想对其进行一些自定义绘画(例如;动态更改填充颜色、在其上显示文本等)。

我可以通过以下代码绘制主窗口的任何区域:

// hWnd: Handle of the main window  
case WM_PAINT:
    hDc = BeginPaint(hWnd, &Ps);
        Rect = AFunctionToGetCornerThePointsOfTheEditControl();
        Rect.right = Rect.left + 3 * (Rect.right - Rect.left) / 4; // Fill 3/4 (75%) of it
        Rect.left   -= 10; // Enlarge the paint region a little
        Rect.top    -= 10; // so that we can see it if it stays
        Rect.bottom += 10; // under the edit control.
        hBrush = CreateSolidBrush(RGB(50,100,255));
        ret = FillRect(hDc, &Rect, hBrush); // ret = 1 always
        ler = GetLastError();               // ler = 0 
    EndPaint(hWnd, &Ps);
    break;

看起来像这样:

我稍微更改了这段代码来绘制子控件:

// hWndEdit: Handle of the edit control
case WM_PAINT:
    hDc = BeginPaint(hWndEdit, &Ps);
        Rect = AFunctionToGetCornerThePointsOfTheEditControl();
        Rect.right = Rect.left + 3 * (Rect.right - Rect.left) / 4; // Fill 3/4 (75%) of it
        Rect.left   -= 10;
        Rect.top    -= 10;
        Rect.bottom += 10;
        hBrush = CreateSolidBrush(RGB(50,100,255));
        ret = FillRect(hDc, &Rect, hBrush); // ret = 0 always
        ler = GetLastError();               // ler = 6 (ERROR_INVALID_HANDLE) 
    EndPaint(hWndEdit, &Ps);
    break;

这次不行。一旦我将其某些部分拖出屏幕区域,主窗口就会完全消失,并且它变得完全没有响应。其下方的桌面图标可见,但不可点击。

那么,为了绘制子窗口(编辑控件),我必须做什么?

【问题讨论】:

  • 进行自定义绘画的正确方法是继承它。

标签: c++ winapi paint gdi win32gui


【解决方案1】:

这篇文章对我帮助很大:Subclassing Controls

首先,我创建了一个单独的消息处理函数来处理子消息。

LRESULT CALLBACK MyClass::ChildWindowProc(  HWND        hWnd,
                                            UINT        uMsg,
                                            WPARAM      wParam,
                                            LPARAM      lParam,
                                            UINT_PTR    uIdSubclass,
                                            DWORD_PTR   dwRefData)
{
    static PAINTSTRUCT Ps;
    static RECT Rect;
    static HBRUSH hBrush1 = CreateSolidBrush(RGB(50,100,255));
    static HBRUSH hBrush2 = CreateSolidBrush(RGB(255,100,50));
    HDC hDc;
    LRESULT lResult;
    switch (uMsg)
    {
        case WM_PAINT:
            switch (uIdSubclass)
            {
                case 1:
                    hDc = BeginPaint(hWnd, &Ps);
                        Rect.left   = 0;
                        Rect.right  = (LONG) (((double) ITEM2_WIDTH) * Status::GI()->Get_JobCurPercentage());
                        Rect.top    = 0;
                        Rect.bottom = ITEM_HEIGHT - 3;
                        FillRect(hDc, &Rect, hBrush1);
                    EndPaint(hWnd, &Ps);
                    break;
                case 2:
                    hDc = BeginPaint(hWnd, &Ps);
                        Rect.left   = 0;
                        Rect.right  = (LONG) (((double) ITEM2_WIDTH) * Status::GI()->Get_JobTotPercentage());
                        Rect.top    = 0;
                        Rect.bottom = ITEM_HEIGHT - 3;
                        FillRect(hDc, &Rect, hBrush2);
                    EndPaint(hWnd, &Ps);
                    break;
                default:
                    return DefSubclassProc(hWnd, uMsg, wParam, lParam);
            }
            break;
        case WM_NCDESTROY:
            //ReleaseDC(hWnd, hDc);
            return DefSubclassProc(hWnd, uMsg, wParam, lParam);
            break;
        default:
            return DefSubclassProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}

接下来,我将这个函数绑定到控件上:

SetWindowSubclass(  /*_In_  HWND            hWnd*/          ed_cur_Progress.hWnd,
                    /*_In_  SUBCLASSPROC    pfnSubclass*/   ChildWindowProc,
                    /*_In_  UINT_PTR        uIdSubclass*/   1,
                    /*_In_  DWORD_PTR       dwRefData*/     (DWORD_PTR) NULL);
SetWindowSubclass(  /*_In_  HWND            hWnd*/          ed_tot_Progress.hWnd,
                    /*_In_  SUBCLASSPROC    pfnSubclass*/   ChildWindowProc,
                    /*_In_  UINT_PTR        uIdSubclass*/   2,
                    /*_In_  DWORD_PTR       dwRefData*/     (DWORD_PTR) NULL);

而且,仅此而已!结果是惊人的。

【讨论】:

  • BeginPaint 返回的设备上下文不应该通过调用ReleaseDC 来释放。更糟糕的是,您在WM_DESTROY 处理程序中将未初始化的HDC 变量传递给ReleaseDC。关于清理的另一个注意事项:WM_NCDESTROY 是发送到窗口的最后一条消息。那是你应该放置你的清理代码的地方。
  • @Tim 感谢您提供的信息;我将根据它更改我的代码。那我该如何释放设备上下文呢?我首先要释放它吗?
  • 调用EndPaint时,系统会释放从BeginPaint返回的设备上下文相关的资源。我知道留下一个“悬空”HDC 看起来不对,但事实并非如此。参考见Drawing in the Client Area
【解决方案2】:

您正在处理的WM_PAINT 是主窗口的。您需要在其所有者 WM_PAINT 消息中绘制编辑框。我猜你从“hDc = BeginPaint(hWndEdit, &Ps);”得到错误,你可以检查一下。

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 2017-06-21
    • 2016-05-15
    • 1970-01-01
    相关资源
    最近更新 更多