如何使用FillRect()绘制子窗口?

时间:2013-08-12 01:22:51

标签: c++ winapi paint gdi win32gui

我有一个主窗口,使用以下样式创建 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;

看起来像这样:
appearance of the painted window

我改变了这段代码来改为绘制子控件:

// 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;

这次它不起作用。一旦我将一部分拖出屏幕区域,主窗口就完全消失了,它变得完全没有响应。它下面的桌面图标是可见的,但不可点击。

那么,为了绘制子窗口(编辑控件),我该怎么办?

2 个答案:

答案 0 :(得分: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);

而且,就是这样!结果令人惊讶。

appearance

答案 1 :(得分:-1)

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