MFC window segmentation and communication between windows

Source: Internet
Author: User

An accidental opportunity re-touched the separation of the MFC window, and re-wrote a window Separation program based on the data. Now I will share the specific process with you:

1. we will first create an MFC single-document program. The specific splitting method first splits the entire customer of a single document into two rows and one column, first, we define a csplitterwnd Class Object m_wndsplitter in the cmainframe class of MFC. In the main window class, we can call all the member functions of the csplitterwnd class through the m_wndsplitter object, to split the window into two rows and one column, we reload the oncreateclient function in the cmainframe class and write the code in the oncreateclient function. Now we split the window:

Crect rect;
Getclientrect (& rect); // generates the first static split.
M_wndsplitter.createstatic (this, // parent window pointer
2, 1); // number of rows and columns

Split the main window into two rows and one column

Then, the split window is associated with a specific class.

M_wndsplitter.createview (0, 0, // The ordinal number of the row and column in the pane
Runtime_class (cfengeview), // View class

Csize (rect. Width (), rect. Height ()-rect. Height ()/5), pcontext); // create a parameter for the parent window

M_wndsplitter.createview (1, 0, runtime_class (cliftdown ),
Csize (rect. Width (), rect. Height ()/5), pcontext); // class associated with the split window
// The oncreateclient function of the base class is not called.
Return true;

The split two windows are separated here. One is associated with the basic view class, and the other is used for displaying some information. We create a new class. The basic class of this new class is ceditview, after completion, note that the header file must be associated. Otherwise, the cmainframe does not recognize the newly created class. If the following error is reported:

Error c2143: syntax error: Missing '; 'before '*'
Error c2501: 'cfengedoc ': Missing storage-class or type specifiers
Error c2501: 'getdocument': Missing storage-class or type specifiers

We can add # include "fengedoc. H" at the beginning of the header file of the class and run it again.

 

 

 

OK. Next we will split the first row into two columns Based on this. The first column of the First row requires that the input function is mainly for information input, as well as button addition, the second column of the First row needs to be displayed in the form of a list

We add the entered information. To complete the preceding functions, we first add a dialog box resource and associate the resource with a new class. The basic class of the new class is cformview, at the same time, in order to display the input confidence in the form of a class table, we also add a new class. The basic class of this class is clistview, after adding a class, we need to split it again on the basis of the first split. The process of this split is the same as that of the first split, but note the following differences: 1. we need to add a csplitterwnd class to the base class, such as m_wndsplitter1, 2. add the code to the oncreate function directly in the base object class. The specific code is as follows:

 

 

Crect rect;
Getclientrect (& rect); // obtain the window creation Information pointer
Ccreatecontext * pcontext = (ccreatecontext *) lpcreatestruct-> lpcreateparams;
M_wndsplitter1.createstatic (this,); // generates the second static split.
// Generate a view for the first pane
M_wndsplitter1.createview (0, 0, // The ordinal number of rows and columns in the window
Runtime_class (cliftup), // View class
Csize (rect. Width ()/4, rect. Height ()),//
Pcontext );
// Generate a view for the second pane
M_wndsplitter1.createview (0, 1, runtime_class (crightup), csize (1, 1), pcontext );
Return 0;

At the same time, add the onsize function to the main view class and add code to the function:

Cview: onsize (ntype, CX, CY );
Crect rect;
Getclientrect (& rect );
Int x = rect. Width ();
Int y = rect. Height ();
M_wndsplitter1.movewindow (-2,-2, X, Y + 3 );
M_wndsplitter1.setcolumninfo (0, x/4, 0); // left pane position
M_wndsplitter1.setcolumninfo (1, x-X/4, 0); // The Position of the right pane
M_wndsplitter1.recalclayout ();

In addition to the header file identification, note that when the program reports that the clistview class cannot be found, add # include <afxcview. h> to the header file of the newly created crightup class. Add a message response function to the buttons in the dialog box. The message response function is in the cliftup class associated with the dialog box. To display the information entered in the dialog box, create an array to store the information. Here we create a cstringarray-type number infoarray [4] In the cfengedoc class. define a bool type variable Add at the same time. Be sure to initialize add in the constructor. Otherwise, an error will be reported. Next, associate four cstring type variables with the four edit boxes in the dialog box. M_num, m_name, m_zhuanye, m_home.

Next, we need to associate several view areas. First, enter the code in the response function of the cliftup class:

Updatedata (true );

If (m_num.isempty () | m_name.isempty ())

{

Afxmessagebox ("Enter cannot be blank ");

Return 1;

}

Cfengedoc * pdoc = (cfengedoc *) getdocument ();

Pdoc-> infoarray [0]. insertat (0, m_num );

Pdoc-> infoarray [1]. insertat (0, m_name );

Pdoc-> infoarray [2]. insertat (0, m_zhuanye );

Pdoc-> infoarray [3]. insetat (0, m_home );

Pdoc-> Add = true; // added data
Pdoc-> updateallviews (null); // update all views
M_num = _ T ("");
M_name = _ T ("");
M_zhuanye = _ T ("");
M_home = _ T ("");
Updatedata (false); // clear the content of each control

Next, reload the oninitialupdate () function in the cliftdown class and add the code to the function:

Cedit & medit = geteditctrl (); // gets the control of the editing view.
Medit. setwindowtext ("waiting for the user to enter student information! "); // Set the display information
Medit. enablewindow (false); // The editing control cannot be edited.

Reload the onupdate function of another function in the cliftdown class and add the code to the function:

Cfengedoc * pdoc = (cfengedoc *) getdocument (); // get the document pointer
If (pdoc-> Add) // added data
{
Cstring STR;
STR = "student information with the student ID" + pdoc-> infoarray [0]. getat (0) + "has been added successfully! ";
Cedit & medit = geteditctrl (); // gets the edit View Control
Medit. setwindowtext (STR); // display information

}

 

Then, reload the oninitialupdate () function in the crightup class and add the code to the function:

Cstring m_columnlabelstr [] = {"student ID", "name", "Major", "nationality "};
// Header field
Clistctrl & listctrl = getlistctrl (); // gets the list control
DWORD dwstyle = listctrl. getextendedstyle ();
Dwstyle | = lvs_ex_fullrowselect;
// Select an exercise to highlight the entire line (only applicable to listctrl in the report style)
Dwstyle | = lvs_ex_gridlines;
Dwstyle | = lvs_ex_underlinehot;
Listctrl. setextendedstyle (dwstyle); // list Style
Int width [6] = {80, 80, 110,150 };
For (INT I = 0; I <4; I ++)
{
Listctrl. insertcolumn (I, m_columnlabelstr [I], lvcfmt_left, width [I]);

}

 

In the crightup class, reload the function precreatewindow and add the Code:

CS. Style = cs. Style | lvs_report; // set it to display the report list.

At the same time, reload the onupdate function in the crightup class and add the code to the function:


Cfengedoc * pdoc = (cfengedoc *) getdocument (); // You must obtain the document class pointer to obtain the document class pointer and use the document class as a bridge.
If (pdoc-> Add) // added data
{
Clistctrl & listctrl = getlistctrl (); // gets the list control
Listctrl. deleteallitems (); // delete all items
For (INT I = 0; I <pdoc-> infoarray [0]. getsize (); I ++) // insert data into the list box
{Listctrl. insertitem (I, pdoc-> infoarray [0]. getat (I ));
Listctrl. setitemtext (I, 1, pdoc-> infoarray [1]. getat (I ));
Listctrl. setitemtext (I, 2, pdoc-> infoarray [2]. getat (I ));
Listctrl. setitemtext (I, 3, pdoc-> infoarray [3]. getat (I ));
}

}

OK. Run the following command:

MFC window segmentation and communication between windows

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.