C++MFC Programming note day10 MF interface control use 2, Property Page dialog box, MFC thread

Source: Internet
Author: User

A tree-type control
1 related classes
The ctreectrl-parent class is a CWnd, control class.
The ctreeview-parent class is the CCtrlView, view class. Ctreeview=cview+ctreectrl
Ctreeview::gettreectrl
2 Use of CTreeCtrl
Compare CListCtrl: Each data item of a list control is an equal relation, through the data item
The index of the data item is worth the information. Each data item in a tree control is called a node. Between nodes
Relationships include parent-child relationships and sibling relationships. A node is usually obtained through a node handle.
Data that is typically used to represent hierarchical relationships.


2.1 Setting the icon list for a control
Ctreectrl::setimagelist
2.2 Inserting a tree node
Ctreectrl::insertitem
2.3 Setting the expansion state of a node
Ctreectrl::expand
2.4 Setting the node height
Ctreectrl::setitemheight
....
Jobs: Simulating the left part of the explorer using a tree control






Two Property Page dialog box
1 classification
Tabbed and wizard-style
2 related classes
cpropertypage-the page dialog box class.
The cpropertysheet-frame window class, also known as the Form class.
One property Page dialog box = object of a sheet class + objects of multiple page classes
3 Use
3.1 Creating a Tabbed property page
3.1.1 The Settings Dialog Resource window, modify the language to Simplified Chinese, and double-click generate the appropriate class.
Note that the parent class must be cpropertypage.
3.1.2 Repeat 3.1.1 Steps to design and add multiple pages
3.1.3 Add a subclass of CPropertySheet in the project, add two of the dialog class to the class
Member variable, and in the constructor, call the AddPage () function to add the Page object.
3.1.4 Creating and displaying tabbed property pages
CPropertySheet::D omodal ()
3.1.5 in the En_change message handler function of the CPage1 edit box control:
CPropertyPage::SetModified ()
Set the Apply button to be available
3.1.6 Add a virtual function onapply () in the CPage1 class, in the function, the line width in the dialog box
is passed to the view, and the view is redrawn.
3.1.7 in the message handler function of the CPage2 color button:
1 Set the Apply button to be available
2 Pop-up Color dialog box, select the appropriate color, save to the variable in the dialog box.
3.1.8 Add a virtual function onapply () in the CPage2 class, in the function, the color of the dialog box
is passed to the view, and the view is redrawn.
In the page (inherited from CPropertyPage) onapply, get and manipulate the view window example:
CFrameWnd *pframe= (cframewnd*) AfxGetMainWnd ();
Csdi_propertypageview *pview= (csdi_propertypageview*) Pframe->getactiveview ();
pview->m_curlinecolor=m_curcolor;//setting a color variable in a view
Pview->invalidaterect (null,true);//Set View window invalid, throw redraw
In the sheet page (inherited from CPropertySheet), define the page variable, in the constructor (with two), add:
AddPage (&M_PAGE1);
AddPage (&m_page2);
Use of the Color dialog box:
CColorDialog cd;
int RESULT=CD. DoModal ();
if (Result==idok)//If the value is modified
{
M_CURCOLOR=CD. GetColor ();//Gets the selected value
SetModified (TRUE);//After modification, the Apply button is set to the available state
}


3.2 Creating a wizard-style property page
3.2.1 Create and display steps are similar to tabbed ones, except that, before they are created and displayed,
Set as Wizard mode
CPropertySheet::SetWizardMode
3.2.2 Set the wizard button for each page, in Cpropertypage::onsetactive ()
virtual function in the Set Wizard button.
Cpropertypage::getparent-gets the parent window of the page
Cpropertysheet::setwizardbuttons-Setup Wizard button
3.2.3 Processing CPage1
1 binding member variables to controls
2 Add Wm_initdialog Message handler function, add City option to function
3 in the virtual function Onwizardnext function of CPage1, the null judgment.
3.2.4 Processing CPage2
1 binding member variables to controls
2 in the virtual function Onwizardnext function of CPage2, the null judgment.
3.2.5 Processing Cpage
1 binding member variables to controls
2 in the virtual function Onwizardfinish function of CPage2, the null judgment.
3.2.6 in the message function of the view class on the menu:
1 Passing data from a dialog box to a view
2 View Redraw
3.2.7 in the OnDraw function of the view class, the output data




Three MFC threads
MFC divides threads into two main categories by usage:
Worker threads-there is usually no user interface to complete some time-consuming operations in the background.
User interface threads-typically have a user interface and a message loop that can interact with the user.
1 Steps to use worker threads
1.1 Defining thread functions, function prototypes
UINT mycontrollingfunction (LPVOID pparam);
Note: A thread function is either a global function or a static member function of a class
1.2 Calls Afxbegintread and passes the thread function as the first parameter. Other than that
The second parameter function is the transfer of data between the main thread and the sub-threads.
2 user interface thread usage steps
2.1 Adding a derived class for the CWinThread class in the project
2.2 Creating a user interface for a child thread in the Cmythread::initinstance () function
(The user interface of a child thread takes a dialog box)
2.3 Call Afxbegintread to pass Cmythread run-time class information as a parameter


3 Thread Synchronization Classes
Csyncobject-the parent class of the thread synchronization class, the subclasses are:
ccriticalsection-Critical Zone
cevent-Events
cmutex-Mutex

csemaphore-Signal Volume


Example 1:
1. New MFC dialog box Application Mfctreectrl.
2. Visual editing Interface control


3. CTRL+W member variables and message events for bound controls


4. Adding member variables and functions in ***dlg.h
CImageList m_ilnormal;//Icon List
void Inittreectrl ();


5. The main implementation code in ***DLG.CPP (partially auto-generated)

MFCtreeCtrlDlg.cpp:implementation file//#include "stdafx.h" #include "MFCtreeCtrl.h" #include "MFCtreeCtrlDlg.h" # Include "Resource.h" #ifdef _debug#define new Debug_new#undef this_filestatic char this_file[] = __file__; #endif//////// CAboutDlg dialog used for App Aboutclass Caboutdlg:public Cdialog{public:caboutdlg ();//Dialog Data//{{afx_data (CAboutDlg) enum {IDD = Idd_aboutbox};//}}AFX_ data//ClassWizard generated virtual function overrides//{{afx_virtual (CABOUTDLG) protected:virtual void    DoDataExchange (cdataexchange* PDX); DDX/DDV support//}}afx_virtual//implementationprotected://{{afx_msg (CABOUTDLG)//}}AFX_MSGDECLARE_MESSAGE_MAP ( )}; Caboutdlg::caboutdlg (): CDialog (Caboutdlg::idd) {//{{afx_data_init (CAboutDlg)//}}afx_data_init}void CABOUTDLG::D Odataexchange (cdataexchange* pdx) {CDialog::D odataexchange (PDX);//{{afx_data_map (CAboutDlg)//}}afx_data_map} Begin_message_map (CAboutDlg, CDialog)//{{afx_msg_map (CABOUTDLG)//No message Handlers//}}afx_msg_mapend_message_map ()/////////////////////////////////////////////////// Cmfctreectrldlg Dialogcmfctreectrldlg::cmfctreectrldlg (cwnd* pparent/*=null*/): CDialog (Cmfctreectrldlg::idd, pparent) {//{{afx_data_init (CMFCTREECTRLDLG)//Note:the ClassWizard would add Member Initialization here//}}afx_data_init//Note that LoadIcon does not require a subsequent destroyicon in Win32m_hicon = AfxG Etapp ()->loadicon (IDR_MAINFRAME);} void Cmfctreectrldlg::D odataexchange (cdataexchange* pdx) {CDialog::D odataexchange (PDX);//{{afx_data_map ( CMFCTREECTRLDLG) DDX_Control (PDX, Idc_tree, M_tree);//}}afx_data_map}begin_message_map (Cmfctreectrldlg, CDialog)// {{Afx_msg_map (CMFCTREECTRLDLG) On_wm_syscommand () On_wm_paint () On_wm_querydragicon ()/}} Afx_msg_mapend_message_map ()/////////////////////////////////////////////////////////////////////////////// Cmfctreectrldlg message Handlersbool Cmfctreectrldlg::oninitdialog () {cdialog::oninitdialog ();/Add "About ..." menu item to System menu.//Idm_aboutbox must is in the System command range. ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox); ASSERT (Idm_aboutbox < 0xf000); cmenu* Psysmenu = GetSystemMenu (FALSE), if (psysmenu! = NULL) {CString straboutmenu;straboutmenu.loadstring (Ids_ AboutBox), if (!straboutmenu.isempty ()) {Psysmenu->appendmenu (mf_separator);p Sysmenu->appendmenu (MF_STRING, Idm_aboutbox, Straboutmenu);}}  Set The icon for this dialog. The framework does this automatically//when the application ' s main window was not a Dialogseticon (M_hicon, TRUE);//Set B  IG Iconseticon (M_hicon, FALSE);//Set small icon//todo:add extra initialization Hereinittreectrl (); return TRUE;  Return TRUE unless you set the focus to a control}void Cmfctreectrldlg::onsyscommand (UINT nid, LPARAM LPARAM) {if (NID & 0xfff0) = = Idm_aboutbox) {CAboutDlg dlgabout;dlgabout.domodal ();} Else{cdialog::onsyscommand (NID, LParam);}} If you add a Minimize button to your dialog, you'll need tHe code below//to draw the icon. For MFC applications using the Document/view model,//This is automatically do for your by the framework.void Cmfctreect Rldlg::onpaint () {if (Isiconic ()) {CPAINTDC DC (this);//device context for Paintingsendmessage (Wm_iconerasebkgnd, ( WPARAM) DC. GETSAFEHDC (), 0);//Center icon in client rectangleint Cxicon = GetSystemMetrics (sm_cxicon); int cyicon = GetSystemMetrics ( Sm_cyicon); CRect rect; GetClientRect (&rect); int x = (rect. Width ()-Cxicon + 1)/2;int y = (rect. Height ()-Cyicon + 1)/2;//Draw the ICONDC. DrawIcon (x, y, m_hicon);} Else{cdialog::onpaint ();}} The system calls the obtain the cursor to display while the user drags//the minimized window. Hcursor Cmfctreectrldlg::onquerydragicon () {return (hcursor) M_hicon;} void Cmfctreectrldlg::inittreectrl () {m_ilnormal.create (Idb_normal,16,1,rgb (0,0,0)); M_tree. SetImageList (&m_ilnormal,lvsil_normal); Htreeitem Hroot=m_tree. InsertItem ("root folder", 0, 1); Htreeitem H1=m_tree. InsertItem ("Sub-folder 1", 0,1,hrooT); Htreeitem H2=m_tree. InsertItem ("Sub-folder 2", 0,1,hroot); Htreeitem H3=m_tree. InsertItem ("Sub-folder 3", 0,1,hroot); Htreeitem H4=m_tree. InsertItem ("Sub-folder 4", 0,1,hroot); Htreeitem H2_1=m_tree. InsertItem ("sub-folder 2_1", 0,1,H2); Htreeitem H2_1_1=m_tree. InsertItem ("sub-folder 2_1_1", 0,1,h2_1); M_tree. SetItemHeight (25);//Height m_tree. Expand (Hroot,tve_expand);//Set the expanded state of a node item}


"Additional examples are added later"




Related Article

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.