WTL for MFC Programmers(5)

Defining a Window Implementation

定义一个窗体实现

Any non-dialog window you create will derive from CWindowImpl. Your new class needs to contain three things:

你所产生的任何非对话框的窗体都将从CWindowImpl派生,你需要为你的新类做下面的3件事:

1.                              A window class definition

2.                              A message map

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

1.              一个窗体类的定义

2.              一个消息映射

3.              调用窗体特征为窗体使用缺省的风格。

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.

窗体类的定义需要使用DECLARE_WND_CLASSDECLARE_WND_CLASS_EX宏。这两个宏都将定义一个封装了WNDCLASSEX结构的ATL结构CWndClassInfoDECLARE_WND_CLASS允许你提供一个新的窗体名并对其他的窗体参数赋缺省值,DECLARE_WND_CLASS_EX允许你提供一个类的风格和窗体的背景色。你可以将类名赋空值,ATL将自动为你生成一个类名。

Let's start out a new class definition, and I'll keep adding to it as we go through this section.

让我们开始一个新的类定义,我将在本章中不断地为它加入新的内容。

class CMyWindow : public CWindowImpl<CMyWindow>

{

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 add an empty map to our window.

下面开始消息映射。ATL的消息映射比MFC简单的多。它就像一个大的switch语句,通过switch找到正确的句柄并执行相应的函数。消息映射宏是BEGIN_MSG_MAPEND_MSG_MAP。下面让我们添加一个空的消息映射到我们的窗体。

class CMyWindow : public CWindowImpl<CMyWindow>

{

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 bothered with getting the styles right when it creates our window. Here's a sample traits definition using the ATL class CWinTraits:

我将在下一节讲述如何添加一个句柄到映射中。最后,我们需要为我们的类定义窗体特征。窗体特征是产生窗体时使用的窗体风格和扩展窗体风格的合并。它像模板参数一样提供,使得调用程序不必再为产生窗体时如何取到正确的风格而烦恼。下面是一个使用ATLCWinTraits定义特征的简单例子:

typedef CWinTraits<WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,

                   WS_EX_APPWINDOW> CMyWindowTraits;

 

class CMyWindow : public CWindowImpl<CMyWindow, CWindow, CMyWindowTraits>

{

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:

调用程序能够在CMyWindowTraits定义时重载风格,但这并不是很必要的。ATL也提供一些预定义的CWinTraits的特殊形式,如为顶级窗体使用的CFrameWinTraits:

typedef CWinTraits<WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN |

                     WS_CLIPSIBLINGS,

                   WS_EX_APPWINDOW | WS_EX_WINDOWEDGE>

        CFrameWinTraits;

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WTL 具有两面性,确实是这样的。它没有MFC的界面(GUI)类库那样功能强大,但是能够生成很小的可执行文件。如果你象我一样使用MFC进行界面编程,你会觉得MFC提供的界面控件封装使用起来非常舒服,更不用说MFC内置的消息处理机制。当然,如果你也象我一样不希望自己的程序仅仅因为使用了MFC的框架就增加几百K的大小的话,WTL就是你的选择。当然,我们还要克服一些障碍: ATL样式的模板类初看起来有点怪异 没有类向导的支持,所以要手工处理所有的消息映射。 MSDN没有正式的文档支持,你需要到处去收集有关的文档,甚至是查看WTL的源代码。 买不到参考书籍 没有微软的官方支持 ATL/WTL的窗口与MFC的窗口有很大的不同,你所了解的有关MFC的知识并不全部适用与WTL。 从另一方面讲,WTL也有它自身的优势: 不需要学习或掌握复杂的文档/视图框架。 具有MFC的基本的界面特色,比如DDX/DDV和命令状态的自动更新功能(译者加:比如菜单的Check标记和Enable标记)。 增强了一些MFC的特性(比如更加易用的分隔窗口)。 可生成比静态链接的MFC程序更小的可执行文件(译者加:WTL的所有源代码都是静态链接到你的程序中的)。 你可以修正自己使用的WTL中的错误(BUG)而不会影响其他的应用程序(相比之下,如果你修正了有BUG的MFC/CRT动态库就可能会引起其它应用程序的崩溃。 如果你仍然需要使用MFCMFC的窗口和ATL/WTL的窗口可以“和平共处”。(例如我工作中的一个原型就使用了了MFC的CFrameWnd,并在其内包含了WTL的CSplitterWindow,在CSplitterWindow中又使用了MFC的CDialogs -- 我并不是为了炫耀什么,只是修改了MFC的代码使之能够使用WTL的分割窗口,它比MFC的分割窗口好的多)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snaill

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值