C ++ MFC programming notes day10 MF interface controls 2. properties page dialog box, MFC thread

Source: Internet
Author: User

C ++ MFC programming notes day10 MF interface controls 2. properties page dialog box, MFC thread
One tree control
1. Related Classes
CTreeCtrl-the parent class is CWnd and the control class.
CTreeView-the parent class is CCtrlView and the View class. CTreeView = CView + CTreeCtrl
CTreeView: GetTreeCtrl
2 Use of CTreeCtrl
Compare CListCtrl: each data item in the list control has an equal relationship.
The index is worth the information of the data item. Each data item in the tree control is called a node. Between Nodes
The relationship includes the parent-child relationship and the sibling relationship. A node is usually obtained through a node handle.
It is usually used to represent hierarchical data.


2.1 set the widget icon list
CTreeCtrl: SetImageList
2.2 Insert Tree nodes
CTreeCtrl: InsertItem
2.3 set the expand status of a node
CTreeCtrl: Expand
2.4 set the node height
CTreeCtrl: SetItemHeight
....
Job: Use the tree control to simulate the left part of the Resource Manager






2. property page dialog box
1 category
Label and guide
2 related classes
CPropertyPage-page dialog box class.
CPropertySheet-framework window class, also known as form class.
One property Page dialog box = one Sheet Class Object + multiple Page class objects
3. Use
3.1 create a tag property page
3.1.1 set dialog box resource window, modify the language to simplified Chinese, double-click to generate the corresponding class.
Note that the parent class must be CPropertyPage.
3.1.2 Repeat steps 3.1.1 to design and add multiple pages
3.1.3 Add the CPropertySheet subclass to the project, and add
Member variable. In the constructor, call the AddPage () function to add a page object.
3.1.4 create and display a tab property page
CPropertySheet: DoModal ()
3.1.5 In the EN_CHANGE message processing function of the edit box control of CPage1:
CPropertyPage: SetModified ()
Set the application button to available
3.1.6 Add the virtual function OnApply () to the CPage1 class. In the function, set the line width in the dialog box.
Pass it to the view and redraw the view.
3.1.7 in the message processing function of the "color" button of CPage2:
1. Set the application button to available.
2. In the displayed color dialog box, select the appropriate color and save it to the variables in the dialog box.
3.1.8 Add the virtual function OnApply () to the CPage2 class. In the function, set the color in the dialog box.
Pass it to the view and redraw the view.
In OnApply on the Page (inherited from CPropertyPage), obtain and operate the View window example:
CFrameWnd * pFrame = (CFrameWnd *) AfxGetMainWnd ();
CSDI_propertypageView * pView = (CSDI_propertypageView *) pFrame-> GetActiveView ();
PView-> m_curLineColor = m_curColor; // set the color variable in the view.
PView-> InvalidateRect (NULL, TRUE); // sets the View window to be invalid, causing re-painting
On the Sheet page (inherited from CPropertySheet), define the page variable. In the constructor (there are 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 (); // obtain the selected value
SetModified (TRUE); // after modification, set the application button to available.
}


3.2 Create a wizard property page
3.2.1 The creation and display steps are similar to those of the label type. The only difference is that before the creation and display,
Set to wizard mode
CPropertySheet: SetWizardMode
3.2.2 set the wizard button for each page on the CPropertyPage: OnSetActive ()
Set the wizard button in the virtual function.
CPropertyPage: GetParent-get the parent window of the page
CPropertySheet: SetWizardButtons-set the wizard button
3.2.3 process CPage1
1. Bind a member variable to the control.
2. Add the WM_INITDIALOG message processing function and add the City option to the function.
3. The OnWizardNext function of the virtual function CPage1 is null.
3.2.4 process CPage2
1. Bind a member variable to the control.
2. In the virtual function OnWizardNext function of CPage2, It is null.
3.2.5 process CPage
1. Bind a member variable to the control.
2. In the virtual function OnWizardFinish function of CPage2, It is null.
3.2.6 in the message function of the View class to the menu:
1. Pass the data in the dialog box to the view
2 view re-painting
3.2.7 output data in the OnDraw function of the View class




3. MFC thread
MFC divides threads into two categories by purpose:
Worker thread-generally, there is no user interface to complete some time-consuming operations in the background.
User Interface threads-usually have user interfaces and message loops, and can interact with users.
1. Steps for using a worker thread
1.1 define a thread function and a function prototype
UINT MyControllingFunction (LPVOID pParam );
Note: The thread function is either a global function or a static member function of the class.
1.2 call AfxBeginTread and pass the thread function as the first parameter. In addition
The second parameter function is to transmit data between the main thread and the sub-thread.
2 steps for using the user interface thread
2.1 Add a derived class of the CWinThread class to the Project
2.2 create a subthread user interface in the CMyThread: InitInstance () function
(Dialog box for subthread User Interface)
2.3 call AfxBeginTread and pass in the runtime class information of CMyThread as a parameter


3. Thread Synchronization
CSyncObject-the parent class of the thread synchronization class. Its sub-classes include:
CCriticalSection-critical section
CEvent-Event
CMutex-mutex

CSemaphore-semaphore


Example 1:
1. Create the MFCtreeCtrl application in the MFC dialog box.
2. Visual editing interface controls


3. ctrl + w bind the member variables and message events corresponding to the control.


4. *** add member variables and functions to Dlg. h.
CImageList m_ilNormal; // list of icons
Void initTreeCtrl ();


5. *** main implementation code in Dlg. cpp (some are automatically 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 (); // Dial Og 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: DoDataExchange (CDataExchange * pDX) {CDialog: DoDataExchange (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 * p Parent/* = NULL */): CDialog (CMFCtreeCtrlDlg: IDD, pParent) {// {AFX_DATA_INIT (CMFCtreeCtrlDlg) // NOTE: the ClassWizard will add member initialization here //} AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in response = AfxGetApp ()-> LoadIcon (IDR_MAINFRAME);} void response:: DoDataExchange (CDataExchange * pDX) {CDialog: DoDataExchange (pDX); // {AFX_DATA_MAP (CMFCtree CtrlDlg) DDX_Control (pDX, IDC_TREE, m_tree); //} AFX_DATA_MAP} values (outputs, CDialog) // {AFX_MSG_MAP (outputs) ON_WM_SYSCOMMAND () ON_WM_PAINT () values () //} AFX_MSG_MAPEND_MESSAGE_MAP () //////////////////////////////////////// /// // ///// ////// CMFCtreeCtrlDlg message handlersBOOL CMFCtreeCtrlDlg:: OnInitDialog () {CDialog: OnInitDialog (); // Add" About... "menu item to system menu. // IDM_ABOUTBOX must be 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); pSysMenu-> AppendMenu (MF_STRING, IDM_ABOUTBOX, strAboutMenu); }}// Set the icon for this dialog. the framework does this automatically // when the application's main window is not a dialogSetIcon (m_hIcon, TRUE); // Set big 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 will need the code below // to draw the icon. for MFC applications using the document/view model, // this is automatically done for you by the framework. void CMFCtreeCtrlDlg: 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 callthis to obtain the cursor to display while the user drags // the minimized window. HCURSOR labels: 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",); HTREEITEM h1 = m_tree.InsertItem ("subfolders 1", hroot ); HTREEITEM h2 = m_tree.InsertItem ("subfolder 2", hroot); HTREEITEM h3 = m_tree.InsertItem ("subfolder 3", hroot ); HTREEITEM h4 = m_tree.InsertItem ("subfolder 4", hroot); HTREEITEM h2_1 = m_tree.InsertItem ("subfolder 2_1", h2 ); HTREEITEM h2_1_1 = m_tree.InsertItem ("subfolders 2_1_1", h2_1); m_tree.SetItemHeight (25); // height m_tree.Expand (hroot, TVE_EXPAND ); // set the expansion status of a node item}


[Add other examples later]




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.