Hi, I’m Sean! Recently made a small software based on MFC, record some knowledge points of MFC here.

Create a dialog-based application

The key steps are shown in the screenshot above. Click Next until you finish.

About the use of MFC interface controls

Dialog, Menu, Icon and other files can be found through the left resource view. We can add controls to Dialog, such as buttons and edit boxes. After the control is added, you can modify the control’s ID and other properties by modifying properties, and you can add variables to the dialog box class by right-clicking the event handler (callback function).

Button Common Button

A button event handler, this one is simple and usually uses the BN_CLICKED BN_DOUBLECLICKED callback function.The logic behind the click is implemented in the callback function:

void CVisionFastInputDlg::OnBnClickedAddButton()
{
  // code ...
}
Copy the code

Edit Control Plain text Edit box

Normal text edit box event handler, I use EN_CHANGE, when the contents of the edit box change, trigger a callback.Common code operations are as follows:

Void My_Dlg: : OnEnChangeTextValueEdit () {/ / get the text box content cstrings value; GetDlgItemText(IDC_TEXT_VALUE_EDIT, value); STD ::string v = CT2A(value); // STD ::string to CString m_value = v.c_str(); POINT cp = m_value_edit.getcaretpos (); POINT cp = m_value_edit.getcaretpos (); // Set the cursor position SetCaretPos(cp); ShowCaret(); UpdateData(FALSE); // UpdateData from a variable in the code to the interface. }Copy the code

Static Text Static Text

You can just fill it in when you set the property.

List the Control table

This property is set to View to Report, so that it can be a table style, and add a variable to the interface class by right-clicking it. This control has some initialization operations, and the callback functions are NM_DBLCLK, NM_CLICK, and double-click callback, which can be used to refresh index. Double-clicking the callback can be used to trigger a new dialog box.Common codes are as follows:

// Control variable CListCtrl m_list; / / add header cstrings colums [] = {TEXT (" No "), TEXT (" Name "), TEXT (" Sex ")}; // Add column and set width m_list.InsertColumn(0, colums[0], LVCFMT_LEFT, 200); m_list.InsertColumn(1, colums[1], LVCFMT_LEFT, 800); m_list.InsertColumn(2, colums[2], LVCFMT_LEFT, 200); M_list.setcolumnwidth (2, LVSCW_AUTOSIZE_USEHEADER); / / set properties, special selected m_list. SetExtendedStyle (m_shortcut_list. GetExtendedStyle () | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); // add the first row of data m_list.insertitem (0, TEXT("1001")); m_list.SetItemText(0, 1, TEXT("Henry Ha")); m_list.SetItemText(0, 2, TEXT("Male")); M_list.insertitem (1, TEXT("1002")); m_list.SetItemText(2, 1, TEXT("Marry Li")); m_list.SetItemText(3, 2, TEXT("Male")); // Delete all rows m_list.deleteAllItems ();Copy the code

Combo-box Control Drop-down selection box

This is a dropdown menu, add the callback function ON_CBN_SELCHANGE

CComboBox m_cbx; // add the option m_cbx.insertString (0, _T("Red")); m_cbx.InsertString(1, _T("Black")); M_cbx.setcursel (0); ON_CBN_SELCHANGE(IDC_DATASOURCE, &CVisionFastInputDlg::OnCbnSelchange) void My_Dlg::OnCbnSelchange() { // TODO: Add control notification handler code here int index = m_cbx.getCursel (); }Copy the code

miscellaneous

  1. Disable/enable buttons
m_btn.EnableWindow(TRUE);
m_btn.EnableWindow(FALSE);
Copy the code
  1. Set the dialog box title
SetWindowText(_T("Amazing Application"));
Copy the code
  1. Add a hint to the button
CToolTipCtrl m_ToolTip;
m_ToolTip.Create(this);
m_ToolTip.AddTool(&m_btn, _T("This is my button"));
Copy the code
  1. Register and deregister hotkeys
// Listen for hotkey messages ON_MESSAGE(WM_HOTKEY, &My_Dlg::OnHotKey); // declare afx_msg LRESULT OnHotKey(WPARAM WPARAM, LPARAM LPARAM); // define a hotkey value WM_HOTEKY_SAVE, Registered hotkey Ctrl + Shift + S bool ret = : : RegisterHotKey (this - > GetSafeHwnd (), WM_HOTEKY_SAVE, MOD_CONTROL | MOD_SHIFT, 'S'); ret = UnregisterHotKey(GetSafeHwnd(), WM_HOTEKY_SAVE);Copy the code
  1. Minimize to tray area and tray area icon destruction
Void My_Dlg::CloseAndHide() {// The main window hides NOTIFYICONDATA m_nID when a close message is received; m_nid.cbSize = (DWORD)sizeof(NOTIFYICONDATA); m_nid.hWnd = this->m_hWnd; m_nid.uID = IDR_MAINFRAME; m_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; m_nid.uCallbackMessage = WM_SHOWTASK; // Custom message name wcscPY_s (m_nid.sztip, _T("Application")); m_nid.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME)); Shell_NotifyIcon(NIM_ADD, &m_nid); // Add the icon ShowWindow(SW_HIDE) in the tray area; // Hide the main window UnHotKey(); } void My_Dlg::DeleteTray() {Shell_NotifyIcon(NIM_DELETE, &m_nID); }Copy the code
  1. Pallet area operation
ON_MESSAGE(WM_SHOWTASK, &my_dlg ::OnShowTask) // declare afx_msg LRESULT OnShowTask(WPARAM WPARAM LPARAM); LRESULT My_Dlg::OnShowTask(WPARAM WPARAM, LPARAM LPARAM) { = IDR_MAINFRAME) return 1; Switch (lParam) {case WM_LBUTTONUP:// Left click to display the main interface {SetHotKey(); this->ShowWindow(SW_SHOW); SetForegroundWindow(); DeleteTray(); }break; Case WM_RBUTTONUP:// Right click the popup menu {LPPOINT lpoint = new tagPOINT; ::GetCursorPos(lpoint); // Get mouse position CMenu menu; menu.CreatePopupMenu(); menu.AppendMenuW(MFT_STRING, IDR_SHOW, _T("Open")); menu.AppendMenuW(MFT_STRING, IDR_TRIGER, _T("Disable")); menu.AppendMenuW(MFT_STRING, IDR_TRIGER, _T("Enable")); menu.AppendMenuW(MFT_STRING, IDR_EXIT, _T("Exit")); SetForegroundWindow(); int xx = TrackPopupMenu(menu, TPM_RETURNCMD, lpoint->x, lpoint->y, NULL, this->m_hWnd, NULL); // display the menu and get the option ID if (xx == IDR_SHOW) {SetHotKey(); this->ShowWindow(SW_SHOW); SetForegroundWindow(); DeleteTray(); } else if (xx == IDR_TRIGER) { } else if (xx == IDR_EXIT) { DeleteTray(); HMENU hmenu = menu.Detach(); menu.DestroyMenu(); delete lpoint; ExitProcess(0); } HMENU hmenu = menu.Detach(); menu.DestroyMenu(); delete lpoint; }break; Case WM_LBUTTONDBLCLK: {}break; } return 0; }Copy the code
  1. Prompt box
HWND hPreWnd; If (hPreWnd = ::FindWindow(NULL, _T("Application"))) {// MB_SYSTEMMODAL indicates blocking mode bool ret = ::MessageBox(hPreWnd, _T("Are you sure to delete all!" ), _T("Warning!" ), MB_YESNO | MB_SYSTEMMODAL); }Copy the code
  1. Program singleton
void My_Dlg::SingletonInstance() { HWND hPreWnd; if (hPreWnd = ::FindWindow(NULL, _T("Application"))) { ::ShowWindow(hPreWnd, SW_SHOW); ::SetForegroundWindow(hPreWnd); ExitProcess(0); }}Copy the code

That’s all for today! Thank you for reading! If this article is helpful, please give it a thumbs up