查找BS_GROUPBOX控件的文本高度和标题背景颜色

问题描述 投票:0回答:1

我正在设计一个Group Box控件,其目的是自动排列与其连接的控件。

[Group Box]包含可选的标题文本,我的问题是,当我将Y坐标设置为0(零)的子控件附加时,标题文本将被重叠(不可见),如下所示:

image

我计算出字幕文本的大约高度为20,因此,如果我将Y坐标手动设置为20,则会得到正确的结果:

image

所以,我的第一个问题是,是否有常规方法来获取分组框标题的高度?

我看过GetSystemMetrics(),但似乎没有度量标准。

第二个问题,您可以看到示例标题文本的背景如何不是相同的浅蓝色而是灰色。

我必须处理WM_PAINT来更改此颜色吗?我想避免这种情况,因为仅通过处理WM_ERASEBKGND即可轻松设置“组框”背景色,但标题背景仍然保持灰色(在WM_ERASEBKGND中未处理)。

下面是我用来擦除背景的代码。它是代码注释中链接的代码的修改版本。现在,它使用了一些自定义类型,宏和DirectX,但这不适用于字幕背景。我确定我缺少绘制字幕颜色的东西。

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.
}
c++ winapi directx
1个回答
0
投票
所以,我的第一个问题是,是否有常规方法来获得分组框标题的高度?
您可以使用GetTextExtentPoint32进行高度测量。请参阅“ String Widths and Heights”。

第二个问题,您可以看到示例标题的背景文字不是淡蓝色,而是灰色。

处理WM_CTLCOLORSTATIC消息以设置静态控件的文本背景颜色。

WM_CTLCOLORSTATIC

结果如下:

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;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.