Implement window separation in the Custom splitter window dialog box

Source: Internet
Author: User
 

Custom splitter Window Using MFC class csplitterwnd

Custom splitter Window Using MFC class csplitterwnd

this article shows a demonstration of how to create an explorer interface on a dialog using the MFC class csplitterwnd . an Explorer interface is two controls? master and slave. the master? S content is not changed but the slave? S is. In this particle project the master is a list box containing several? Folders? And when you select one of the data in the slave (another list box) is changed.

The splitter window can onlySplitTo two views (CviewInstances), and can only be placed inside a frame (CframewndInstance). That? S why we must create a frame on the dialog, create two views and place the controls inside the views, then add the views to the splitter window and add the splitter window to the frame.

 

 

If we create an ordinary frame on a dialog it will be sunken and we don? T want that because it doesn? T look good. That? S why we created a cframewnd derived class called cflatframewnd which? S only job is to look flat. To do this we remove its ws_ex_clientedge style before it? S created. It is a very simple class that is declared and used only in the. cpp file of dialog.

The views that contain the controls cannot be ordinary views (just cview), because they need to do some specific actions. That? S why we created the cholderview class that does these things and holds the controls.

These actions are:

    • receive the control it holds as a parameter because when it is sized it needs to resize the control as well. that? S why it has a handler for the wm_size message.
    • dispatch all commands that it records es from its child windows to the parent dialog to handle. that? S why we have overridden the oncommand virtual function.
    • receive the parent dialog as a parameter to know where to dispatch the commands.

So after creating these controls we need to create the helper classes we need to add all of them on the dialog. to do this we must add a function to the dialog. we called it createviews. it creates the frame and views, combines them and sets them on the dialog. the controls which will be split must be created there dynamically as their parent must be the View class. we suggest you use the defined C Ontrol IDs in the holderview. h file idc_main? For the master control and idc_slave for the slave control. This however is not obligatory it is just recommended.

Here is what you need to do in order to use this code:

  1. Add the holder ViewFilesTo your project
  2. In the dialog class. cpp File Include the holder view Header
  3. In the beginning of the dialog class. h file add this code? Class cflatframewnd ;?
  4. Add a member variable of Type cflatframewnd like this? Cflatframewnd m_pframe? And member variables for the controls you? D like to split.
  5. On the dialog template add an invisible control with ID idc_vtemplate. It willServeOnly to get its rectangle and so the control structure you? Ll add will be there. Add the function createviews from cviews. cpp to you dialog class
  6. Fill the function with the appropriate code as shown with the comments.
  7. In the initdalog method call createviews and then initialize the controls (if they needExtraInitialization after they? Re created)
So, how to write the createviews function?

First to create a view on a splitter window we need a create context which is described with the ccratecontext class. in this context the only thing we need to set is the view which the splitter will hold. then the splitter will automatically create an instance of this class when the addview method is called. the other members of the class must be set to null like this:

Ccreatecontext CCC;
CCC. m_pnewviewclass = runtime_class (cholderview );
CCC. m_pcurrentdoc = NULL;
CCC. m_pnewdoctemplate = NULL;
CCC. m_plastview = NULL;
CCC. m_pcurrentframe = NULL;

Also we must register a simple window class for the frame. It must only be an empty window. Then we get the rectangle of the frame fromThe invisibleControl on the dialog template, and create the frame as a child of the dialog box. This is the code used:

Crect rect;
Getdlgitem (idc_vtemplate)-> getwindowrect (rect );
Screentoclient (rect );
M_pframe = new cflatframewnd;
M_pframe-> Create (strmyclass, "", ws_child, rect, this );
M_pframe-> showwindow (sw_show );

After this we create the splitter with 1 row and 2 columns on the frame and add its views. to add a view apart from the create context a size is needed. we use just any size because it is not important at this time as the control will be resized soon.

M_csplitter.createstatic (m_pframe, 1, 2 );
M_csplitter.modifystyleex (ws_ex_clientedge, 0, swp_nosize | swp_nomove | swp_nozorder | swp_noactivate );
M_csplitter.createview (100,100, runtime_class (cholderview), csize (), & CCC );
M_csplitter.createview (100,100, runtime_class (cholderview), csize (), & CCC );

To get a view from csplitterwnd we must use it? S getpane method which gets a view at a specified position. We get each view and then create the control on it. Here is a code extracted from the sample project that shows this action:

Cholderview * pview = (cholderview *) m_csplitter.getpane (0, 0 );
Assert_valid (pview );
Pview-> setwnd (& m_listmain );
Pview-> setowner (this );
M_listmain.create (lbs_policy | ws_child | ws_visible | ws_vscroll, crect (,), pview, idc_main );

Finally we set the splitter? S size to cover the entire frame. The views in it will automatically be Resize and resize the controls that they? Re holding.

What more can you create using this code?

Snapshot TProgramLike this one that is fairly simpleYou canCreate a tree control as a master and a list view control as a slave. That? S how you can make almost an exact copy of Windows Explorer? S interface.

The setwnd method of cholderview sets the Active Control on it. it is not said that it must be the only one. for example you can switch between different controls on The View. suppose you want to add different looks for your slave control? A list and an edit. just create a list box and an edit control on the view and when the appropriate event is raised, change the Active Control with setwnd and the control will be changed.

Attachments:

Project demo files: splitterwnd.zip

Source files: Splittersource.zip

Article contributedBorislav

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.