MFC背景颜色

本文介绍了如何在MFC中通过CWinApp::SetDialogBkColor改变对话框背景颜色,CWnd::OnPaint进行窗口背景填充,并利用CWnd::OnCtlColor定制控件和对话框的颜色。详细讲述了各个方法的使用和实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、从InitInstance设置应用程序默认背景和对话框颜色。

CWinApp::SetDialogBkColor

This method is called from within the InitInstance method to set the default background and text color for dialog boxes and message boxes within your application.

void SetDialogBkColor( 
COLORREF clrCtlBk = RGB(192, 192, 192), 
COLORREF clrCtlText = RGB(0, 0, 0) ); 
eg:

        CMyDlg dlg;
SetDialogBkColor(RGB(69,137,148),RGB(220,20,10));
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();


2、使用OPaint设置应用程序默认背景。

CWnd::OnPaint 

afx_msg void OnPaint( );

Remarks

The framework calls this member function when Windows or an application makes a request to repaint a portion of an application’s window. TheWM_PAINT message is sent when the UpdateWindow or RedrawWindow member function is called.

A window may receive internal paint messages as a result of calling the RedrawWindow member function with the RDW_INTERNALPAINT flag set. In this case, the window may not have an update region. An application should call the GetUpdateRect member function to determine whether the window has an update region. If GetUpdateRect returns 0, the application should not call the BeginPaint and EndPaint member functions.

It is an application’s responsibility to check for any necessary internal repainting or updating by looking at its internal data structures for each WM_PAINT message because a WM_PAINT message may have been caused by both an invalid area and a call to the RedrawWindow member function with the RDW_INTERNALPAINT flag set.

An internal WM_PAINT message is sent only once by Windows. After an internal WM_PAINT message is sent to a window by the UpdateWindow member function, no further WM_PAINT messages will be sent or posted until the window is invalidated or until the RedrawWindow member function is called again with the RDW_INTERNALPAINT flag set.

For information on rendering an image in document/view applications, see CView::OnDraw.

For more information about using WM_Paint, see the following topics in the Win32 SDK Programmer’s Reference:

  • The WM_PAINT Message

  • Using the WM_PAINT Message

CWnd OverviewClass MembersHierarchy Chart

See Also   CWnd::BeginPaint, CWnd::EndPaint, CWnd::RedrawWindow, CPaintDC, CView::OnDraw

eg:

       CPaintDC dc(this);   //窗体CDC
       CBrush brush;    
       CRect rect;
       GetClientRect(&rect);     //客户区大小
       for(int m = 255;m > 0;m--)
{
            int x,y;
            x = rect.Width() * m / 255;   //计算绘制宽度
            y = rect.Height() * m / 255;//计算绘制高度
            brush.DeleteObject();
    brush.CreateSolidBrush(RGB(186,m,128));//定义指定颜色画刷
            dc.FillRect(CRect(0,0,x,y),&brush);//填充矩形
       }


3、使用OnCtlColor设置控件、对话框颜色(button控件已经不发送CTLCOLOR_BTN消息了)。

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.

Example



HBRUSH CZilchDlg::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;
}
如果要指定某个特定控件可以这样写:ID为IDC_STATIC1
if (pWnd->GetDlgCtrlID()==IDC_STATIC1)
{
       pDC->SetTextColor(RGB(255,0,0));  //设置字体颜色
       pDC->SetBkMode(TRANSPARENT); //设置字体背景为透明
      // TODO: Return a different brush if the default is not desired
      return (HBRUSH)::GetStockObject(BLACK_BRUSH);  // 设置背景色
}
else
return hbr;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值