One of the MFC skills

Source: Internet
Author: User

1. "add property page
:

Class of the create dialog box, which must be inherited from cpropertypage. Then, create an object of the cpropertysheet class in the class (header file) of the property page to add this dialog box.
M_tabsheet is the object m_skatch of the newly created dialog box class. Finally, the following code is implemented in functions such as oninitdialog () in the. cpp file:
M_tabsheet.create (this, ws_child | ws_visible, 0 );
"// Make the button of the tab below
"If (m_tabsheet.m_hwnd)
"M_tabsheet.showwindow (sw_maximize); // display the tab
// Add a tag. The tag name is determined by the title bar of each subdialog box.
"M_tabsheet.addpage (& m_skatch );
"// Use create to create an attribute page
"M_tabsheet.create (this, ws_child | ws_visible, ws_ex_controlparent );
"
Rect;
"M_tabsheet.getwindowrect (& rect );
"Int width = rect. Right-rect. Left;
"Int Height = rect. Bottom-rect. Top;
"
"// Adjust the size and position of the property page
"M_tabsheet.setwindowpos (null, 225,225, width-82, height, swp_noactivate );
The property page is added. To add multiple attribute pages, you only need to add multiple objects, as shown in the following figure: m_tabsheet.addpage (& m_skatch1 );
M_tabsheet.addpage (& m_skatch2 );
......

2. "add the title bar (column) in List Control
:

Create a list control with the ID of idc_list. In the view item under its styles attribute, select "TOP" and "sort" in "report" and "align.
Create a clistctrl object m_list in the class (header file) in the dialog box where the list is located, and implement the following code in functions such as oninitdialog () in the. cpp file:
Cstring strname [3];
Strname [0] = "screen name ";
Strname [1] = "Form ID ";
Strname [2] = "category path ";
For (INT I = 0; I <3; I ++)
{
M_list.insertcolumn (I, strname [I], lvcf mt_left, 130 );
}
Before that, bind the List Control ID and the clistctrl object m_list to the dodataexchange (cdataexchange * PDX) function, as shown below:
Ddx_control (PDX, idc_list, m_list );

3. "add control in toolbar and statusbar
:

Method 1 (1). You can only create a control in the toolbar. First, create a button in the toolbar with the ID id_tool_combo (where we want to place the created control on the button ).
Second, create a new class cmaintoolbar, which must be inherited from the ctoolbar (the creation process is roughly as follows: select project/Add to project/New Class; you can also select the project root, and then click
Right-click a class, select a new class, or CTL + W, and select Add class/New Class --- Select generic in class type
Class, enter the name of the new class in the name column, and enter the ctoolbar in the base class). After the creation is successful, the object of the control to be added is created in the class, for example:
Ccombobox "" m_wndmycombo;
Cstatic "" m_wndcategory, m_wndcategorypath;
Cbutton "" m_wndopenbutton;
Cedit "" m_wndedit;
Then initialize In the constructor, for example:
M_wndmycombo.m_hwnd = NULL;
M_wndcategory.m_hwnd = NULL;
M_wndcategorypath.m_hwnd = NULL;
M_wndopenbutton.m_hwnd = NULL;
M_wndedit.m_hwnd = NULL;
Create the object m_wndtoolbar of the cmaintoolbar in the header file of the cmainframe, and implement the oncreate function in the. cpp file as follows:
"Int Index = 0;
"Crect rect; // It can be defined in the header file.
"// ComboBox
"{
"" // Find the specified tool item
"" While (m_wndtoolbar.getitemid (INDEX )! = Id_tool_combo)
"Index ++;
"" // Set the width of the specified tool item and obtain the new area. 120 is the width.
"" M_wndtoolbar.setbuttoninfo (index, id_tool_combo, tbbs_separator, 120 );
"" M_wndtoolbar.getitemrect (index, & rect );
""
"" // Set the location
"" Rect. Top + = 1;
"" Rect. Bottom + = 200;
""
"" // Create and display controls
"" If (! M_wndtoolbar.m_wndmycombo.create (ws_child | ws_visible | cbs_autohscroll |
"Cbs_dropdownlist | cbs_hasstrings, rect, & m_wndtoolbar, id_tool_combo ))
""{
"Trace0 (" failed to create combo-box/N ");
"Return false;
""}
"" M_wndtoolbar.m_wndmycombo.showwindow (sw_show );
""
"" // Fill in content"
"" M_wndtoolbar.m_wndmycombo.addstring ("25% ");
"" M_wndtoolbar.m_wndmycombo.addstring ("50% ");
"" M_wndtoolbar.m_wndmycombo.addstring ("75% ");
"
"" // Select the default item
"" M_wndtoolbar.m_wndmycombo.setcursel (0 );
""
"" // Get the content and display it in msgbox
"" Cstring strcontent;
"" M_wndtoolbar.m_wndmycombo.getwindowtext (strcontent );

"" Index = 0;
"}
Other controls are created similarly (just pay attention to the parameters of the respective create function ).
Method 2 (2). This method is not easy to control: directly create the control object to be added in the header file of cmainframe, such as the cbutton object.
M_wndaboutbutton, and then create the ctoolbar or cstatusbar object, such as the cstatusbar object.
_ Wndstatusbar; Add the following functions:
Protected:
Virtual void recalclayout (bool bnotify = true );
"Afx_msg void cmainframe: onviewstatusbar ();
Then, bind the statusbar ID and the onviewstatusbar function in the. cpp file, as shown below: begin_message_map (cmainframe, cframewnd)
"// {Afx_msg_map (cmainframe)
"On_command (id_view_status_bar, onviewstatusbar)
"On_wm_create ()
"//} Afx_msg_map
End_message_map ()
Then implement the following code at the end of the create function (before the return value:
Crect RC;
"Verify (m_wndaboutbutton.create (_ T (" myabout "),
"" Ws_visible, RC, this, id_app_about ));

"// Todo: Remove this if you don't want Tool tips or a resizeable Toolbar
"M_wndtoolbar.setbarstyle (m_wndtoolbar.getbarstyle () |
"Cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic );
Implement the following in the recalclayout function:
Crect RC;
If (m_wndstatusbar.m_hwnd)
{
"" M_wndstatusbar.getwindowrect (& rc );
"" Screentoclient (& rc );
"" RC. Right-= 50;
"" M_wndstatusbar.setwindowpos (null, RC. Left, RC. Top, RC. Width (), RC. Height (),
"Swp_nozorder );

"" RC. Left = RC. Right;
"" RC. Right + = 50;
"" M_wndaboutbutton.setwindowpos (null, RC. Left, RC. Top, RC. Width (), RC. Height (),
"Swp_nozorder );
"}
Finally, implement the following in onviewstatusbar:
Bool bshow = m_wndstatusbar.getstyle () & ws_visible;
"M_wndaboutbutton.setwindowpos (null, 0, 0, 0, 0,
Swp_nozorder | swp_nomove | swp_nosize | swp_noactivate |
(Bshow? Swp_showwindow: swp_hidewindow ));
The creation in the toolbar is the same. You only need to change the handle.

4. "controls created through control dynamically control their attributes
:

In the header file of the dialog box class, create an object for the control whose property is to be changed. To change the property of a button (whose ID is idc_mybutton), create a cbutton pair
Like m_button. Then, bind the button ID and the created object in the dodataexchange function in. cpp:
// {Afx_data_map (cprintdlg)
"" // Note: The classwizard will add DDX and DDV cballs here
"Ddx_control (PDX, idc_mybutton, m_button );
"//} Afx_data_map
Then you can initialize the function at the end of the function:
"M_button.enablewindow (false );
So far, attributes have been changed. If you want to dynamically change its attributes, You can implement the following (change the attributes by clicking two buttons ):
// Click response function of the radioall button
Void cprintdlg: onradioall ()
{
"" // Todo: add your control notification handler code here
"" M_button.enablewindow (true );
}

// Click response function of the radioselect button
Void cprintdlg: onradioselect ()
{
"" // Todo: add your control notification handler code here
"" M_button.enablewindow (false );
}
You can also change it by clicking a check button, and implement it in its click response function:
M_button.enablewindow (! M_button.isw.wenabled ());
This applies to other controls.

5. "window separation and dock
:
1. Create a cmysplitter class. The base class is csplitterwnd.
2. The onmousemove function of this class is reloaded:
Void cmysplitter: onmousemove (uint nflags, cpoint point)
{
"// Restrict the movement range of the shard.
"If (point. x <228 | point. x> 600)
"{
"" Cwnd: onmousemove (nflags, point );
"}
"Else
"{
"" Csplitterwnd: onmousemove (nflags, point );
"}
}
3. Then we can split it with a normal window to do that. If (point. x <228 | point. x> 600) the range here can be set with you ^ _ ^, which is simple enough.
Iv. Split Window
Create a shard object in maifram. h:
Protected:
Cmysplitter m_wndsplitter; // split the window object
// Implement window splitting in maifram. cpp:
Bool cmainframe: oncreateclient (lpcreatestruct/* lpcs */, ccreatecontext * pcontext)
{
"// Create a splitter window
If (! M_wndsplitter.createstatic (this, 1, 2 ))
"" Return false;
If (! M_wndsplitter.createview (0, 0,
Runtime_class (cleftview), csize (228,100), pcontext)
|! M_wndsplitter.createview (0, 1,
Runtime_class (cdataeditview), csize (100,100), pcontext ))
"{
"" M_wndsplitter.destroywindow ();
"" Return false;
"}

"Return true;
}

6. ① how to maximize the program at the beginning?

② In the upper-right corner of the form, the EXE file made by VC ++ does not have the "maximize and minimize" button. How can this function be implemented?
③ How does one gray the maximize button when a window is displayed?

① C… in the app class... In app: initinstance (), change m_pmainwnd-> showwindow (sw_show) to m_pmainwnd-> showwindow (sw_maximize );
② Ws_sysmenu | ws_minimizebox | ws_maximizebox style is used for createwidnow.
③ Method 1:
Bool cmainframe: precreatewindow (createstruct & CS)
{
If (! Cframewnd: precreatewindow (CS ))
Return false;
// Todo: Modify the window class or styles here by modifying
// The createstruct CS
// Disable the maxmini box
CS. Style & = ~ Ws_maximizebox;
Return true;
}
Method 2:
Cmenu * pmenu = afxgetapp ()-> m_pmainwnd-> getsystemmenu (false );
Int x = pmenu-> getmenuitemcount ();
Uint pid = pmenu-> getmenuitemid (x-1 );
Pmenu-> enablemenuitem (PID, mf_disabled );
Method 3:
Modifystyle (ws_maximizebox, 0 );
This function can also be a maximize button failure!
In addition, you can dynamically change the window style in the program.

7. Change the title of the property page

Void cprosheet: setpagetitle (INT npage, int nimage, cstring strtitle)
{
Tc_item item;
// Item. Mask = tcif_text | tcif_image; // set the icon + Text
Item. Mask = tcif_image; // only sets the icon
Item. iimage = nimage;
// Item. psztext = strtitle. getbuffer (0); // set the text
Gettabcontrol ()-> setitem (npage, & item );
// Remove the annotator before the above two lines of code to set text
}

8. Create a dynamic menu

Void cmainframe: onselectstate (nmtoolbar * pnmtb, lresult * PLR)
{
"Cmenu menu;
"If (! Menu. createmenu ())
"Return;
"Menu. appendmenu (mf_string, 0," Start ");
"Menu. appendmenu (mf_string, 0," end ");
"Crect RC;
"M_wndtoolbar.sendmessage (tb_getrect, pnmtb-> iItem, (lparam) & rc );
"M_wndtoolbar.clienttoscreen (& rc );
"Menu. trackmenu (tpm_leftalign | tpm_leftbutton | tpm_vertical,
"RC. Left, RC. Bottom, this, & rc );
// "Menu. destroymenu ();
"Menu. Detach ();
}
9. about Printing

1. Which view should be printed?
(Cmainframe *) afxgetmainwnd ()-> m_wndsplitter.setactivepane (...)
// Pane corresponding to the view to be printed

2. there is a single document Project, and the document window is split: The Left view is managed by the ctreeview derived class, and the right view is managed by the clistview derived class cmylistview (which is lvs_report in style, I want to add print and print preview for the right view. CPP added
On_command (id_file_print, clistview: onfileprint)
On_command (id_file_print_preview, clistview: onfileprintpreview) and
Bool cmylistview: onprepareprinting (cprintinfo * pinfo)
{
// Todo: Call doprepareprinting to invoke the Print dialog box

// Return clistview: onprepareprinting (pinfo );
Pinfo-> setmaxpage (2 );
Bool Bret = doprepareprinting (pinfo );
Pinfo-> m_nnumpreviewpages = 2;
Return Bret;
}
3. The following section is extracted from msdn to change the message routing. After this code is used, messages (menus, controls, and subwindows) in the cview will be processed by the cmyshape class first. I don't know if you want this effect.

// This example extends strates extending the framework's standard command
// Route from the view to objects managed by the view. This example
// Is from an object-oriented drawing application, similar to
// Drawcli sample application, which draws and edits "shapes ".

Bool cmyview: on1_msg (uint NID, int ncode, void * pextra,
Afx_cmdhandlerinfo * phandlerinfo)
{
// Extend the framework's command route from the view
// The application-specific cmyshape that is currently selected
// In the view. m_pactiveshape is null if no shape object
// Is currently selected in the view.
If (m_pactiveshape! = NULL)
& M_pactiveshape-> on1_msg (NID, ncode, pextra, phandlerinfo ))
Return true;

// If the object (s) in the extended command route don't handle
// The command, then let the base class on‑msg handle it.
Return cview: on1_msg (NID, ncode, pextra, phandlerinfo );
}

// The command handler for id_shape_color (menu command to change
// The Color of the currently selected shape) was added
// The message map of cmyshape (note, not cmyview) using classwizard.

// The menu item will be automatically enabled or disabled, depending
// On whether a cmyshape is currently selected in the view, that is,
// Depending on whether cmyview: m_pactiveview is null. It is not
// Necessary to implement an on_update_command_ui handler to enable
// Or disable the menu item.

Begin_message_map (cmyshape, c1_target)
// {Afx_msg_map (cmyshape)
On_command (id_shape_color, onshapecolor)
//} Afx_msg_map
End_message_map ()

If you just want to call the onfileprint () function, you can try the following code, just like calling functions in other classes.

Cmdiframewnd * pframe =
(Cmdiframewnd *) afxgetapp ()-> m_pmainwnd;

// Get the active MDI child window.
Cmdichildwnd * pchild =
(Cmdichildwnd *) pframe-> getactiveframe ();

// Or javasichildwnd * pchild = pframe-> mdigetactive ();

// Get the active view attached to the active MDI child
// Window.
Cmyview * pview = (cmyview *) pchild-> getactiveview ();

Pview-> onfileprint ();

4.
Void cmyreportview: onfileopen ()
{
Char filter [] = "Crystal report files (*. rpt) | *. rpt | all files (*. *) | *. * | ";
Crect rect;
Cfiledialog opendlg (true, 0, 0, ofn_hidereadonly | ofn_filemustexist, (lpctstr) filter, null );
If (opendlg. domodal ()! = Idok) // display the file dialog box
Return;
Cstring m_fname = opendlg. getpathname (); // get the file name
If (m_crystalreport)
M_crystalreport.destroywindow ();
Getclientrect (rect );
//// // Create a control ///////////////
If (! M_crystalreport.create (afxregisterwndclass (0,
Afxgetapp ()-> loadstandardcursor (idc_arrow), ws_child | ws_visible, rect, this, idc_crystalreport1 ))

{
Afxmessagebox ("control creation failed! ");
Return;
}
M_crystalreport.setwindowparenthandle (long) (This-> m_hwnd); // you can specify the parent window.
M_crystalreport.setwindowborderstyle (0); // set it to no border
M_crystalreport.setwindowleft (0); // left space
M_crystalreport.setwindowtop (0); // top space
M_crystalreport.setwindowcontrols (false); // The toolbar is not displayed.
M_crystalreport.setreportfilename (m_fname); // you can specify a report file.
M_crystalreport.set1_wwidth (rect. Width (); // you can specify the window width.
M_crystalreport.setwindowheight (rect. Height (); // you can specify the window height.
M_crystalreport.setformulas (0, "company =/" VC Knowledge Base/""); // set the value of the company variable in the report to VC Knowledge Base
M_crystalreport.setdestination (0); // you can specify that the output object is a screen.
M_crystalreport.printreport (); // display the report
}
Void cmyreportview: onfileprint ()
{
If (m_crystalreport & m_crystalreport.getreportfilename ()! = "")
{
M_crystalreport.setdestination (1); // you can specify a printer as the output object.
M_crystalreport.printreport (); // print
}

10. Scroll

Create an SDI project based on cscrollview (select cscrollview in step 1)
If you have already created an instance, skip this step.
Then:
As shown in figure
Void ctestview: oninitialupdate ()
{
Cscrollview: oninitialupdate ();

Csize sizetotal;
// Todo: calculate the total size of this view
Sizetotal. Cx = 1024; // modify the two
Sizetotal. Cy = 768 ;//
Setscrollsizes (mm_text, sizetotal );

 

 

 

PS: This article Reprinted from: http://hi.baidu.com/percommouse/blog/item/63cd0a60fdf0eb40eaf8f8d6.html

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.