怎么样使用OnCtlColor消息函数对BUTTON设置不同的颜色

C++语言 码拜 9年前 (2015-11-13) 2026次浏览
本人的目的:程序中有BUTTON控件,IDC_BUTTON3  现在需要设置其字体大小和颜色
做到下面这种程度,debug过了,确实是执行了,但字体大小和颜色没有任何变化~

HBRUSH Csunboxtest11Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
  switch (pWnd->GetDlgCtrlID() ) 
	   {
              case IDC_BUTTON3 :
                    {
                         m_font.CreatePointFont( 300,_T("Helvetica"),NULL );
                            pDC->SelectObject(&m_font); 
                            pDC->SetTextColor(RGB(100,255,100));
                             pDC->SetBkColor(RGB(0,255,0));
	               }  
               }  
	return hbr;
         }

睡不着了
讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教讨教~~~~~

解决方案:15分
CWnd::OnCtlColor
afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );
Return Value
OnCtlColor must return a handle to the brush that is to be used for painting the control background.
Parameters
pDC
Contains a pointer to the display context for the child window. May be temporary.
pWnd
Contains a pointer to the control asking for the color. May be temporary.
nCtlColor
Contains one of the following values, specifying the type of control:
CTLCOLOR_BTN   Button control
CTLCOLOR_DLG   Dialog box
CTLCOLOR_EDIT   Edit control
CTLCOLOR_LISTBOX   List-box control
CTLCOLOR_MSGBOX   Message box
CTLCOLOR_SCROLLBAR   Scroll-bar control
CTLCOLOR_STATIC   Static control
Remarks
The framework calls this member function when a child control is about to be drawn. Most controls send this message to their parent (usually a dialog box) to prepare the pDC for drawing the control using the correct colors.
To change the text color, call the SetTextColor member function with the desired red, green, and blue (RGB) values.
To change the background color of a single-line edit control, set the brush handle in both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX message codes, and call the CDC::SetBkColor function in response to the CTLCOLOR_EDIT code.
OnCtlColor will not be called for the list box of a drop-down combo box because the drop-down list box is actually a child of the combo box and not a child of the window. To change the color of the drop-down list box, create a CComboBox with an override of OnCtlColor that checks for CTLCOLOR_LISTBOX in the nCtlColor parameter. In this handler, the SetBkColor member function must be used to set the background color for the text.
Note   This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.
CWnd Overview |  Class Members |  Hierarchy Chart
See Also   CDC::SetBkColor
解决方案:5分
重载OnCtlColor只能改变窗体和STATIC的颜色,而对于Button则不行
解决方案:5分
要怎么修改Button的颜色请看:http://bbs.csdn.net/topics/60377907
解决方案:10分
从CButton派生一个类,然后在那个派生类中自绘按钮,就是在DrawItem函数中绘制
解决方案:10分
你可以设置一个接口,供外部调用的,例如说SetButtonColor,某个按钮要什么颜色调用就好了

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎么样使用OnCtlColor消息函数对BUTTON设置不同的颜色
喜欢 (0)
[1034331897@qq.com]
分享 (0)