MFC Programmer WTL Guide (3) WTL interface base class

Source: Internet
Author: User

Introduction to the second part

Okay, now we're officially starting to introduce wtl!. In this section I am talking about generating a basic main window and some friendly improvements provided by WTL, such as updating the UI interface (such as the selection tag on the menu) and better message mapping mechanisms. To better understand the content of this chapter, you should install WTL and add the header file directory of the WTL library to the VC search directory, and copy the WTL Application Generation Wizard to the correct location. The WTL release version contains documentation that describes how to do these settings, and you can view them if you encounter difficulties.

WTL Overall Impression

The WTL classes can be roughly divided into several types:

    1. Implementation of the main frame window- CFrameWindowImpl, CMDIFrameWindowImpl
    2. Encapsulation of the control-CButton, Clistviewctrl
    3. Encapsulation of GDI objects-CDC, CMenu
    4. Some special interface features-Csplitterwindow, Cupdateui, Cdialogresize, Ccustomdraw
    5. Practical tool classes and Macros-CString, CRect, BEGIN_MSG_MAP_EX

This article will delve into the Framework window classes, and will briefly describe the interface feature classes and tool classes, most of which are separate classes, although some are embedded classes, such as Cdialogresize.

Start Writing WTL Program

If you don't have the WTL Application Generation Wizard (which I'll explain later in this wizard), the code structure of the WTL program is much like an ATL program, and the example code used in this chapter is different from the example in the first chapter, mainly to show the features of WTL, and there is no practical value.

In this section we will add code based on WTL generated code to generate a new program where the client area of the main window of the program displays the current time. The code for StdAfx.h is as follows:

#define STRICT
#define WIN32_LEAN_AND_MEAN
#define _WTL_USE_CSTRING
#include <atlbase.h>    // 基本的ATL类
#include <atlapp.h>    // 基本的WTL类
extern CAppModule _Module; // WTL 派生的CComModule版本
#include <atlwin.h>    // ATL 窗口类
#include <atlframe.h>   // WTL 主框架窗口类
#include <atlmisc.h>    // WTL 实用工具类,例如:CString
#include <atlcrack.h>   // WTL 增强的消息宏

Atlapp.h is the first header file in your project that defines the classes and cappmodule,cappmodule that are derived from CComModule for message handling. If you are going to use the CString class, you need to manually define the _wtl_use_cstring label, because the CString class is defined in Atlmisc.h, and many header files that are contained before atlmisc.h use CString, which defines _wtl_ After use_cstring, Atlapp.h will declare the CString class, and the other headers will know the existence of the CString class, thus avoiding the compiler's fuss.

Next, define the frame window. Our SDI window is derived from Cframewindowimpl and uses Declare_frame_wnd_class instead of the declare_wnd_class used in the definition of the window class. The first part of the window definition in MyWindow.h is as follows:

class CMyWindow : public CFrameWindowImpl<CMyWindow>
{
public:
   DECLARE_FRAME_WND_CLASS(_T("First WTL window"), IDR_MAINFRAME);
   BEGIN_MSG_MAP(CMyWindow)
     CHAIN_MSG_MAP(CFrameWindowImpl<CMyWindow>)
   END_MSG_MAP()
};

Declare_frame_wnd_class has two parameters, window class name (class name can be Null,atl will generate a class name for you) and resource ID, create a window WTL with this ID load icon, menu and accelerator table. We also want to link messages into the window like message processing in Cframewindowimpl (such as wm_size and Wm_destroy messages).

Now look at the WinMain () function, which is almost the same as the WinMain () function in the example code in the first section, except that the code to create the window section is slightly different.

// main.cpp:
#include "stdafx.h"
#include "MyWindow.h"
CAppModule _Module;
int APIENTRY WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
            LPSTR lpCmdLine, int nCmdShow )
{
   _Module.Init ( NULL, hInstance );
CMyWindow wndMain;
MSG msg;
   // Create the main window
   if ( NULL == wndMain.CreateEx() )
     return 1;    // Window creation failed
   // Show the window
   wndMain.ShowWindow ( nCmdShow );
   wndMain.UpdateWindow();
   // Standard Win32 message loop
   while ( GetMessage ( &msg, NULL, 0, 0 ) > 0 )
     {
     TranslateMessage ( &msg );
     DispatchMessage ( &msg );
     }
   _Module.Term();
   return msg.wParam;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.