查找文本高度并设置 BS_GROUPBOX 控件的标题背景颜色
Find text height and set caption background color of BS_GROUPBOX control
我正在设计一个 Group Box 控件,其目的是自动排列附加到它的控件。
组框包含一个可选的标题文本,我的问题是当我附加 child 控件并将 Y
坐标设置为 0
(零)时,标题文本将是重叠(不可见),像这样:
我想出来了,字幕文字的高度大约是20,所以如果我手动设置Y
坐标为20,我得到正确的结果:
所以,我的第一个问题是,是否有一种常规方法来获取 Group Box 标题的高度?
我确实看过 GetSystemMetrics()
,但似乎没有相关指标。
第二个问题,你可以看到示例字幕文本的背景不是浅蓝色而是灰色。
我是否必须处理 WM_PAINT
才能更改此颜色?我想避免这种情况,因为只需处理 WM_ERASEBKGND
即可轻松设置组框背景颜色,但标题背景仍保持灰色(未在 WM_ERASEBKGND
中处理)。
下面是我用来擦除背景的代码。它是代码注释中 link 代码的修改版本。现在它使用了一些自定义类型、宏和 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.
}
So, my first question is, is there a conventional way to get the
height of the Group Box caption?
您可以使用GetTextExtentPoint32
进行身高测量。参考“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.
要更改标题背景颜色,您可以通过处理 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;
}
}
结果如下:
我正在设计一个 Group Box 控件,其目的是自动排列附加到它的控件。
组框包含一个可选的标题文本,我的问题是当我附加 child 控件并将 Y
坐标设置为 0
(零)时,标题文本将是重叠(不可见),像这样:
我想出来了,字幕文字的高度大约是20,所以如果我手动设置Y
坐标为20,我得到正确的结果:
所以,我的第一个问题是,是否有一种常规方法来获取 Group Box 标题的高度?
我确实看过 GetSystemMetrics()
,但似乎没有相关指标。
第二个问题,你可以看到示例字幕文本的背景不是浅蓝色而是灰色。
我是否必须处理 WM_PAINT
才能更改此颜色?我想避免这种情况,因为只需处理 WM_ERASEBKGND
即可轻松设置组框背景颜色,但标题背景仍保持灰色(未在 WM_ERASEBKGND
中处理)。
下面是我用来擦除背景的代码。它是代码注释中 link 代码的修改版本。现在它使用了一些自定义类型、宏和 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.
}
So, my first question is, is there a conventional way to get the height of the Group Box caption?
您可以使用GetTextExtentPoint32
进行身高测量。参考“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.
要更改标题背景颜色,您可以通过处理 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;
}
}
结果如下: