简体   繁体   English

MFC自定义控件背景/文本颜色

[英]MFC Custom Control Background/Text Color

So I have used MFC for a few years and made various interfaces which include custom controls. 因此,我已经使用MFC几年了,制作了包括自定义控件在内的各种界面。 I was recently asked to start making an interface which can have dynamically set background and text colors. 最近有人要求我开始制作一个可以动态设置背景和文本颜色的界面。 I started working on this and have been running into an issue getting some of my custom controls to behave. 我开始研究此问题,并且遇到了使某些自定义控件无法正常运行的问题。 All the controls which generate WM_CTLCOLOR messages work really nicely using that interface to allow the parent to set the colors of the child controls, but not all the controls I have customized generate these messages. 使用该接口,所有生成WM_CTLCOLOR消息的控件都可以很好地工作,以允许父级设置子级控件的颜色,但是并非我自定义的所有控件都会生成这些消息。 Specifically a class I built off of CTabCrtl and a few based on CWnd. 具体来说,我是基于CTabCrtl建立的一个类,还有一些基于CWnd的类。 These classes don't seem to generate WM_CTLCOLOR messages before they call a paint. 这些类在调用绘画之前似乎不会生成WM_CTLCOLOR消息。 I was wondering if anyone had any ideas on how to either get the background and text color of a controls parent CWnd or how to manually create a WM_CTLCOLOR message. 我想知道是否有人对如何获取控件父级CWnd的背景和文本颜色或如何手动创建WM_CTLCOLOR消息有任何想法。 I have tried to get the current DC of the controls parent so I can call GetBkColor() and GetTextColor() but doing this always seems to return default values. 我试图获取控件父级的当前DC,以便可以调用GetBkColor()和GetTextColor(),但这样做似乎总是返回默认值。 I have also tried to create WM_CTLCOLOR messages but don't know where I should create them or the exact syntax.. Any help would be great. 我也尝试过创建WM_CTLCOLOR消息,但不知道我应该在哪里创建它们或确切的语法。.任何帮助都将非常有用。

The idea of WM_CTLCOLOR is that is has to be called from within the WM_PAINT and WM_ERASEBKGND. WM_CTLCOLOR的想法是必须从WM_PAINT和WM_ERASEBKGND内部调用。 In such cases you have a DC. 在这种情况下,您将拥有DC。 You send the message to the parent and you get a brush and the text and background Color gets set. 您将消息发送给父母,然后得到画笔,并设置了文本和背景颜色。

I see only a major problem that WM_CTLCOLOR isn't a real window message... it is just syntesized from the various WM_CTLCOLOR... messages described in the SDK. 我只看到一个主要问题,即WM_CTLCOLOR不是真正的窗口消息...它只是从SDK中描述的各种WM_CTLCOLOR ...消息中合成的。

So the syntax is documented in the MSDN. 因此,该语法已在MSDN中进行了记录。 Cast the DC handle to WPARAM and hast the window handle to LPARAM... 将DC手柄投射到WPARAM上,并将窗口手柄投射到LPARAM ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM