MFC common controls [turn]
- Reference video: https://www.bilibili.com/video/BV1JW41147NX?t=2&p=3
catalogue
Combo box (drop-down box) CComboBox
Static text box CStatic
- Static text box is the simplest control. It is mainly used to display text information. It cannot accept user input. Generally, it does not need to connect variables or process messages.
- Important properties of static text boxes are:
- ID: the default ID of all static text boxes is IDC_STATIC, static ID, not responding to any message (event)
- Caption: modify the displayed content
Interface | function |
CWnd::SetWindowText | Set control content |
CWnd::GetWindowText | Get control content |
CStatic::SetBitmap | Set bitmap (picture with suffix bmp) |
Associated control variables:
- Due to XXX_STATIC static ID is a variable that cannot be associated, so you need to modify the ID before associating the variable:
- In the main dialog class OnInitDialog(), complete the corresponding interface test:
- Example 1: setting text
m_text.SetWindowTextW(TEXT("ha-ha")); CString str; m_text.GetWindowTextW(str); MessageBox(str);
- Example 2: display picture:
//Set the static control window style to bitmap centered display m_pic.ModifyStyle(0xf, SS_BITMAP | SS_CENTERIMAGE); //Get bitmap handle through path #define HBMP(filepath,width,height) (HBITMAP)LoadImage(AfxGetInstanceHandle(),filepath,IMAGE_BITMAP,width,height,LR_LOADFROMFILE|LR_CREATEDIBSECTION) //The width and height settings should be set according to the size of the control CRect rect; m_pic.GetWindowRect(rect); //Static control setting bitmap m_pic.SetBitmap(HBMP(TEXT("./1.bmp"), rect.Width(), rect.Height()));
Normal button CButton
- The main attribute of the button is Caption, which sets the text displayed on the button.
- The most messages processed by the command button are: BN_CLICKED, double-click the button to jump to the processing function.
- Alternatively, click button properties - > control event - > select the desired event and add the processing function:
Interface | function |
CWnd::SetWindowText | Set control content |
CWnd::GetWindowText | Get control content |
CWnd::EnableWindow | Sets whether the control is grayed out |
Associated control variables:
- In the main dialog class OnInitDialog(), complete the corresponding interface test
//Get the contents of the button CString str; m_button.GetWindowText(str); MessageBox(str); //Set button content m_button.SetWindowText(TEXT("^_^")); //Set button status to gray m_button.EnableWindow(FALSE); m_button.EnableWindow(TRUE);
Edit box CEdit
- Common attribute settings:
attribute | meaning |
Number | True can only enter numbers |
Password | True password mode |
Want return | True receives the Enter key to wrap the line automatically. Line wrapping can only be performed in multiline mode |
Multiline | True multiline mode |
Auto VScroll | True: when there are too many characters in the vertical direction, the scroll bar will appear automatically, and the Vertical Scroll setting is valid |
Vertical Scroll | True when there are too many characters in the vertical direction, the scroll bar will appear automatically. It is used with Auto VScroll |
Horizontal Scroll | True when there are too many characters in the vertical direction, the scroll bar will appear automatically. It is used in combination with auto hscoll |
Read Only | True read only |
- Common interfaces:
Interface | function |
CWnd::SetWindowText | Set control content |
CWnd::GetWindowText | Get control content |
Associate control variables
- In the main dialog class OnInitDialog(), complete the corresponding interface test:
/Set button content m_edit.SetWindowText(TEXT("this is a test")); //Get the contents of the button CString str; m_edit.GetWindowText(str); MessageBox(str);
Associate basic type variables
- If an edit box is connected with a variable of Value category, the variable represents the edit box, and the content displayed in the edit box is the Value of the variable.
- However, changing the content of the edit box will not automatically update the value of the corresponding variable. Similarly, changing the value of the variable will not automatically refresh the content of the edit box. To maintain consistency, you need to use the UpdateData() function to update:
- If the contents of the edit box are changed, the statement UpdateData(TRUE) should be used to obtain the dialog box data
- If value of the variable changes, dialog box control should be initialized with the statement update data (false)
- In the main dialog class OnInitDialog(), complete the corresponding code test:
m_e1 = 10.11; UpdateData(FALSE); //FALSE indicates that M_ The value of E1 is updated to the corresponding control UpdateData(TRUE); //TRUE indicates that the value of the control is updated to m_e1 variable
Combo box (drop-down box) CComboBox
- Common attribute settings:
attribute | meaning |
data | Set the content, and use the semicolon ";" in English between different contents separate |
type | Display style |
Sort | True auto sort content |
- Common interfaces:
CComboBox::AddString | Add a string to the combo box |
CComboBox::SetCurSel | Sets the current selection (which item is currently displayed), and the subscript starts from 0 |
CComboBox::GetCurSel | Gets the subscript of the currently selected item in the combo box |
CComboBox::GetLBText | Gets the content of the specified location |
CComboBox::DeleteString | Deletes the string at the specified location |
CComboBox::InsertString | Inserts a string at the specified location |
- After associating control variables, test the interface:
- Example:
m_cbx.AddString(TEXT("Tang Monk")); m_cbx.AddString(TEXT("Sun WuKong")); m_cbx.AddString(TEXT("Zhu Bajie")); m_cbx.AddString(TEXT("Monk Sha")); //Set default options m_cbx.SetCurSel(0); //insert m_cbx.InsertString(4, TEXT("White dragon horse")); //delete m_cbx.DeleteString(3); //Get the specific contents of the index of No. 1 CString str; m_cbx.GetLBText(1, str); MessageBox(str);
- Get the selected subscript and display the content:
//Get the index position int index = m_cbx.GetCurSel(); CString str; m_cbx.GetLBText(index, str); MessageBox(str);
- Common events for combo boxes are: CBN_SELCHANGE: this event is automatically triggered when an item in the combo box is selected.
void CMFCApplication2Dlg::OnCbnSelchangeCombo1() { // TODO: add control notification handler code here }
List control CListCtrl
- Common attribute settings: View - > report (report method)
- Common interfaces:
Interface | function |
CListCtrl::SetExtendedStyle | Set list style |
CListCtrl::GetExtendedStyle | Get list style |
CListCtrl::InsertColumn | Insert a column, mainly used to set the title |
CListCtrl::InsertItem | Insert new item content on a line |
CListCtrl::SetItemText | Set the sub item content of a row and a column |
CListCtrl::GetItemText | Get the contents of a row or column |
- After associating control variables, test the interface:
- Example:
//Set style //LVS_EX_GRIDLINES grid //LVS_EX_FULLROWSELECT selects the entire row m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT); //Insert title CString head[] = { TEXT("full name"), TEXT("Age"), TEXT("Gender") }; //Insert column m_list.InsertColumn(0, head[0], LVCFMT_LEFT, 100); m_list.InsertColumn(1, head[1], LVCFMT_LEFT, 100); m_list.InsertColumn(2, head[2], LVCFMT_LEFT, 100); //Insert the body content, first determine the row, and then determine the column for (int i = 0; i < 10; i++) { CString str; str.Format(TEXT("Zhang San_%d"), i ); //OK line m_list.InsertItem(i, str); //Set column int j = 0; m_list.SetItemText(i, ++j, TEXT("male")); m_list.SetItemText(i, ++j, TEXT("23")); }
- Program rendering:
Tree control CTreeCtrl
- Common attribute settings:
attribute | meaning |
has buttons | True has an expand button |
has lines | True has expansion lines |
lines at root | True has a root node |
- Common interfaces:
Interface | function |
AfxGetApp() | Get application object pointer |
CWinApp::LoadIcon | Load custom icon |
CImageList::Create | Create image list |
CImageList::Add | Image list append Icon |
CTreeCtrl::SetImageList | Set drawing status list |
CTreeCtrl::InsertItem | Insert node |
CTreeCtrl::SelectItem | Set default selection |
CTreeCtrl::GetSelectedItem | Get selected item |
CTreeCtrl::GetItemText | Get something |
- Associated control variables:
Add Icon resource (icon)
- Put the ico resource file in the project res folder
- Resource view - > icon - > Add Resource:
- Import ico file
- Load icon through code
//1. Settings Icon //Prepare HICON Icon HICON icons[4]; icons[0] = AfxGetApp()->LoadIconW(IDI_ICON1); icons[1] = AfxGetApp()->LoadIconW(IDI_ICON2); icons[2] = AfxGetApp()->LoadIconW(IDI_ICON3); icons[3] = AfxGetApp()->LoadIconW(IDI_ICON4); //CImageList list; // This collection must be saved h do member properties //Create a picture collection list.Create(30, 30, ILC_COLOR32, 4, 4); //Add specific pictures for (int i = 0; i < 4;i++) { list.Add(icons[i]); } m_tree.SetImageList(&list, TVSIL_NORMAL); //2. Set node HTREEITEM root = m_tree.InsertItem(TEXT("Root node"), 0, 0, NULL); HTREEITEM parent = m_tree.InsertItem(TEXT("Parent node"), 1, 1, root); HTREEITEM sub1 = m_tree.InsertItem(TEXT("Child node 1"), 2, 2, parent); HTREEITEM sub2 = m_tree.InsertItem(TEXT("Child node 2"), 3, 3, parent); //Set default options m_tree.SelectItem(sub1);
- Common events of tree control are: TVN_SELCHANGED: this event is automatically triggered when a node is selected.
void CMy06CTreeCtrlDlg::OnTvnSelchangedTree1(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR); // TODO: add control notification handler code here *pResult = 0; //Get current item HTREEITEM item = m_tree.GetSelectedItem(); CString name = m_tree.GetItemText(item); MessageBox(name); }
Label control CTabCtrl
- Drag and drop Tab Control in the ui toolbox
- Put tabsheet H and tabsheet CPP is placed in the project file sibling directory and added to the project directory
- Associate Tab Control on ui with Control type (CTabSheet)
- Add dialog box
- a) Resource view - > Dialog - > right click - > insert Dialog
- b) Set corresponding properties:
- Style - > child (child window)
- Border - > none
- c) Custom class: click dialog box template - > right click - > Add Class (MyDlg1, MyDlg2)
- d) In the main dialog box, you need corresponding header files to define custom class objects
- OnInitDialog() in the main dialog class does initialization
//Add Tab m_tab.AddPage(TEXT("System settings"), &dlg1, IDD_DIALOG1); m_tab.AddPage(TEXT("system management"), &dlg2, IDD_DIALOG2); //display m_tab.Show();
- Attached:
- TabSheet.h:
#if !defined(AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_) #define AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 // TabSheet.h : header file // / // CTabSheet window #define MAXPAGE 16 class CTabSheet : public CTabCtrl { // Construction public: CTabSheet(); // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTabSheet) //}}AFX_VIRTUAL // Implementation public: int GetCurSel(); int SetCurSel(int nItem); void Show(); void SetRect(); BOOL AddPage(LPCTSTR title, CDialog *pDialog, UINT ID); virtual ~CTabSheet(); // Generated message map functions protected: LPCTSTR m_Title[MAXPAGE]; UINT m_IDD[MAXPAGE]; CDialog* m_pPages[MAXPAGE]; int m_nNumOfPages; int m_nCurrentPage; //{{AFX_MSG(CTabSheet) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; / //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_TABSHEET_H__42EE262D_D15F_46D5_8F26_28FD049E99F4__INCLUDED_)
- TabSheet.cpp:
// TabSheet.cpp : implementation file // #include "stdafx.h" //#include "Property5.h" #include "TabSheet.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif / // CTabSheet CTabSheet::CTabSheet() { m_nNumOfPages = 0; m_nCurrentPage = 0; } CTabSheet::~CTabSheet() { } BEGIN_MESSAGE_MAP(CTabSheet, CTabCtrl) //{{AFX_MSG_MAP(CTabSheet) ON_WM_LBUTTONDOWN() ON_WM_HSCROLL() //}}AFX_MSG_MAP END_MESSAGE_MAP() / // CTabSheet message handlers BOOL CTabSheet::AddPage(LPCTSTR title, CDialog *pDialog,UINT ID) { if( MAXPAGE == m_nNumOfPages ) return FALSE; m_nNumOfPages++; m_pPages[m_nNumOfPages-1] = pDialog; m_IDD[m_nNumOfPages-1] = ID; m_Title[m_nNumOfPages-1] = title; return TRUE; } void CTabSheet::SetRect() { CRect tabRect, itemRect; int nX, nY, nXc, nYc; GetClientRect(&tabRect); GetItemRect(0, &itemRect); nX=itemRect.left; nY=itemRect.bottom+1; nXc=tabRect.right-itemRect.left-2; nYc=tabRect.bottom-nY-2; m_pPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW); for( int nCount=1; nCount < m_nNumOfPages; nCount++ ) m_pPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW); } void CTabSheet::Show() { int i = 0; for( i=0; i < m_nNumOfPages; i++ ) { m_pPages[i]->Create( m_IDD[i], this ); if (AfxGetMainWnd()) InsertItem( i, m_Title[i] ); else return; } m_pPages[0]->ShowWindow(SW_SHOW); for( i=1; i < m_nNumOfPages; i++) m_pPages[i]->ShowWindow(SW_HIDE); SetRect(); } void CTabSheet::OnLButtonDown(UINT nFlags, CPoint point) { CTabCtrl::OnLButtonDown(nFlags, point); if(m_nCurrentPage != GetCurFocus()) { m_pPages[m_nCurrentPage]->ShowWindow(SW_HIDE); m_nCurrentPage=GetCurFocus(); m_pPages[m_nCurrentPage]->ShowWindow(SW_SHOW); // m_pPages[m_nCurrentPage]->SetFocus(); //AfxMessageBox("checked"); wbm test } } int CTabSheet::SetCurSel(int nItem) { if( nItem < 0 || nItem >= m_nNumOfPages) return -1; int ret = m_nCurrentPage; if(m_nCurrentPage != nItem ) { m_pPages[m_nCurrentPage]->ShowWindow(SW_HIDE); m_nCurrentPage = nItem; m_pPages[m_nCurrentPage]->ShowWindow(SW_SHOW); // m_pPages[m_nCurrentPage]->SetFocus(); CTabCtrl::SetCurSel(nItem); } return ret; } int CTabSheet::GetCurSel() { return CTabCtrl::GetCurSel(); } void CTabSheet::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { // TODO: Add your message handler code here and/or call default CTabCtrl::OnHScroll(nSBCode, nPos, pScrollBar); }