使用 C++ 静态控制背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4495509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:33:15  来源:igfitidea点击:

Static Control Background Color with C++

c++winapicontrolsstaticbackground-color

提问by contrapsych

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.

我正在使用 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);

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 CreateWindowfunction for the static control without changing system colors, and just have it apply to that one control and not anything that sends the WM_CTLCOLORSTATICmessage. I have experimented around with using the GetDCfunction and SetBkColorfunction outside of the message loop but nothing works.

我的问题是,我该怎么做?我目前使用下面的方法并且它有效,但我想知道,有没有办法以某种方式永久设置背景颜色,就在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 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.

我将非常感谢可以提供的任何帮助,至少为我指明正确的方向,谢谢。

回答by cpx

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.

对于静态文本控件,没有永久的方法来设置文本颜色或其背景。即使您想将更改应用于单个静态控件;当控件即将被绘制时,您仍然需要在父 dlgproc 中处理 WM_CTLCOLORSTATIC 通知消息。

This is due to the DefWindowProcoverwriting your changes to the device context each time it handles WM_CTLCOLORSTATICas stated in the MSDN:

这是因为DefWindowProc每次WM_CTLCOLORSTATIC按照MSDN 中的说明进行处理时都会覆盖您对设备上下文的更改:

By default, the DefWindowProc function selects the default system colors for the static control.

默认情况下,DefWindowProc 函数为静态控件选择默认系统颜色。

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;
    }
}

If you're looking to make the control's background transparent over a parent dialog you could use SetBkMode(hdcStatic, TRANSPARENT).

如果您想让控件的背景在父对话框上透明,您可以使用SetBkMode(hdcStatic, TRANSPARENT).

回答by NoMadCap

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:

创建标签后,使用 GetDC() 函数获取设备上下文。然后使用:

SetTextColor(hdcStatic, RGB(0,0,0));
SetBkColor(hdcStatic, RGB(230,230,230)); // Code Copied from the above answer by cpx.

And it should do .

它应该做。

回答by Jonathan Wood

Have you considered subclassing the static window and doing owner draw?

您是否考虑过继承静态窗口并进行所有者绘制?