In Visual C ++, the dialog box template is not used to create a dialog box.

Source: Internet
Author: User

Introduction

Generally, VC is used as the applicationProgramTo create a dialog box, you must use the template resource of the dialog box. You can create dialog box resources visually by using the dialog editor in the VC ++ integrated development environment. This method can be applied to most dialog box programming scenarios. The program modules involved in the dialog box must depend on the development project resources and cannot be used as the public program modules shared by multiple development projects. For example, a series of applications developed by the author require a user to input registration information. Generally, the utility module can be introduced into different development projects, while the actual program file only has a unique backup. This facilitates program maintenance and avoids multiple inconsistent copies of the public module. However, if there is a user interface in the public program module, different development projects cannot be introduced simply (because different projects have their own resources, and their resource definitions may not conflict ). In addition, if a large number of dialogs are required during the development process, the content of these dialogs is similar, but slightly different. It is also cumbersome to use the dialog template resources. This article describes how to dynamically create a dialog box without using a regular dialog box resource template in Visual C ++.

1. Basic Principles

This article describes how to create a dialog box application without using the dialog box template resources. If you carefully read the cdialog related content in msdn, you will find that
Cdialogcreateindriect and
Cdialoginitmodalindirect two member functions. These two member functions can use the dialog box Resource Creation dialog box in the memory.
Cdialogcreateindriect is used to create a dialog box without mode,
Cdialoginitmodalindirect can create a mode dialog box.

Both member functions contain parameters.
The lpcdlgtemplatelpdialogtemplate points to the memory location that contains the dialog box and control resources. The memory of this section consists of two parts:

1.1 dialog box Resources

It starts with a dlgtemplate structure, indicating the position, size, and style of the dialog box window. Next to it, it is an array pointing to the menu resource (the first two bytes are 0x0000, indicating that there is no menu ),
The following Unicode String Array represents the registration class name corresponding to the control (the first two bytes are 0x0000, indicating that the default pre-defined class is used), and then the dialog box title represented by the Unicode string array, double-byte font length and font name.

1.2 dialog box control resources

One or more dialog box control resources follow the dialog box resources. Each control resource starts with a dlgitemtemplate structure, indicating the position, size, and style of the control window, and then the registration class name corresponding to the control, (If the first two bytes are 0xffff, the pre-defined class is used.) At this time, the last two bytes indicate the specific classes as shown in Table 1, then the control title (if the first two bytes are 0xffff, the icon resource will be used). The following data is used to create the control. When the system sends the wm_create message, it is referenced by the lparam parameter, you can use 0x0000 to omit this part of data.

 

Table1

 

 

 

 

Zero X 0080

Button

Zero X 0081

Edit

Zero X 0082

Static

Zero X 0083

List box

Zero X 0084

Scroll bar

Zero X 0085

Combo box

 

 

 

 

 

 

 

Table 1

The dlgtemplate structure is

Typedefstruct // dLTT

Dwordstyle

Dworddwextendedstyle

WordCdit

Shortx

Shorty

Shortcx

Shortcy

Dlgtemplate

Style and dwextendedstyle are the style of the dialog box. cdit indicates the number of controls contained in the dialog box. X, Y, CX, and Cy indicate the window size. The dlgitemtemplate structure is similar to this one. Only one ID is added.

Based on the description above, you can know that you only need to apply for memory space, define the dialog box and control resources in the memory as required, and no longer rely on project resources. In actual programming, it is easier to define and create controls in the dialog box class. Therefore, you can define only the memory dialog box resources and omit the control resource part. You can dynamically create the control by using conventional methods.

2. Implementation methods and steps

The following describes the implementation steps in conjunction with an instance of the Progress display dialog box:

First, a cmemorydialog class is derived from cdialog, which serves as the base class for all non-template dialogs. Choose Insert> newclass to bring up the new class dialog box. In the Name field, type cmemorydialog and select the baseclass option.
Cdialog: click OK. Classwizard prompts the user to create a dialog box template resource first. Ignore this and continue to create. In.
In the H file, locate Enum IDD = _ unknown_resource_id _, indicating that the unknown resource ID willCodeDelete.

Define member variables:

Cstringm_strtitle // dialog box title

Dlgtemplatem_dlgtempl // memory dialog box template

Define member functions:

Intdomodalmemorydialog // create mode dialog box

Voidcreatememorydialog // used to create a dialog box without Mode

The domodalmemorydialog code list is as follows:

Intcmemorydialogdomodalmemorydialog

Intr

Wchar szfontname = L″Arial″// Font name

Wchar pszboxcaption // Title

Int nlentitle = m_strtitle.getlength + 1

Pszboxcaption = new wcharnlentitle

// Convert the title to a unicode string

MultiByteToWideCharCP_ACP0m_StrTitle-1 pszboxcaptionnlentitle

Try

// Allocate memory for storing Resources

Intnbuffersize =
Sizeofdlgtemplate + 2 sizeofword/menu and class/+ nlentitlesizeofwchar

Nbuffersize + = sizeofword + sizeofszfontname/fontinformation/+

Nbuffersize = nbuffersize + 2 & 2 // adjustsize to make first control DWORD aligned

Hlocalhlocal

Hlocal = localalloclhnd nbuffersize

If hlocal = NULL

Afxthrowmemoryexception

Byte pbuffer = bytelocallockhlocal

If pbuffer = NULL

Localfreehlocal

Afxthrowmemoryexception

Byte pdest = pbuffer

// Copy the dlgtemplate Structure

Memcpypdest & m_dlgtempl sizeofdlgtemplate

Pdest + = sizeofdlgtemplate

Wordpdest = 0 // indicates no menu

Wordpdest + 1 = 0 // use the default class

Pdest + = 2 sizeofword

// Copy the title string

Memcpypdestpszboxcaptionnlentitlesizeofwchar

Pdest + = nlentitlesizeofwchar

Wordpdest = 11 // length of the font name

Pdest + = sizeofword

// Copy the character string

Memcpypdestszfontnamesizeofszfontname

Pdest + = sizeofszfontname

Delete pszboxcaption

// Create mode dialog box

Initmodalindirectdlgtemplatepbuffer

R = domodal

Localunlockhlocal

Localfreehlocal

Catchcmemorygatetione

Afxmessagebox "memory allocation failed ″

End_catch

Return R

Creatememorydialog is similar to this.

Initmodalindirectdlgtemplatepbuffer

R = domodal

Replace the two rows

Createindirectdlgtemplatepbuffer

Showwindowsw_show

And remove the return value.

Then, the new class ccustomprogressdialog is derived from cmemorydialog again. Add the member cprogressctrlm_proctrl to the. h file and overwrite the oninitdialog member function of the base class. Dynamically create a cprogressctrl object in oninitdialog.

A simple progress display dialog box is created. You can create more dialog box classes as needed, or add more control objects at any time.

3. Conclusion

Through the implementation steps of the above progress display dialog box, you can see the implementation method of the create no template resource dialog box. You can derive more dialog box classes as needed, or add more control objects at any time. These modules are not related to resources and can be included by multiple development projects at the same time, which is easy to maintain.

 

 

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.