C++ 注册后如何设置窗口的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3463471/
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 12:53:32  来源:igfitidea点击:

How to set background color of window after I have registered it?

c++apiwinapi

提问by Kaije

I am not using a dialog, I'm using my own custom class which I have registered and then used the CreateWindowcall to create it, I have preset the background color to red when registering:

我没有使用对话框,我使用的是我自己注册的自定义类,然后使用CreateWindow调用来创建它,注册时我已将背景颜色预设为红色:

WNDCLASSEX wc;
wc.hbrBackground = CreateSolidBrush(RGB(255, 0, 0));

But now I want to change the background color at runtime, by e.g. clicking a button to change it to blue.

但是现在我想在运行时更改背景颜色,例如单击按钮将其更改为蓝色。

I have tried to use SetBkColor()call in the WM_PAINT, and tried returning a brush from the WM_CTLCOLORDLGmessage, they don't work.

我尝试在 中使用SetBkColor()call WM_PAINT,并尝试从WM_CTLCOLORDLG消息中返回画笔,但它们不起作用。

Any help?

有什么帮助吗?

采纳答案by Zabba

From Window Backgroundcomes:

窗口背景来:

...The system paints the background for a window or gives the window the opportunity to do so by sending it a WM_ERASEBKGND message when the application calls BeginPaint. If an application does not process the message but passes it to DefWindowProc, the system erases the background by filling it with the pattern in the background brush specified by the window's class.....

...... An application can process the WM_ERASEBKGND message even though a class background brush is defined. This is typical in applications that enable the user to change the window background color or pattern for a specified window without affecting other windows in the class. In such cases, the application must not pass the message to DefWindowProc. .....

...系统为窗口绘制背景,或者在应用程序调用 BeginPaint 时通过向窗口发送 WM_ERASEBKGND 消息来为窗口提供绘制背景的机会。如果应用程序不处理该消息而是将其传递给 DefWindowProc,则系统通过用窗口类指定的背景画笔中的图案填充它来擦除背景......

...... 应用程序可以处理即使定义了类背景画笔,WM_ERASEBKGND 消息也是如此。这在允许用户更改指定窗口的窗口背景颜色或图案而不影响类中的其他窗口的应用程序中很典型。在这种情况下,应用程序不得将消息传递给 DefWindowProc。.....

So, use the WM_ERASEBKGND message's wParam to get the DC and paint the background.

因此,使用 WM_ERASEBKGND 消息的 wParam 获取 DC 并绘制背景。

回答by Tassos

You may try the following:

您可以尝试以下操作:

HBRUSH brush = CreateSolidBrush(RGB(0, 0, 255));
SetClassLongPtr(hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)brush);

回答by Adrian McCarthy

Short answer: Handle WM_ERASEBKGND.

简短回答:处理WM_ERASEBKGND

Longer answer:

更长的答案:

When you register the WNDCLASS, you're providing information about all windows of that class. So if you want to change the color of just one instance of the window, you'll need to handle it yourself.

当您注册 WNDCLASS 时,您将提供有关该类的所有窗口的信息。所以如果你只想改变一个窗口实例的颜色,你需要自己处理。

When it's time to repaint your window, the system will send your wndproc a WM_ERASEBKGNDmessage. If you don't handle it, the DefWindowProcwill erase the client area with the color from the window class. But you can handle the message directly, painting whatever color (or background pattern) you like.

当需要重新绘制您的窗口时,系统会向您的 wndproc 发送一条WM_ERASEBKGND消息。如果你不处理它,DefWindowProc它将用窗口类的颜色擦除客户区。但是您可以直接处理消息,绘制您喜欢的任何颜色(或背景图案)。