我正在使用 Windows API 创建一个基本的 GUI,但我遇到了一个问题.它从一个主窗口开始,该窗口以我设置的自定义背景颜色打开 (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.
我的问题是,我该怎么做?我目前使用下面的方法并且它有效,但我想知道,有没有办法在静态控件的 CreateWindow
函数之后以某种方式永久设置背景颜色,而无需更改系统颜色,以及只需将它应用于那个控件,而不是任何发送 WM_CTLCOLORSTATIC
消息的控件.我已经尝试在消息循环之外使用 GetDC
函数和 SetBkColor
函数,但没有任何效果.
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 would be very thankful for any help that could be provided, at least pointing me in the right direction, thanks.
对于静态文本控件,没有永久的方法来设置文本颜色或其背景.即使您想将更改应用于单个静态控件;您仍然需要在即将绘制控件时在父 dlgproc 中处理 WM_CTLCOLORSTATIC 通知消息.
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.
这是由于 DefWindowProc
每次处理 WM_CTLCOLORSTATIC
时都会覆盖您对设备上下文的更改,如 MSDN:
This is due to the DefWindowProc
overwriting your changes to the device context each time it handles WM_CTLCOLORSTATIC
as stated in the MSDN:
默认情况下,DefWindowProc 函数为静态控件选择默认系统颜色.
By default, the DefWindowProc function selects the default system colors for the static control.
static HBRUSH hBrush = CreateSolidBrush(RGB(230,230,230));
case WM_CTLCOLORSTATIC:
{
if (settingstext == (HWND)lParam)
//OR if the handle is unavailable to you, get ctrl ID
DWORD CtrlID = GetDlgCtrlID((HWND)lParam); //Window Control ID
if (CtrlID == IDC_STATIC1) //If desired control
{
HDC hdcStatic = (HDC) wParam;
SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230));
return (INT_PTR)hBrush;
}
}
如果您想让控件的背景在父对话框上透明,您可以使用 SetBkMode(hdcStatic, TRANSPARENT)
.
If you're looking to make the control's background transparent over a parent dialog you could use SetBkMode(hdcStatic, TRANSPARENT)
.
这篇关于使用 C++ 静态控制背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!