天天看點

MFC 技術注意第62條的翻譯

技術注意062:Windows 控件的消息反射

TN062: Message Reflection for Windows Controls

本技術注意講述了消息反射,一個 MFC4.0 中的新特色。同時講述了使用消息反射建立一個簡單可重用控件的指導。

This technical note describes message reflection, a new feature in MFC 4.0. It also contains directions for creating a simple reusable control that uses message reflection.

本文并沒有就 ActiveX 控件(以前稱為 OLE 控件)如何進行消息反射的。有關這方面的資料請參見《ActiveX Controls: Subclassing a Windows Control in Visual C++ Programmer's Guide》。

This technical note does not discuss message reflection as it applies to ActiveX controls (formerly called OLE controls). Please see the articleActiveX Controls: Subclassing a Windows Control in Visual C++ Programmer's Guide.

什麼是消息反射?

What Is Message Reflection?

視窗不斷地頻繁地發送通知消息給其父視窗。 例如:許多控件會發送控件顔色通知消息(WM_CTLCOLOR 或它的一個變體)給其父視窗以允許其父視窗來提供一個繪制控件背景的刷子。

Windows controls frequently send notification messages to their parent windows. For instance, many controls send a control color notification message (WM_CTLCOLOR or one of its variants) to their parent to allow the parent to supply a brush for painting the background of the control.

在 Windows 和 MFC4.0 以前的版本中,父視窗——通常是一個對話框,會來響應處理這些消息。這就意味着處理這些消息的代碼需要在父視窗類中實作,而且對于每一個類都要處理這些消息。在上述的情況下,每個需要自定義背景控件的對話框都不得不處理控件顔色通知消息。如果一個控件可以處理自己的背景顔色,而且可以被重用,事情将變的簡單的多。

In Windows and in MFC prior to version 4.0, the parent window, often a dialog box, is responsible for handling these messages. This means that the code for handling the message needs to be in the parent window’s class and that it has to be duplicated in every class that needs to handle that message. In the case above, every dialog box that wanted controls with custom backgrounds would have to handle the control color notification message. It would be much easier to reuse code if a control class could be written that would handle its own background color.

在 MFC 4.0 中,舊的消息機制仍然在工作 —— 父視窗可以處理通知消息。另外同時,MFC 4.0 通過提供了一種稱為“消息反射”的新機制使的重複使用變的容易多了。消息反射允許這些通知消息既能夠被控件自身處理,也能夠被其父視窗來處理。在上述控件背景顔色例子中,現在你就既可以通過被發射過來的WM_CTLCOLOR消息自己寫一個能夠處理背景顔色的控件類,所有的一切就不再依靠其父視窗了。(請注意,既然這個消息反射隻能在 MFC 實作,不是 Windows 系統的。是以父視窗必須派生于CWnd,這樣消息反射才能正常工作)。

In MFC 4.0, the old mechanism still works—parent windows can handle notification messages. In addition, however, MFC 4.0 facilitates reuse by providing a feature called “message reflection” that allows these notification messages to be handled in either the child control window or the parent window, or in both. In the control background color example, you can now write a control class that sets its own background color by handling the reflected WM_CTLCOLOR message—all without relying on the parent. (Note that since message reflection is implemented by MFC, not by Windows, the parent window class must be derived from CWnd for message reflection to work.)

MFC 舊版本通過為一些消息提供虛函數的機制部分實作了類似的功能,最典型的就是自繪制的清單框(WM_DRAWITEM等等)。然後新的消息反射機制更通用和持久。

Older versions of MFC did something similar to message reflection by providing virtual functions for a few messages, such as messages for owner-drawn list boxes (WM_DRAWITEM, and so on). The new message reflection mechanism is generalized and consistent.

消息消息機制向後與MFC4.0版本前的代碼相相容。

Message reflection is backward compatible with code written for versions of MFC previous to 4.0.

如果你在控件的父視窗類中為一個或一定範圍特定的消息提供了一個處理函數,對于同樣的消息,如果在您的進行中您并沒有調用其基類的處理函數它就會覆寫掉被反射的消息處理。例如,如果你在一個對話框類中試圖處理 WM_CTLCOLOR,您的處理将覆寫掉任何被反射的消息處理函數。

If you have supplied a handler for a specific message, or for a range of messages, in your parent window's class, it will override reflected message handlers for the same message provided you don't call the base class handler function in your own handler. For example, if you handle WM_CTLCOLOR in your dialog box class, your handling will override any reflected message handlers.

如果你在父視窗中為一個或一系列一定範圍的特定的 WM_NOTIFY 消息提供一個處理函數,您的處理函數隻有當這些發送消息的子控件通過ON_NOTIFY_REFLECT() 宏就不會有一個被反射消息處理了。如果該處理傳回 TRUE,消息就也會給父視窗來處理,而如果它傳回的是一個 FALSE值,就不會讓父視窗來處理該消息。請注意:被反射的消息是在通知消息之前被處理。

If, in your parent window class, you supply a handler for a specific WM_NOTIFY message or a range of WM_NOTIFY messages, your handler will be called only if the child control sending those messages does not have a reflected message handler through ON_NOTIFY_REFLECT(). If you use ON_NOTIFY_REFLECT_EX() in your message map, your message handler may or may not allow the parent window to handle the message. If the handler returns TRUE, the message will be handled by the parent as well, while a call that returns FALSE does not allow the parent to handle it. Note that the reflected message is handled before the notification message.

當一個 WM_NOTIFY 消息被發送後,控件就得到了第一次的機會來處理它。如果任何其他的被反射消息被發送,父視窗會有第一個機會來處理之,而控件将能接收被反射的消息。為了達到這樣的目的,在控件類消息映射中需要一個處理函數和一個合适的入口。

When a WM_NOTIFY message is sent, the control is offered the first chance to handle it. If any other reflected message is sent, the parent window has the first chance to handle it and the control will receive the reflected message. To do so, it will need a handler function and an appropriate entry in the control's class message map.

反射消息的消息映射宏與通常通知的消息映射宏有點微小的差別:它需要在其正常名字中添加 _REFLECT。例如,為了在父視窗中處理 WM_NOTIFY 消息,你可以在父視窗類的消息映射中使用宏 ON_NOTIFY。而在子控件類中處理反射消息,您必須使用 ON_NOTIFY_REFLECT 來響應消息。在一些情形之中,參數也是不一樣的。請注意:ClassWizard(類向導)通常可以為你添加消息入口,并提供了一個帶有正确參數的實作函數的大體架構。

The message-map macro for reflected messages is slightly different than for regular notifications: it has _REFLECT appended to its usual name. For instance, to handle a WM_NOTIFY message in the parent, you use the macro ON_NOTIFY in the parent’s message map. To handle the reflected message in the child control, use the ON_NOTIFY_REFLECT macro in the child control’s message map. In some cases, the parameters are different, as well. Note that ClassWizard can usually add the message-map entries for you and provide skeleton function implementations with correct parameters.

請參閱 TN061: 在新 WM_NOTIFY 消息中的 ON_NOTIFY 與 WM_NOTIFY 資訊

See TN061: ON_NOTIFY and WM_NOTIFY Messages for information on the new WM_NOTIFY message.

反射消息的消息映射入口和處理函數原型

Message-Map Entries and Handler Function Prototypes for Reflected Messages

為了處理被反射過來的控件通知消息,使用列于下表的消息映射宏和函數原型。

To handle a reflected control notification message, use the message-map macros and function prototypes listed in the table below.

ClassWizard 通常能夠為你加入消息映射入口并為您提供一個實作函數的骨架。請參見

《Defining a Message Handler for a Reflected Message in the Visual C++ Programmer's Guide》來獲得有關怎麼為反射消息定義處理函數的資訊。

ClassWizard can usually add these message-map entries for you and provide skeleton function implementations. SeeDefining a Message Handler for a Reflected Message in the Visual C++ Programmer's Guide for information about how to define handlers for reflected messages.

為了把消息名稱轉化為被反射的宏名稱,在消息名稱前加 ON_,然後在消息名稱後添加

_REFLECT。例如,WM_CTLCOLOR 相應的就轉變為 ON_WM_CTLCOLOR_REFLECT。

To convert from the message name to the reflected macro name, prepend ON_ and append _REFLECT. For example, WM_CTLCOLOR becomes ON_WM_CTLCOLOR_REFLECT. (To see which messages can be reflected, do the opposite conversion on the macro entries in the table below.)

上述規則[普遍通用,但是要注意有三個例外:

The three exceptions to the rule above are as follows:

用于 WM_COMMAND 通知的宏是 ON_CONTROL_REFLECT;

用于 WM_NOTIFY 通知的宏是 ON_NOTIFY_REFLECT;

用于 ON_UPDATE_COMMAND_UI 通知的宏是 ON_UPDATE_COMMAND_UI_REFLECT。

The macro for WM_COMMAND notifications is ON_CONTROL_REFLECT.

The macro for WM_NOTIFY reflections is ON_NOTIFY_REFLECT.

The macro for ON_UPDATE_COMMAND_UI reflections is ON_UPDATE_COMMAND_UI_REFLECT.

在上述特殊情形中,您必須指定處理成員函數的名字。而在其他情形中,您必須使用标準的處理函數名稱。

In each of the above special cases, you must specify the name of the handler member function. I

n the other cases, you must use the standard name for your handler function.

函數參數的意義和傳回值的意思已經羅列于函數名稱之下或已經預先寫好了。例如,CtlColor 在文檔中表示為 OnCtlColor。幾個被反射消息處理函數需要的參數個數要比在其父視窗中相應的函數需要的參數要少。請參見下表中文檔中的正式參數。

The meanings of the parameters and return values of the functions are documented under either the function name or the function name with On prepended. For instance, CtlColor is documented in OnCtlColor. Several reflected message handlers need fewer parameters than the similar handlers in a parent window. Just match the names in the table below with the names of the formal parameters in the documentation.

映射宏    入口    函數原型

ON_CONTROL_REFLECT( wNotifyCode, memberFxn )  afx_msg void memberFxn ( );

ON_NOTIFY_REFLECT( wNotifyCode, memberFxn )  afx_msg void memberFxn ( NMHDR * pNotifyStruct, LRESULT* result );

ON_UPDATE_COMMAND_UI_REFLECT( memberFxn )  afx_msg void memberFxn ( CCmdUI* pCmdUI );

ON_WM_CTLCOLOR_REFLECT( )    afx_msg HBRUSH CtlColor ( CDC* pDC, UINT nCtlColor );

ON_WM_DRAWITEM_REFLECT( )    afx_msg void DrawItem ( LPDRAWITEMSTRUCT lpDrawItemStruct );

ON_WM_MEASUREITEM_REFLECT( )    afx_msg void MeasureItem ( LPMEASUREITEMSTRUCT lpMeasureItemStruct );

ON_WM_DELETEITEM_REFLECT( )    afx_msg void DeleteItem ( LPDELETEITEMSTRUCT lpDeleteItemStruct );

ON_WM_COMPAREITEM_REFLECT( )    afx_msg int CompareItem ( LPCOMPAREITEMSTRUCT lpCompareItemStruct );

ON_WM_CHARTOITEM_REFLECT( )    afx_msg int CharToItem ( UINT nKey, UINT nIndex );

ON_WM_VKEYTOITEM_REFLECT( )    afx_msg int VKeyToItem ( UINT nKey, UINT nIndex );

ON_WM_HSCROLL_REFLECT( )    afx_msg void HScroll ( UINT nSBCode, UINT nPos );

ON_WM_VSCROLL_REFLECT( )    afx_msg void VScroll ( UINT nSBCode, UINT nPos );

ON_WM_PARENTNOTIFY_REFLECT( )    afx_msg void ParentNotify ( UINT message, LPARAM lParam );

ON_NOTIFY_REFLECT 和 ON_CONTROL_REFLECT 有幾個變體,以允許諸如控件及其父視窗多個對象來處理給定的消息。

The ON_NOTIFY_REFLECT and ON_CONTROL_REFLECT macros have variations that allow more than one object (such as the control and its parent) to handle a given message.

映射宏    入口   函數原型

ON_NOTIFY_REFLECT_EX( wNotifyCode, memberFxn )  afx_msg BOOL memberFxn ( NMHDR * pNotifyStruct, LRESULT* result );

ON_CONTROL_REFLECT_EX( wNotifyCode, memberFxn ) afx_msg BOOL memberFxn ( );

處理被反射的消息:可重用控件的例子

Handling Reflected Messages: An Example of a Reusable control

這個簡單的例子建立了一個可重用的控件,叫 CYellowEdit。該控件與一個正常編輯控件的功能幾乎相同,不同就是它在黃色背景中顯示黑字。當然,你可以很容易地添加一些成員函數使之顯示不同的顔色。

This simple example creates a reusable control called CYellowEdit. The control works the same as a regular edit control except that it displays black text on a yellow background. It would be easy to add member functions that would allow the CYellowEdit control to display different colors.

1請參照下列步驟:

To try this example, do the following steps:

在一個已存在的應用程式添加一個對話框,參見《dialog editor in the Visual C++ User’s Guide》。

您必須有一個已存在的應用程式來開發可重用控件,如果您還沒有可使用的應用程式,您不妨使用 AppWizard 來建立一個基于對話框的應用程式。

Create a new dialog box in an existing application. For more information seedialog editor in the Visual C++ User’s Guide.

You must have an application in which to develop the reusable control. If you don’t have an existing application to use, create a dialog-based application using AppWizard.

等 VC++ 把您的應用程式載入之後,您使用 ClassWizard 建立一個基于 CEdit 類的新CYellowEdit類。取消 "Add To Component Gallery" 選項。

With your project loaded into Visual C++, use ClassWizard to create a new class called CYellowEdit based on CEdit. Leave the “Add to Component Gallery” box checked.

在 CYellowEdit 類中添加三個成員變量。前兩個是 COLORREF 的變量以記錄文本和背景顔色。第三個變量是一個用于繪制背景的刷子類 CBrush 變量。刷子變量允許你隻建立一次,接下來隻要引用它即可,一直到 CYellowEdit 控件銷毀的時候刷子才被自動銷毀。

Add three member variables to your CYellowEdit class. The first two will be COLORREF variables to hold the text color and the background color. The third will be a CBrush object which will hold the brush for painting the background. The CBrush object allows you to create the brush once, merely referencing it after that, and to destroy the brush automatically when the CYellowEdit control is destroyed.

在構造函數中初始化這些成員變量:

Initialize the member variables by writing the constructor as follows:

CYellowEdit::CYellowEdit()

{

 m_clrText = RGB( 0, 0, 0 );

 m_clrBkgnd = RGB( 255, 255, 0 );

 m_brBkgnd.CreateSolidBrush( m_clrBkgnd );

}

使用 ClassWizard 給您的 CYellowEdit 類為 WM_CTLCOLOR 反射消息添加一個處理函數。注意,您能處理的消息清單中的消息名稱前的等于号表明該消息是個可以被反射的消息。這在《Defining a Message Handler for a Reflected Message in the Visual C++ Programmer's Guide》中有所描述。ClassWizard 會為您添加下面的消息映射宏以及相應的函數骨架:

Using ClassWizard, add a handler for the reflected WM_CTLCOLOR message to your CYellowEdit class. Note that the equal sign in front of the message name in the list of messages you can handle indicates that the message is reflected. This is described inDefining a Message Handler for a Reflected Message in the Visual C++ Programmer's Guide.

ClassWizard adds the following message-map macro and skeleton function for you:

ON_WM_CTLCOLOR_REFLECT()

// Note: other code will be in between....

HBRUSH CYellowEdit::CtlColor(CDC* pDC, UINT nCtlColor)

{

 // TODO: Change any attributes of the DC here

 // TODO: Return a non-NULL brush if the

 //    parent's handler should not be called

 return NULL;

}

用下面的代碼代替函數的主體。該程式代碼很簡單,就是指定文本顔色、文本背景顔色以及控件其餘部分的背景顔色。

Replace the body of the function with the following code. The code specifies the text color, the text background color, and the background color for rest of the control.

pDC->SetTextColor( m_clrText );    // text

pDC->SetBkColor( m_clrBkgnd );    // text bkgnd

return m_brBkgnd;                // ctl bkgnd

在對話框中建立一個編輯控件,按下一個控件鍵下之後輕按兩下編輯控件并使之與一個成員變量相關聯。在為對話框增添成員變量之際,完成變量名稱,并在“控件”中選擇“CYellowEdit”作為您的變量原型。别忘記在你的對話框中設定 Tab 順序。另外也别忘記在你的對話框頭檔案中添加 CYellowEdit 類的頭檔案。

Create an edit control in your dialog box, then attach it to a member variable by double-clicking the edit control while holding a control key down. In the Add Member Variable dialog box, finish the variable name and choose “Control” for the category, then “CYellowEdit” for the variable type. Don’t forget to set the tab order in the dialog box. Also, be sure to include the header file for the CYellowEdit control in your dialog box’s header file.

編譯并運作您的程式,該編輯控件将顯示一個黃色的背景。

Build and run your application. The edit control will have a yellow background.

現在您可以使用 Component Gallery 把您的 CYellowEdit 控件類添加到其他工程項目中去。

You can now use Component Gallery to add your CYellowEdit control class to other projects.

MFC 技術注意第62條的翻譯

繼續閱讀