3

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

这个主窗口上有一个子窗口,它是一个用样式
WS_VISIBLE | WS_CHILD | ES_READONLY
和 ex-style创建的编辑控件
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;

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

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

4

2 回答 2

1

这篇文章对我帮助很大:子类化控件

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

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

就这样!结果是惊人的。

外貌

于 2013-08-12T10:08:33.913 回答
-1

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

于 2013-08-12T02:32:40.057 回答