现在的位置: 首页 > 综合 > 正文

OnEraseBkgnd、 OnPaint、 OnCtlColor的作用

2013年02月03日 ⁄ 综合 ⁄ 共 5603字 ⁄ 字号 评论关闭

 

CWnd::OnEraseBkgnd( CDC*
pDC
);

The framework calls this member function when the CWnd object background needs erasing (for example, when resized).(翻译:当CWnd对象的背景需要擦除时候框架会调用此成员函数)

Remark:

It is called to prepare an invalidated region for painting.

The default implementation erases the background using the window class background brush specified by thehbrBackground member of the window class structure.
(默认是用window类背景刷来擦除背景)

If the hbrBackground member is NULL, your overridden version ofOnEraseBkgnd should erase the background color. Your version should also align the origin of the intended brush with theCWnd
coordinates by first calling UnrealizeObject for the brush, and then selecting the brush.

An overridden OnEraseBkgnd should return nonzero in response toWM_ERASEBKGND if it processes the message and erases the background; this indicates that no further erasing is required. If it returns 0, the window will remain
marked as needing to be erased. (Typically, this means that thefErase member of thePAINTSTRUCT structure will beTRUE.)

Windows assumes the background is computed with the MM_TEXT mapping mode. If the device context is using any other mapping mode, the area erased may not be within the visible part of the client area.

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.

(注意:为了使你的应用程序能够处理windows消息(WM_ERASEBKGND ),框架自动调用此函数。当接收到此消息时,传递给此函数的参数反映了被框架接收的参数。如果你调用的是基类的函数,该函数将会使用原始的·····)

 

CWnd::OnPaint

The framework calls this member function when Windows or an application makes a request to repaint a portion of an application's window.(当windows或者应用程序请求部分重绘时候框架会调用此消息)

Remark:

The WM_PAINT message is sent when the UpdateWindow or RedrawWindow member function is called.(当UpdateWindow or RedrawWindow函数被调用时候,WM_PAINT消息被发送)

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 eachWM_PAINT message because aWM_PAINT message may have been caused by both
an invalid area and a call to theRedrawWindow member function with theRDW_INTERNALPAINT flag set.

An internal WM_PAINT message is sent only once by Windows. After an internalWM_PAINT message is sent to a window by theUpdateWindow member function, no furtherWM_PAINT messages will be sent
or posted until the window is invalidated or until theRedrawWindow member function is called again with theRDW_INTERNALPAINT flag set.

 

CWnd::OnCtlColor( CDC*
pDC
, CWnd* pWnd, UINTnCtlColor);

The framework calls this member function when a child control is about to be drawn.(当子控件将要重绘时,框架将调用此函数。)

Parameters

pDC

Contains a pointer to the display context for the child window. May be temporary.(指向子窗口dc的指针)

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

Return Value

OnCtlColor must return a handle to the brush that is to be used for painting the control background.(返回一个画刷句柄,该画刷用于painting 该控件的背景)

 

Remarks

Most controls send this message to their parent (usually a dialog box) to prepare thepDC for drawing the control using the correct colors.(为了准备绘制控件的pDC,大多数控件发送此消息给它们的父窗口(通常是对话框))

To change the text color, call the SetTextColor member function with the desired red, green, and blue (RGB) values.(使用SetTextColor函数可以改变文本的颜色 )

To change the background color of a single-line edit control, set the brush handle in both theCTLCOLOR_EDIT andCTLCOLOR_MSGBOX message codes, and call the CDC::SetBkColor function in response to theCTLCOLOR_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 aCComboBox
with an override of OnCtlColor that checks forCTLCOLOR_LISTBOX in thenCtlColor parameter. In this handler, theSetBkColor 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.

 

// 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;
}

 

OnPaint会调用OnEraseBkgnd去擦除背景,一般绘图时为了防止闪烁,会在OnEraseBkgnd中添加代码,自己绘制背景,否则默认是以白色画刷擦除背景。OnEraseBkgnd中添加代码失败是因为你需要在画完后直接返回,如return true;不能去调用父类的return CWnd::OnEraseBkgnd();如果不返回就会把你刚刚画的擦除掉。

 

抱歉!评论已关闭.