VS2008中如何改变对话框的背景颜色

具体步骤:

1,首先为所要改变颜色的对话框类添加WM_CTLCOLOR消息函数。

2,给你的工程中的对话框类添加一个CBrush变量,如:

CBrush   m_bkBrush; //准备一把背景刷子

3,在对话框的初始化函数OnInitDialogreturn   TRUE的前面添加一行代码:

m_bkBrush.CreateSolidBrush(RGB(255,255,0)); //创建一把黄色的背景刷子

4,最后把你原来添加消息函数的代码改成如下:

HBRUSH   CEX06aDialog::OnCtlColor(CDC*   pDC,   CWnd*   pWnd,   UINT   nCtlColor)

{

         HBRUSH   hbr   =   CDialog::OnCtlColor(pDC,   pWnd,   nCtlColor);  

   

         if(nCtlColor==CTLCOLOR_DLG)

//如果是CTLCOLOR_EDIT edit背景色能改变吗?

         {

              return   m_bkBrush; //返回刚才创建的背景刷子

         }

         //TODO:如果默认的不是所需画笔,则返回另一个画笔

         return   hbr;

}

附加:在MSDN中,关于参数nCtlColor,取以下值:

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

 

 

 

附msdn

The framework calls this member function when a child control is about to be drawn.

afx_msg HBRUSH OnCtlColor(
   CDC* pDC,
   CWnd* pWnd,
   UINT nCtlColor 
);

Collapse imageParameters

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

Collapse imageReturn Value

OnCtlColor must return a handle to the brush that is to be used for painting the control background.

Collapse imageRemarks

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.

NoteNote:

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.

Collapse imageExample

Visual C++ Copy imageCopy Code
// This OnCtlColor handler will change the color of a static control
// with the ID of IDC_MYSTATIC. The code assumes that the CPenWidthsDlg
// class has an initialized and created CBrush member named m_brush.
// The control will be painted with red text and a background
// color of m_brush.
HBRUSH CPenWidthsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
   // Call the base class implementation first! Otherwise, it may
   // undo what we're trying to accomplish here.
   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

   // Are we painting the IDC_MYSTATIC control? We can use
   // CWnd::GetDlgCtrlID() to perform the most efficient test.
   if (pWnd->GetDlgCtrlID() == IDC_MYSTATIC)
   {
      // Set the text color to red
      pDC->SetTextColor(RGB(255, 0, 0));

      // Set the background mode for text to transparent 
      // so background will show thru.
      pDC->SetBkMode(TRANSPARENT);

      // Return handle to our CBrush object
      hbr = m_brush;
   }

   return hbr;
}

Collapse imageRequirements

Header: afxwin.h

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值