Application of "placeholder and dynamic creation" technique in MFC programming

zhaozj2021-02-08  261

Liu Yong Xia Jiabang

In MFC programming, the application of "placeholders and dynamic creation" techniques are very broad, although there is some programming books and articles, but lack of system introduction and necessary summary, inconvenience to applications. This article will introduce this programming skills and have a detailed summary of the application scope and precautions of this technique.

I. Introduction

The MFC provides a set of standard classes for application development in the Windows environment. In most cases, it can meet the needs of application development, but in some specific circumstances, developers need to use some techniques based on MFC. Working, "placeholder and dynamic creation" are one of them.

Dynamically create MFC programming is a more common skill, that is, dynamically create a control, window as needed, but the method is inconvenient, that is, the determination of the control position is very cumbersome. On this basis, "placeholder and dynamic creation" technique, reasonably use Windows display is the characteristics on planes and the editing environment of Microsoft Visual C , which solves this problem. The method first places a "placeholder" with a particular sign in a position that needs to be displayed, "placeholder" can be a button (in the toolbar), in the toolbar), pane (in the status bar) Or the PICTRUE control (in the dialog box), the size can be adjusted by editing the environment or related code as needed; then "dynamically created" the interface element required to place "placeholder".

Using this tip to implement some common but MFCs in Windows applications, such as creating some non-button controls on the toolbar, status bar; create a toolbar, status bar, or a control in the dialog; in the dialog Embed an attribute table or another dialog, and so on. The use of this technique is described in detail below by example.

Second, the example

Attributes are a very useful interface element that brings convenience to us to collect a lot of data, but if you encounter "find", you need to "placeholder and dynamic creation" in the case of embedding attributes in the dialog box. "Skills can meet our requirements. By constructing a routine similar to a Windows Find Application, this technique is used, which is the dialog application in the VC 6.0 environment. After generating code, you can follow the steps below.

1. Set "placeholder"

First place a Picture control in the dialog box, where you need to place a Picture control, remove the properties of a specific marker ID external control, can be used. The method of finding the control generally has two: or using ClassWizard to map the control into the data member of the dialog box class (an instance of a CSTATIC class), this example uses this method; or in the program according to "placeholder "Specific ID, using the getDlgitem () function to obtain control of the control.

Then adjust the size of the Picture control to the corresponding properties page dialog in the dialog editor.

Finally, the following code is added to the OnInitDialog () function of the dialog to obtain the "placeholder" position and make the corresponding coordinate transformation, where m_ctrlholder is an instance of the CSTATIC class that is obtained by the ClassWizard map.

CRECT RECT;

m_ctrlholder.getWindowRect (& RECT);

ScreenToClient (& Re);

2. Generate attributes and property page code

Generate the code to be created as needed, this step can be performed.

3. "Dynamic creation" and data exchange of the property sheet

After completing the above preparation, you can create a property table. First, add a public data member in the dialog class, an instance or pointer to the property sheet, and is used to use the operation of the attribute table such as dynamic creation and data exchange.

Then, the code of step 1 in the OnInitDialog function is then added to the "dynamic creation" code, that is, the PICTRUE control is "dynamically created" attribute table to override the PICTRUE control, thereby implementing the purpose of embedding attributes in the dialog. Dynamically Creating Properties This is called the create () function, but must specify the ws_child, ws_visible property, and the rest of the property will be added again as needed. In addition, when embedding attribute tables in the dialog box, you must specify the WS_TABSTOP and WS_EX_CONTROLPARENT attributes, so that the Tab key can smoothly transfer the focus. Finally, you need to perform data exchange on the relevant controls of the property sheet, which can be done by the property table data member, the data member of the property page, and the Updatedata () function. The relevant code is as follows:

// In the pile generation dialog, the property meter data member m_ppropsheet and "Dynamic Create" Property table "control";

m_ppropsheet = new cqrypropsheet (this);

IF (! m_ppropsheet -> Create (this, ws_child | ws_visible, 0)) {

Delete M_Ppropsheet;

m_ppropsheet = NULL;

Return -1;

}

// Initialize the attribute table, this example is initialized to the "Introducing Subfolder" checkbox;

M_ppropsheet -> m_page1.m_bchildfolder = true;

m_ppropsheet -> m_page1.Updatedata (false);

// Modify the property table property to pass the TAB key to the focus, and the attribute table can accept the focus;

&

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

New Post(0)