简体   繁体   中英

Find text height and set caption background color of BS_GROUPBOX control

I'm designing a Group Box control whose purpose is to automatically arrange controls attached to it.

The Group Box contains an optional caption text, and my problem is that when I attach child controls with Y coordinate set to 0 (zero) the caption text will be overlapped (not visible), like this:

图片

I figured out, the approximate height of the caption text is 20, so if I manually set Y coordinate to 20 I get the correct result:

图片

So, my first question is, is there a conventional way to get the height of the Group Box caption?

I did take a look at GetSystemMetrics() but there doesn't seem be a metric for this.

Second question, you can see how the background of the sample caption text is not same light blue color but gray.

Do I have to handle WM_PAINT to change this color? I would like to avoid this, since the Group Box background color could be easily set by just handling WM_ERASEBKGND but the caption background stays gray anyway (not handled in WM_ERASEBKGND ).

Below is code I used to erase the background. It is a modified version of the code from the link in the code comments. Now it uses some custom types, macros and DirectX, but this doesn't work for the caption background. I'm sure I'm missing something to draw the caption color.

case WM_ERASEBKGND:
{
    /*
    SYMPTOMS
    ========
    When a BS_GROUPBOX style window is created, its background does not erase
    correctly.

    CAUSE
    =====
    The parent window of the BS_GROUPBOX style window has the WS_CLIPCHILDREN style,
    which prevents the parent window from erasing the group box's background.

    RESOLUTION
    ==========
    Subclass the group box window to process the WM_ERASEBKGND message by erasing
    its background. Listed below is a code fragment to demonstrate this procedure.

    STATUS
    ======
    This behavior is by design.

    MORE INFORMATION
    ================
    https://jeffpar.github.io/kbarchive/kb/079/Q79982/
    */

    HRESULT hr = CreateGraphicsResources();
    SmartObject<DrawableWindow> parent = nullptr;

    if (FAILED(hr) || !mParent->IsDrawable())
        return FALSE;

    // Obtain parent window's background color.
    parent = mParent;
    const D2D1::ColorF color = parent->GetBackgroundColor();
    mpBrush->SetColor(color);

    // Other drawing variables
    RECT rect{ };
    HDC hDC = GetDC(mhWnd);
    auto pRender = std::get<1>(mpRenderTarget);
    const D2D1_SIZE_F size = pRender->GetSize();
    const D2D1_RECT_F rectangle = D2D1::RectF(0, 0, size.width, size.height);

    // Erase the group box's background.
    GetClientRect(mhWnd, &rect);
    pRender->BindDC(hDC, &rect);

    pRender->BeginDraw();
    pRender->FillRectangle(&rectangle, mpBrush);
    hr = pRender->EndDraw();

    if (FAILED(hr))
    {
        ShowError(ERR_BOILER, hr);
    }

    ReleaseDC(mhWnd, hDC);

    // Instruct Windows to paint the group box text and frame.
    InvalidateRect(mhWnd, &rect, FALSE);

    // Insert code here to instruct the contents of the group box
    // to repaint as well.

    return TRUE; // Background has been erased.
}

So, my first question is, is there a conventional way to get the height of the Group Box caption?

You can use GetTextExtentPoint32 for height measurement. Refer to " String Widths and Heights ".

Second question, you can see how the background of the sample caption text is not same light blue color but gray.

For changing caption background color you can via handle WM_CTLCOLORSTATIC message to set the text background colors of the static control.

case WM_CTLCOLORSTATIC:
{
    if (GetDlgItem(hDlg, IDC_STATIC4) == (HWND)lParam)
    {
        HDC hdcStatic = (HDC)wParam;
        SetBkColor(hdcStatic, RGB(0, 255, 0));

        if (hbrBkgnd == NULL)
        {
            hbrBkgnd = CreateSolidBrush(RGB(0, 255, 0));
        }
        return (INT_PTR)hbrBkgnd;
    }
}

Result like this:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM