静态控制背景颜色与 C++
我正在使用 Windows API 创建一个基本的 GUI,但遇到了一个问题。它从一个主窗口开始,该窗口以我设置的自定义背景颜色(RGB(230,230,230))
打开。然后,它使用静态控件在左上角显示文本。
settingstext = CreateWindow("STATIC",
"SETTINGS",
SS_LEFT | WS_CHILD,
12,
20,
100,
20,
hwnd,
NULL,
proginstance,
NULL);
ShowWindow(settingstext, 1);
这是可行的,但是当显示文本时,我需要一种方法来更改它的背景以匹配主窗口,否则它看起来就像没有融入其中。
我的问题是,我该怎么做?我目前使用下面的方法并且它有效,但我想知道,有没有一种方法可以在静态控件的 CreateWindow 函数之后以某种方式永久设置背景颜色而不更改系统颜色,以及只需将其应用于该一个控件,而不应用于发送 WM_CTLCOLORSTATIC
消息的任何控件。我尝试过在消息循环之外使用 GetDC 函数和 SetBkColor 函数,但没有任何效果。
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230));
return (INT_PTR)CreateSolidBrush(RGB(230,230,230));
}
我想这样做是因为...
- 我不想用每次重新绘制窗口时都需要调用的函数来填充我的消息循环。
- 让更改仅应用于此静态控件。
我将非常感谢您提供的任何帮助,至少为我指明了正确的方向,谢谢。
I am creating a basic GUI with the Windows API and I have run into an issue. It starts with a main window that opens with a custom background color I set (RGB(230,230,230))
. It then displays text in the upper left corner with the static control.
settingstext = CreateWindow("STATIC",
"SETTINGS",
SS_LEFT | WS_CHILD,
12,
20,
100,
20,
hwnd,
NULL,
proginstance,
NULL);
ShowWindow(settingstext, 1);
This works, but when the text is displayed I need a way to change the background of it to match the main window or else it just looks like it doesn't blend in.
My question is, how do I do this? I currently use the method below and it works, but I wanted to know, is there a way to permanently set the background color somehow, right after the CreateWindow
function for the static control without changing system colors, and just have it apply to that one control and not anything that sends the WM_CTLCOLORSTATIC
message. I have experimented around with using the GetDC
function and SetBkColor
function outside of the message loop but nothing works.
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230));
return (INT_PTR)CreateSolidBrush(RGB(230,230,230));
}
I want to do this because...
- I don't want to fill up my message loop with functions that need to be called every time the window repaints.
- Have the changes apply to only this static control.
I would be very thankful for any help that could be provided, at least pointing me in the right direction, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于静态文本控件,没有永久的方法来设置文本颜色或其背景。即使您想将更改应用到单个静态控件;当控件即将被绘制时,您仍然需要在父 dlgproc 中处理 WM_CTLCOLORSTATIC 通知消息。
这是由于
DefWindowProc
每次处理WM_CTLCOLORSTATIC
时都会覆盖您对设备上下文的更改,如 MSDN:如果您希望使控件的背景在父对话框上透明,可以使用
SetBkMode(hdcStatic, TRANSPARENT)
。For static text controls there's no permanent way to set the text color or their background. Even if you want to apply the changes to a single static control; you would still have to handle WM_CTLCOLORSTATIC notification message in parent dlgproc just when the control is about to be drawn.
This is due to the
DefWindowProc
overwriting your changes to the device context each time it handlesWM_CTLCOLORSTATIC
as stated in the MSDN:If you're looking to make the control's background transparent over a parent dialog you could use
SetBkMode(hdcStatic, TRANSPARENT)
.您是否考虑过对静态窗口进行子类化并进行所有者绘制?
Have you considered subclassing the static window and doing owner draw?
我认为有一个永久的方法可以做到这一点。
创建标签后,使用 GetDC() 函数获取设备上下文。
然后使用:
它应该可以。
I think there is a permanent way to do it.
Just after you create the label,use GetDC() function to get the Device Context.
Then use:
And it should do .