To be a Windows senior engineer, you need to know the MFC controls (full version)

MFC control:

Windows standard controls:

Windows standard controls are provided by the Windows operating system, and some new controls are also provided in Windows 95. All these control objects are programmable, we can use the dialog editor provided by Visual C++ to add them to the dialog. The Microsoft Foundation Class Library (MFC) provides classes that encapsulate these controls, which are listed in Table 6.1.

In MFC, the class CWnd is the base class of all window classes. Naturally, it is also the base class of the control class.

Windows standard controls are provided in the following environments:

windows 95

Windows NT 3.51 and later versions

win32s 1.3

Note: Visual C++ 4.2 and later versions no longer support Win32s.

Basic usage of MFC basic common controls

VC++ control toolbox:

Button

Used to receive user commands. After the application receives the user commands, it usually needs to perform some background work. The button can respond to the click or double-click action . After the button receives the mouse action, it sends corresponding control notifications to its parent window, and the user can perform message mapping on these control notifications to perform corresponding processing.

In a dialog box, you can define a default button, as long as you select the "Default" option in the button properties. If you press the Enter key while the dialog is active, it is equivalent to clicking the default button. MFC provides the CButton class to support button controls.

Check Box

Used to display a certain possible choice, the choice is independent, the user can select or cancel the option . The check mark appears when the option is selected, and the check mark disappears when the option is canceled. The check box is supported by the CButton class in MFC. The user can set or get the current state of the check box through the SetCheck() function and GetCheck() function .

BST_UNCHECKED==((CButton*)GetDlgItem(IDC_CHECK_RES1))->GetCheck()
((CButton*)GetDlgItem(IDC_CHECK_RES1))->SetCheck(true);		

Edit Control

Used to receive the string entered by the user . By selecting the options of the edit box, the edit box can receive character strings, numbers, passwords, etc .; the edit box can also be set to receive multi-line character strings; the case can be converted automatically. The edit box may send multiple control notifications to the parent window , and the user can handle these control notifications if needed. MFC provides the CEdit class to support edit box controls .

GetDlgItem(IDC_***)->SetWindowText(Cstring);
 
GetDlgItem(IDC_***)->GetWindowText(Cstring);

Combo Box

The combination of the list box and the edit box , the user can not only select the existing options in the list, but also input new choices. MFC provides the CComboBox class to support the combo box control.

CComboBox * AAA = (CComboBox*)(GetDlgItem(IDC_***));
 
AAA->AddString(_T("***"));
 
AAA->SelectString(0, _T("***"));
 
AAA->SetCurSel(0);
 
 
int nSel = AAA->GetCurSel();
 
AAA->GetLBText(nSel, Cstring);
 
if(strType.Trim() == _T("***"))

List Box

Used to select a series of possible options , the user can browse through these options through the scroll bar. In the list box, you can make a single selection or multiple selections , depending on the user's settings in the control properties dialog box. MFC provides the CListBox class to support the list box control.

Group Box

Used to enclose a group of controls with a logical relationship, and add borders and titles around these controls . It should be noted that the composition box is only for "grouping" the controls in terms of visual effects. The real "grouping" work requires additional work. Only visually show a set of logical relationships without adding any code

Radio Button

It is used to select a certain possible option. Unlike the (Check Box) check box, this option is not independent .

Generally, several radio buttons form a group , and only one radio button in the same group is selected .

MFC also uses the CButton class to support radio button controls ,

The SetCheck() function and GetCheck() function are also applicable to radio buttons.

Static Text

Used to display a specific string at a specified location , generally used to identify the content of another nearby control . The character string displayed in the static text control generally does not change , but when needed, it must be assigned a different food ID number and set by calling the corresponding function. MFC provides the CStatic class to support static controls .

Picture Control

Display bitmap (Bitmap) and icon (Icon), graphics drawing and display, mainly the square area is suitable for display, the same square area can also use (Static Text) static text box.

Scroll Bar

This includes the horizontal scroll bar and the vertical scroll bar . In addition to the different visual effects, the horizontal scroll bar generates a WM_HSCROLL message when it is scrolled , and the vertical scroll bar sends a WM_VSCROLL message when it is scrolled . MFC provides support for CScrollBar .

Slider Control (Slider Control)

Usually used to accept a series of discrete values in a program .

The user can set the value range of the slider control, and can add tick marks to the control to show the meaning of a specific position .

MFC provides support for the CSliderCtrl class .

Spin Button

Including a pair of up and down arrows close together, use the fine adjustment button to increase or decrease a specific value.

Fine-tuning buttons often require a "buddy" control, which is usually an edit box .

When the up arrow of the fine-tuning button is clicked, the number in the edit box will increase; otherwise, it will decrease . MFC provides CPinButtonCtrl class for support .

Progress Bar (Progress Control)

At the time of an operation it takes a long time to reflect the current progress .

When the progress of the operation continues to advance, the progress bar fills the progress bar frame with a distinctive color. The user can set the range and current position of the progress bar.

MFC provides the CProgressCtrl class for support .

CProgressCtrl* progressbar = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS_1);
 
progressbar->SetRange(0, 4);
 
progressbar->SetPos(0);

Hot Key Control (Hot Key)

The hotkey control looks like an edit box, but the hotkey control can immediately reflect the key combination that the user has just pressed . This is especially useful when setting up the program's hotkey.

The hot key control only displays the key combination on the "visual" , and the work of setting the hot key still requires the user to add code to complete.

MFC provides the CHotKey class for support .

List Control

Display a series of strings with icons in a certain order.

The list control provides four display modes: large icon (Icon), small icon (Small Icon), list (List) and report (Report) .

The user can add new items to the list control, and can also control the display mode of the list control.

MFC provides the CListCtrl class for support .

initialization:

 struct INFO { int id; CString time; CString type; }info; CString id; int nRow=0; id.Format(_T("%d"), info.id); m_ListCtrl.InsertItem(nRow,id); m_ListCtrl.SetItemText(nRow, 1, info.time); m_ListCtrl.SetItemText(nRow, 2, info.type); nRow ++;


Add record:

 
struct INFO
 
{
 
int id;
 
CString time;
 
CString type;
 
}info;
 
CString id;
 
int nRow=0;
 
id.Format(_T("%d"), info.id);
 
m_ListCtrl.InsertItem(nRow,id);
 
m_ListCtrl.SetItemText(nRow, 1, info.time);
 
m_ListCtrl.SetItemText(nRow, 2, info.type);
 
nRow ++;

Tree Control

Display the hierarchical relationship of a series of items , the most typical example is to display files and folders on the disk .

If there are sub- items , click the item in the tree control to expand or contract its sub-items .

MFC provides the CTreeCtrl class for support .

initialization

   void CConfigDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码  CRect rcWnd; GetClientRect(&rcWnd);  CRect rcChild = rcWnd; rcChild.top = rcWnd.top+10; rcChild.bottom -=10; rcChild.left += 160; rcChild.right -= 10; CRect laneRect = rcChild;  if(::IsWindow(m_1Dlg)) m_1Dlg.MoveWindow(rcChild);  if(::IsWindow(m_2Dlg)) m_2Dlg.MoveWindow(rcChild);   if ( ::IsWindow( pTree.GetSafeHwnd() ) ) { pTree.MoveWindow(rcWnd.left+10,rcWnd.top+30,130,350,1); } }

Tree node switching

   void CConfigDlg::OnTvnSelchangedTree(NMHDR *pNMHDR, LRESULT *pResult) { LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR); // TODO: 在此添加控件通知处理程序代码 HTREEITEM hSelected=pNMTreeView->itemNew.hItem; if(hSelected!=NULL) { pTree.SelectItem(hSelected); int nDat = pTree.GetItemData(hSelected);  switch(nDat) { case 1: /*MessageBox(_T("YNAME"));*/ if(m_pPreWnd) m_pPreWnd->ShowWindow(SW_HIDE);  m_1Dlg.ShowWindow(SW_SHOW); m_pPreWnd = &m_1Dlg; break; case 3: /*MessageBox(_T("XNAME"));*/ if(m_pPreWnd) m_pPreWnd->ShowWindow(SW_HIDE);  m_2Dlg.ShowWindow(SW_SHOW); m_pPreWnd = &m_2Dlg; break;  default: break; } } *pResult = 0; }

Change position

   void CConfigDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: 在此处添加消息处理程序代码  CRect rcWnd; GetClientRect(&rcWnd);  CRect rcChild = rcWnd; rcChild.top = rcWnd.top+10; rcChild.bottom -=10; rcChild.left += 160; rcChild.right -= 10; CRect laneRect = rcChild;  if(::IsWindow(m_1Dlg)) m_1Dlg.MoveWindow(rcChild);  if(::IsWindow(m_2Dlg)) m_2Dlg.MoveWindow(rcChild);   if ( ::IsWindow( pTree.GetSafeHwnd() ) ) { pTree.MoveWindow(rcWnd.left+10,rcWnd.top+30,130,350,1); } }

Tab Control

Contains a large number of controls, which can meet the requirements of users to display or obtain a large amount of data classification and display. A typical example is each program label in the Windows taskbar.

Each property sheet is divided into several property pages, these property pages are distinguished by their respective tags, these property pages can contain other controls .

When displaying the property sheet, only the entire content of one property page can be displayed at a time, and the labels of other property pages are displayed at the same time. The user opens the corresponding property page by clicking the label.

MFC provides the CTabCtrl class for support .

Initialize bind variables

m_tab_Light.DeleteAllItems();
 
m_LightParkingDlg.DestroyWindow();
 
m_LightStatusDlg.DestroyWindow();
 
m_tab_Light.InsertItem(0, _T("0001")); // 插入第一个标签
 
m_tab_Light.InsertItem(1, _T("0002")); // 插入第二个标签
 
 
CRect tabRect; // 标签控件客户区的位置和大小
 
m_tab_Light.GetClientRect(&tabRect); // 获取标签控件客户区Rect
 
// 调整tabRect,使其覆盖范围适合放置标签页
 
tabRect.left += 2;
 
tabRect.right -= 1;
 
tabRect.top += 21;
 
tabRect.bottom -= 2;
 
m_LightParkingDlg.Create(IDD_DIALOG_LIGHT_PARKING, &m_tab_Light); // 创建第一个标签页
 
m_LightStatusDlg.Create(IDD_DIALOG_LIGHT_STATUS, &m_tab_Light); // 创建第二个标签页
 
//m_LightParkingDlg.InitData();
 
m_LightParkingDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(),SWP_SHOWWINDOW);

Control page switching

 CRect tabRect; // 标签控件客户区的位置和大小
 
m_tab_Light.GetClientRect(&tabRect); // 获取标签控件客户区Rect
 
// 调整tabRect,使其覆盖范围适合放置标签页
 
tabRect.left += 2;
 
tabRect.right -= 1;
 
tabRect.top += 21;
 
tabRect.bottom -= 2;
 
switch (m_tab_Light.GetCurSel())
 
{
 
 
case 0:
 
m_LightStatusDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
 
m_LightParkingDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
 
break;
 
case 1:
 
m_LightParkingDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
 
m_LightStatusDlg.SetWindowPos(NULL, tabRect.left, tabRect.top, tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
 
break;
 
 
}

Animation control (Animation):

Used to play a video clip in AVI format . The user can control the playback, stop, and positioning of the video clip, but it is also limited to these functions . The animation control settings cannot play audio clips . If users need higher-level video or audio support, please use the MCIWnd control .

MFC provides the CAnimateCtrl class to support animation controls .

Advanced edit box (Rich Edit)

The extension of Edit Control function. In the advanced edit box, in addition to simple input and editing of strings, users can also specify specific formats for characters or paragraphs, and users can even insert OLE items into the advanced edit box .

The advanced edit box basically implements a formatted text editor function, and only requires the user to add a few interfaces.

MFC provides CRichEditCtrl class for support .

Date/Time Picker (Date Time Picker)

Provides users with an intuitive way to select the date and time . The date/time selector looks like a combo box, but when the user clicks the drop-down arrow, a calendar control will be expanded for the user to select. After making a selection, the date/time selector will automatically display the new date/time. MFC provides support for the CDateTimeCtrl class.

SYSTEMTIME times_1; //开始时间日期
 
SYSTEMTIME timee_1; //结束时间日期
 
 
CDateTimeCtrl* dtPickctrs_1 = (CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_START1); //获取时间控件句柄
 
CDateTimeCtrl* dtPickctre_1 = (CDateTimeCtrl*)GetDlgItem(IDC_DATETIMEPICKER_END1);
 
 
memset(×_1,0,sizeof(times_1)); //时钟初始化
 
memset(&timee_1,0,sizeof(timee_1));
 
 
dtPickctrs_1->GetTime(×_1); //获取控件所选时间,保存至变量
 
dtPickctre_1->GetTime(&timee_1);
 
 
 
CString strTimeStart; //将时间转换为字符串
 
strTimeStart.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), times_1.wYear,times_1.wMonth,times_1.wDay,times_2.wHour,times_2.wMinute,times_2.wSecond);
 
CString strTimeEnd;
 
strTimeEnd.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"), timee_1.wYear,timee_1.wMonth,timee_1.wDay,timee_2.wHour,timee_2.wMinute,timee_2.wSecond);
 


Get system time

SYSTEMTIME time;
 
::GetLocalTime(&time);

Calendar control (Month Calender)

As shown in the figure below, it looks similar to a real calendar, and the operation is similar. It intuitively provides users with a way to observe and display the current date . MFC provides the CMonthCalCtrl class for support .

IP address control (IP Adress)

The IP address control is used to enter and edit the IP address . MFC provides the CIPAddressCtrl class for support .

The appearance of this control is similar to an edit box, but it can automatically distinguish the entered characters in groups of 3 and add spaced dots. The IP address control provides convenience for developing programs supporting Internet technology.

 Extended Combo Box (Extended Combo Box)

In addition to the ordinary combo box (Combo Box), it also supports image lists .

You can display specific icons in the combo box to indicate the corresponding selection, not just display text.

MFC provides the CComboBoxEx class for support .

Custom Control 

 When the control is used, its Class must have a class that supports this window class and VC class, such as Button, Edit. Add a member variable of the CEdit class: CEdit m_Text (that is, right-click to add a variable), and then Follow the Edit control to use it.

 

 Hyperlink control (SysLink Control):

1. Used to add hyperlinks on MFC applications, just like hyperlinks in html. You can click to link to a web page. Drag the control to the page and bind a variable (m_linkCtrl), the content inside is all written in accordance with the usage of href in standard html . Wrongly written, the natural link cannot be made.

You can go to see how the href attribute in html is set. Also note the handling of double quotes in the string ( remember to add an escape character \ ), and add a click response message

m_linkCtrl.SetWindowTextW(_T("<a href=\"http://blog.csdn.net/miko_xiaoming\">Miko's Note</a>"));
 
PNMLINK pNMLink = (PNMLINK) pNMHDR;
 
ShellExecuteW(NULL, L"open", pNMLink->item.szUrl, NULL, NULL, SW_SHOWNORMAL); //在浏览器中打开

2. Like the Button control, respond to the corresponding message                                  

 

Split Button Control (Split Button Control)

As shown in the figure below, the multi-select button selection function is realized, and the corresponding menu is added when using

 

 Network Address Control (Network Address Control):

Can be directly used as the input of inet_addr(ip), the separator is comma (,)

Command button control

Not only can you add corresponding commands to the buttons, you can also make comments on the commands

Common properties of all controls:

ID: The resource identifier of the control.

Visiable: Whether the control is visible.

Disabled: Whether the control is prohibited, the prohibited control cannot accept keyboard and mouse input.

Group: Whether to start a new group from this space.

Tab stop: Whether it is possible to switch to this control with the [Tab] key in the program.

Help ID: Whether to assign a help ID to the control, the help ID is based on the resource ID of the control itself.

Client edge: Add a sunken border to the control to make the entire control look like it is sunken.

Static edge: Add a border to the control.

Modal frame: Add a 3D frame to the control. Contrary to Client edge, it is protruding.

Transparent: The window with this attribute is transparent, and the windows below it will not be obscured by it.

Accept files: The window with this attribute can receive drag and drop files. If a file is dragged onto this control, the control will receive the WM_DROPFILES message.

No parent notify: Indicates that the control does not send WM_PARENTNOTIFY messages to its parent window.

Right aligned text: indicates that the text is right aligned.

The above properties can be set through the properties dialog box of the control. In the properties dialog box, press [F1] key to view the help information of the properties.

Each type of control is managed by an MFC control class. When a control is added to the dialog box through the resource editor, visualC++ will automatically generate an instance for the control class, namely the object, and call the constructor. When the dialog box is closed , Its various child control objects are automatically destroyed.

You can also write code in the program without using the resource editor to create and display the control and set the properties of the control.

All control classes come from CWnd. Some operations and property settings of the control need to use the member functions of CWnd itself. Some functions of CWnd are often used to manipulate the control and set the control properties.

SetWindowText: Set the caption text to be displayed on the control, which is used to set the caption property of the control

GetWindowText: Get the title text of the control

EnableWindow: Set the Disabled property of the control, that is, whether the social control can receive keyboard and mouse input

SetWindowPos: Change the size, position and Z-axis order of the window.

MoveWindow: change the size and position of the window

GetWindowRec: Get the size and position of the window (the information is stored in a rectangular structure).

GetClientRect: Get the size of the window client area (the information is stored in a rectangular structure)

ShowWindow: Set the visibility of the window (that is, whether the window is visible)

SetWindowText/GetWindowText can also be used to set/get the title text of the dialog box.

Benefits are coming~

Learning is never a matter of one person. You must have a partner who supervises each other. If you are interested in C/C++, you can follow the editor and send me a private message in the background: [ Programming Exchange ] Let's learn together! You can receive some C/C++ project learning video materials!

 

Guess you like

Origin blog.csdn.net/Python6886/article/details/111405398