MFC Programmer WTL Guide (5) dialog box and control

Source: Internet
Author: User
Tags resource wrapper

Introduction to the fourth chapter

MFC's dialog boxes and the encapsulation of controls can really save you a lot of time and effort. Without MFC's encapsulation of the control, you have to have the patience to fill in various structures and write a lot of SendMessage calls to manipulate the controls. MFC also provides dialog data interchange (DDX), which can transfer data between controls and variables. WTL Of course also provides these features and makes a lot of improvements to the encapsulation of the control. This article will look at a dialog based program to demonstrate the functionality you previously implemented with MFC, in addition to the WTL message processing enhancements. The fifth chapter will introduce advanced interface features and WTL to the new controls.

Review the ATL dialog box

Now look back at the two dialog boxes, CDialogImpl and CAxDialogImpl, mentioned in the first chapter. CAxDialogImpl for the dialog box that contains the ActiveX control. This article is not intended to introduce ActiveX controls, so use only CDialogImpl.

There are three things you need to do to create a dialog box:

    1. Create a dialog box resource
    2. Derive a new class from the CDialogImpl class
    3. Add a public member variable, IDD, and set it to the ID of the dialog box resource.

Then you add a message handler function like the main frame window, and WTL doesn't change it, but it does add some other features that you can use in the dialog box.

wrapper classes for common controls

WTL There are many controls that encapsulate classes that you should be familiar with, because they use the same (or almost identical) names as MFC. The method of the control is also named as MFC, so you can use these WTL encapsulation classes in reference to MFC's documentation. The disadvantage is that the F12 key cannot easily be skipped to the definition code of the class.

The following is the encapsulated class of Windows built-in controls:

    • User controls: CStatic, CButton, CListBox, CComboBox, CEdit, CScrollBar, CDragListBox
    • Generic controls: CImageList, Clistviewctrl (CListCtrl in MFC), Ctreeviewctrl (CTreeCtrl in MFC), CHeaderCtrl, CToolBarCtrl, CSTATUSBA Rctrl, CTabCtrl, CToolTipCtrl, Ctrackbarctrl (CSliderCtrl in MFC), Cupdownctrl (CSpinButtonCtrl in MFC), CProgressBarCtrl , CHotKeyCtrl, CAnimateCtrl, CRichEditCtrl, CReBarCtrl, CComboBoxEx, Cdatetimepickerctrl, CMonthCalendarCtrl, CIPAddressCtrl
    • Encapsulation classes not in MFC: CPagerCtrl, Cflatscrollbar, CLinkCtrl (clickable hyperlink, available on XP only)

There are some WTL-specific classes: CBitmapButton, Cchecklistviewctrl (List control with check box), Ctreeviewctrlex and Ctreeitem (usually used together, Ctreeitem Encapsulates Htreeitem), Chyperlink (similar to Hyperlink objects on a Web page, supports all operating systems)

It is important to note that most encapsulation classes are based on the CWindow interface, and like CWindow, they encapsulate the HWND and encapsulate the control's messages (for example, Clistbox::getcursel () encapsulates the Lb_getcursel message). So, like CWindow, creating a wrapper object for a control and associating it with a control that already exists takes up only a very small amount of resources, and of course, like CWindow, the control encapsulates the object without destroying the control itself. There are also some exceptions, such as CBitmapButton, Cchecklistviewctrl and Chyperlink.

Since these articles are positioned for experienced MFC programmers, I don't waste time introducing these encapsulated classes, which are similar to the corresponding control wrappers in MFC. Of course I will introduce the new class of WTL: The Cbitmapbuttoncbitmapbutton class differs greatly from the MFC class with the same name, and Chyperlink is entirely new.

To generate a program based on a dialog box with the Application wizard

Run VC and start the WTL Application Wizard, I believe you have used it in the clock program, for our new program named ControlMania1. On the first page of the wizard, select the application based on the dialog box, and also choose whether to use the modal dialog box or the modeless dialog box. They are very different, I will introduce them in the fifth chapter, now we choose a simple one: modal dialog box. Select the modal dialog box and the Generate CPP file option as follows:

All the options on the second page are meaningful only when the main window is a frame window, now they are unavailable, click Finish, and then click OK to complete the wizard.

As you would expect, the wizard-generated code based on the dialog box program is very simple. The _tWinMain () function is in ControlMania1.cpp, and the following is an important part:

int WINAPI _tWinMain (
   HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/,
   LPTSTR lpstrCmdLine, int nCmdShow )
{
   HRESULT hRes = ::CoInitialize(NULL);
   AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);
   hRes = _Module.Init(NULL, hInstance);
   int nRet = 0;
   // BLOCK: Run application
   {
     CMainDlg dlgMain;
     nRet = dlgMain.DoModal();
   }
   _Module.Term();
   ::CoUninitialize();
   return nRet;
}

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.