关于MFC界面控件的使用
通过左侧资源视图可以找到Dialog、Menu、Icon等文件,
我们可以往Dialog中添加控件,如按钮、编辑框。添加完控件之后,可以通过修改属性修改控件的ID及其他属性,并可以通过右键添加事件处理程序(回调函数),添加变量到对话框类当中。
Button 普通按钮
按钮事件处理程序,这个比较简单,一般就用单击或双击回调函数
(BN_CLICKED BN_DOUBLECLICKED)
具体点击后的逻辑在回调函数中实现即可:
void CVisionFastInputDlg::OnBnClickedAddButton()
Edit Control 普通文本编辑框
普通文本编辑框事件处理程序,我使用了EN_CHANGE,当编辑框中的内容有变化的时候,触发回调。
常用代码操作如下:
void My_Dlg::OnEnChangeTextValueEdit()
CString value;
GetDlgItemText(IDC_TEXT_VALUE_EDIT, value);
std::string v = CT2A(value);
m_value = v.c_str();
POINT cp = m_value_edit.GetCaretPos();
SetCaretPos(cp);
ShowCaret();
UpdateData(FALSE);
Static Text 静态文本
这个在设置属性的时候直接把内容填写好就行了。
List Control 表格
这个属性设置要讲View设置为Report,这样才能是表格样式,并通过右键添加变量将其添加到界面类中,这个控件有些初始化的操作,回调函数常用NM_DBLCLK、NM_CLICK,及单击双击回调,单击回调可以用来刷新index,双击回调可以用来触发新的对话框。
常用代码如下:
CListCtrl m_list;
CString colums[] = {TEXT("No"), TEXT("Name"), TEXT("Sex")};
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);
m_list.SetExtendedStyle(m_shortcut_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES );
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"));
m_list.DeleteAllItems();
Combo-box Control 下拉选择框
这个是个下拉菜单,添加回调函数ON_CBN_SELCHANGE
CComboBox m_cbx;
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()
int index = m_cbx.GetCurSel();
禁用/启用按钮
m_btn.EnableWindow(TRUE)
m_btn.EnableWindow(FALSE)
设置对话框标题
SetWindowText(_T("Amazing Application"));
给按钮添加提示内容
CToolTipCtrl m_ToolTip
m_ToolTip.Create(this)
m_ToolTip.AddTool(&m_btn, _T("This is my button"))
注册、注销热键
ON_MESSAGE(WM_HOTKEY, &My_Dlg::OnHotKey);
afx_msg LRESULT OnHotKey(WPARAM wParam, LPARAM lParam);
bool ret = ::RegisterHotKey(this->GetSafeHwnd(), WM_HOTEKY_SAVE, MOD_CONTROL | MOD_SHIFT, 'S');
ret = UnregisterHotKey(GetSafeHwnd(), WM_HOTEKY_SAVE);
最小化到托盘区及托盘区图标销毁
void My_Dlg::CloseAndHide()
//接收到关闭消息时主窗口隐藏
NOTIFYICONDATA m_nid
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
wcscpy_s(m_nid.szTip, _T("Application"))
m_nid.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME))
Shell_NotifyIcon(NIM_ADD, &m_nid)
ShowWindow(SW_HIDE)
UnHotKey()
void My_Dlg::DeleteTray()//销毁托盘图标
Shell_NotifyIcon(NIM_DELETE, &m_nid)
托盘区的操作
ON_MESSAGE(WM_SHOWTASK, &My_Dlg::OnShowTask)
afx_msg LRESULT OnShowTask(WPARAM wParam, LPARAM lParam);
LRESULT My_Dlg::OnShowTask(WPARAM wParam, LPARAM lParam)
{
if (wParam != IDR_MAINFRAME) return 1;
switch (lParam)
case WM_LBUTTONUP://左键单击显示主界面
SetHotKey();
this->ShowWindow(SW_SHOW);
SetForegroundWindow();
DeleteTray();
}break;
case WM_RBUTTONUP://右击弹出菜单
LPPOINT lpoint = new tagPOINT;
::GetCursorPos(lpoint);
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);
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;
HWND hPreWnd
if (hPreWnd = ::FindWindow(NULL, _T("Application")))
// MB_SYSTEMMODAL为阻塞模式
bool ret = ::MessageBox(hPreWnd, _T("Are you sure to delete all!"), _T("Warning!"), MB_YESNO | MB_SYSTEMMODAL)
void My_Dlg::SingletonInstance()
HWND hPreWnd;
if (hPreWnd = ::FindWindow(NULL, _T("Application")))
::ShowWindow(hPreWnd, SW_SHOW);
::SetForegroundWindow(hPreWnd);
ExitProcess(0);
今天就分享到这里啦!感谢各位的阅读!码字不易,如果本文对你有帮助的话,帮忙点个赞吧~
- 261
-
磊叔的技术博客
Windows
CentOS
- 2071
-
DevYK
Windows
WebRTC
- 913
-
Marno
Flutter
macOS
Windows
- 11.3w
-
CodeDevMaster
Windows
- 3134
-
Karl_wei
Flutter
Android
Windows
- 5856
-
守望时空33
Windows
Linux