WTL for MFC Programmers (5)

zhaozj2021-02-08  276

Defining a Window Implementation

Define a form implementation

Any Non-Dialog Window You Create Will Derive from CWindowImpl. Your New Class Needs To Contain Things:

The form of any non-dialog boxes will be derived from CWindowImpl, you need to do the following 3 things for your new class:

1. a window class definition

2. a Message Map

3. The default styles to use for the window, caled the window traits

1. Definition of a form class

2. A message mapping

3. Calling the form characteristics as the default style.

The window class definition is done using the DECLARE_WND_CLASS or DECLARE_WND_CLASS_EX macro. Both of these define an ATL struct CWndClassInfo that wraps the WNDCLASSEX struct. DECLARE_WND_CLASS lets you specify the new window class name and uses default values ​​for the other members, while DECLARE_WND_CLASS_EX lets you also Specify a class style and window background color. You can also use null for the class name, And atl will generate a name for you.

The definition of the form class requires use of declare_wnd_class or declare_wnd_class_ex macros. These two macros will define an ATL structure CWndClassInfo encapsulated WNDCLASSEX structure. Declare_wnd_class allows you to provide a new form name and assign a default value for other form parameters, and Declare_Wnd_Class_ex allows you to provide a category style and form background color. You can set the class name, ATL will automatically generate a class name for you.

Let's Start Out A New Class Definition, And I'll Keep Adding to It As We Go Through this Section.

Let's start a new class definition, I will continue to join new content in this chapter.

Class CMYWINDOW: PUBLIC CWINDOWIMPL

{

PUBLIC:

Declare_wnd_class (_T ("My Window Class")))

}

. Next comes the message map ATL message maps are much simpler than MFC maps An ATL map expands into a big switch statement;. The switch looks for the right handler and calls the corresponding function The macros for the message map are BEGIN_MSG_MAP and END_MSG_MAP.. Let's address map. The message mapping is started below. ATL's message mapping is simpler than MFC. It is like a large Switch statement, find the correct handle via Switch and performs the corresponding function. Message mapping macro is begin_msg_map and end_msg_map. Let's add an empty message to our form.

Class CMYWINDOW: PUBLIC CWINDOWIMPL

{

PUBLIC:

Declare_wnd_class (_T ("My Window Class")))

Begin_MSG_MAP (CMYWINDOW)

END_MSG_MAP ()

}

I'll cover how to add handlers to the map in the next section. Finally, we need to define the window traits for our class. Window traits are a combination of window styles and extended window styles that are used when creating the window. The Styles Are Specified As Template Parameters So The Caller Doesn't Have To Be Bothere Desten Getting The Styles Right When It Creates Our WINDOW. Here's A Sample Traits Definition Using The ATL CLASS CWINTRAITS:

I will tell how to add a handle to the mapping in the next section. Finally, we need to define form characteristics for our class. Form characteristics are the form of forms and extended forms of forms that are used when generating a form. It is available like template parameters so that the calling program does not have to worry about how to get the correct style when generating a form. Below is a simple example of using the ATL class CWINTraits definition feature:

Typedef Cwintraits

WS_EX_APPWINDOW> CMYWINDOWTRAITS;

Class CMYWINDOW: Public CWindowImpl

{

PUBLIC:

Declare_wnd_class (_T ("My Window Class")))

Begin_MSG_MAP (CMYWINDOW)

END_MSG_MAP ()

}

The caller can override the styles in the CMyWindowTraits definition, but generally this is not necessary ATL also has a few predefined CWinTraits specializations, one of which is perfect for top-level windows like ours, CFrameWinTraits:. Caller can weight when CMyWindowTraits defined Deload style, but this is not necessary. ATL also provides some special forms of predefined Cwintraits, such as cframewintraits used for top forms:

Typedef Cwintraits

WS_CLIPSIBLINGS,

WS_EX_APPWINDOW | WS_EX_WINDOWEDGE>

CframewinTraits;

转载请注明原文地址:https://www.9cbs.com/read-1201.html

New Post(0)