文章目录
- CCommonDialog
- CFileDialog
- CEdit
- 托盘技术
- 进程的启动
- 附录1:启动线程方式
- 附录2:MFC对话框的退出过程
CCommonDialog
通用对话框 CCommonDialog
这些对话框类封装 Windows 公共对话框。 它们提供了易于使用的复杂对话框实现。
CFileDialog
提供用于打开或保存文件的标准对话框。
CColorDialog
提供用于选择颜色的标准对话框。
CFontDialog
提供用于选择字体的标准对话框。
CFindReplaceDialog
为搜索和替换操作提供标准对话框。
CPrintDialog
提供用于打印文件的标准对话框。
CPrintDialogEx
提供 Windows 打印属性表。
CPageSetupDialog
显示和配置页面设置的标准对话框
CFileDialog
其中较为常用的是CFileDialog的构造函数以及GetPathName()
GetPathName()是CFileDialog类的一个成员函数,用于获取用户选择的文件的完整路径名。当用户在打开或保存对话框中选择了一个文件后,可以通过调用GetPathName()函数来获取该文件的完整路径名。该函数的返回值是一个CString对象,包含了用户选择的文件的完整路径名。
class CFileDialog : public CCommonDialog
{DECLARE_DYNAMIC(CFileDialog)public:
// Attributes__declspec(property(get=GetOFN)) OPENFILENAME m_ofn;const OPENFILENAME& GetOFN() const;OPENFILENAME& GetOFN();LPOPENFILENAME m_pOFN;// Constructorsexplicit CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAsLPCTSTR lpszDefExt = NULL,LPCTSTR lpszFileName = NULL,DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,LPCTSTR lpszFilter = NULL,CWnd* pParentWnd = NULL,DWORD dwSize = 0,BOOL bVistaStyle = TRUE);virtual ~CFileDialog();// Operationsvirtual INT_PTR DoModal();/// <summary>/// Determines if the current dialog in folder picker mode.</summary>/// <returns>/// If the dialog in the folder picker mode, the return value is TRUE. Otherwise - FALSE</returns>BOOL IsPickFoldersMode() const { return m_bPickFoldersMode; }/// <summary>/// Determines if the current dialog in non-file system folder picker mode.</summary>/// <returns>/// If the dialog in the non-file system folder picker mode, the return value is TRUE. Otherwise - FALSE</returns>BOOL IsPickNonFileSysFoldersMode() const { return m_bPickNonFileSysFoldersMode; }// Helpers for parsing file name after successful return// or during Overridable callbacks if OFN_EXPLORER is setCString GetPathName() const; // return full path and filenameCString GetFileName() const; // return only filenameCString GetFileExt() const; // return only extCString GetFileTitle() const; // return file titleBOOL GetReadOnlyPref() const; // return TRUE if readonly checked// Enumerating multiple file selectionsPOSITION GetStartPosition() const;CString GetNextPathName(POSITION& pos) const;// Helpers for custom templatesvoid SetTemplate(UINT nWin3ID, UINT nWin4ID);void SetTemplate(LPCTSTR lpWin3ID, LPCTSTR lpWin4ID);// Other operations available while the dialog is visibleCString GetFolderPath() const; // return full pathvoid SetControlText(int nID, LPCTSTR lpsz);void HideControl(int nID);void SetDefExt(LPCTSTR lpsz);virtual void UpdateOFNFromShellDialog();void ApplyOFNToShellDialog();IFileOpenDialog* GetIFileOpenDialog() throw();IFileSaveDialog* GetIFileSaveDialog() throw();IFileDialogCustomize* GetIFileDialogCustomize() throw();
protected:BOOL m_bVistaStyle;BOOL m_bPickFoldersMode;BOOL m_bPickNonFileSysFoldersMode;DWORD m_dwCookie;void* m_pIFileDialog;void* m_pIFileDialogCustomize;BOOL m_bOpenFileDialog; // TRUE for file open, FALSE for file saveCString m_strFilter; // filter string// separate fields with '|', terminate with '||\0'TCHAR m_szFileTitle[_MAX_FNAME]; // contains file title after returnTCHAR m_szFileName[_MAX_PATH]; // contains full path name after returnOPENFILENAME* m_pofnTemp;
};
下面对其构造函数参数详解与CFileDialog的相应属性
explicit CFileDialog(BOOL bOpenFileDialog, //指定什么类型对话框去创建LPCTSTR lpszDefExt = NULL, //指定默认的文件扩展名。可以为NULL,表示没有默认扩展名。LPCTSTR lpszFileName = NULL, //指定默认的文件名。可以为NULL,表示没有默认文件名DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, //指定对话框的标志LPCTSTR lpszFilter = NULL, //指定文件过滤器。用于过滤文件类型,使用户只能选择指定类型的文件CWnd* pParentWnd = NULL, //指定父窗口。可以为NULL,表示使用默认的父窗口DWORD dwSize = 0, //指定对话框的大小。可以为0,表示使用默认大小。BOOL bVistaStyle = TRUE //指定对话框的样式是否为Vista风格。如果为TRUE,表示使用Vista风格;如果为FALSE,表示使用旧的样式。
);
Set it to TRUE to construct a File Open dialog box. 代入这个参数(TRUE)去构建文件打开的对话框。
Set it to FALSE to construct a File Save As dialog box.代入FALSE去构建一个另存为对话框。
#define OFN_READONLY 0x00000001
#define OFN_OVERWRITEPROMPT 0x00000002
#define OFN_HIDEREADONLY 0x00000004
#define OFN_NOCHANGEDIR 0x00000008
#define OFN_SHOWHELP 0x00000010
#define OFN_ENABLEHOOK 0x00000020
#define OFN_ENABLETEMPLATE 0x00000040
#define OFN_ENABLETEMPLATEHANDLE 0x00000080
#define OFN_NOVALIDATE 0x00000100
#define OFN_ALLOWMULTISELECT 0x00000200
#define OFN_EXTENSIONDIFFERENT 0x00000400
#define OFN_PATHMUSTEXIST 0x00000800
#define OFN_FILEMUSTEXIST 0x00001000
#define OFN_CREATEPROMPT 0x00002000
#define OFN_SHAREAWARE 0x00004000
#define OFN_NOREADONLYRETURN 0x00008000
#define OFN_NOTESTFILECREATE 0x00010000
#define OFN_NONETWORKBUTTON 0x00020000
#define OFN_NOLONGNAMES 0x00040000 // force no long names for 4.x modules
#if(WINVER >= 0x0400)
#define OFN_EXPLORER 0x00080000 // new look commdlg
#define OFN_NODEREFERENCELINKS 0x00100000
#define OFN_LONGNAMES 0x00200000 // force long names for 3.x modules
// OFN_ENABLEINCLUDENOTIFY and OFN_ENABLESIZING require
// Windows 2000 or higher to have any effect.
#define OFN_ENABLEINCLUDENOTIFY 0x00400000 // send include message to callback
#define OFN_ENABLESIZING 0x00800000
#endif /* WINVER >= 0x0400 */
#if (_WIN32_WINNT >= 0x0500)
#define OFN_DONTADDTORECENT 0x02000000
#define OFN_FORCESHOWHIDDEN 0x10000000 // Show All files including System and hidden files
#endif // (_WIN32_WINNT >= 0x0500)
CEdit
CEdit是MFC中的一个类,用于创建和操作单行或多行的文本框控件。CEdit类继承自CWnd类,并提供了一系列函数和属性来管理文本框的内容、样式和行为。
通过CEdit类,你可以创建一个文本框控件,并对其进行各种操作,如设置文本内容、获取文本内容、设置文本样式、处理文本框消息等。
// NOTE: This class must remain a binary-compatible subset
// of CEditView. Do not add data members or virtual functions
// directly to this class.
class CEdit : public CWnd
{
public: BOOL Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID);
//WS_CHILD|WS_VISIBLE|ES_MULTILINE...BOOL GetModify() const; //被修改过的标识void SetModify(BOOL bModified = TRUE); //清理或者设置修改标识
#if defined(UNICODE) Unicode版本支持显示气球CString GetCueBanner() const;BOOL ShowBalloonTip(_In_z_ LPCWSTR lpszTitle, _In_z_ LPCWSTR lpszText, _In_ INT ttiIcon = TTI_NONE); //气球提示BOOL ShowBalloonTip(_In_ PEDITBALLOONTIP pEditBalloonTip);BOOL HideBalloonTip(); //隐藏气球提示
#endif // (UNICODE)
// Attributesint GetLineCount() const; //获取文本框中的行数void GetRect(LPRECT lpRect) const; //获取矩形DWORD GetSel() const; //获取选中文字的位置void GetSel(int& nStartChar, int& nEndChar) const;void SetMargins(UINT nLeft, UINT nRight); //设置边栏DWORD GetMargins() const;void SetLimitText(UINT nMax);//设置编辑框最大文字数量UINT GetLimitText() const;CPoint PosFromChar(UINT nChar) const; //坐标与字符索引的映射int CharFromPos(CPoint pt) const;//相反的映射int LineFromChar(int nIndex = -1) const; //行数和字符索引的映射int LineIndex(int nLine = -1) const;//反向映射int LineLength(int nLine = -1) const; //获取第几行的长度void LineScroll(int nLines, int nChars = 0);//滚动多少行,负数向上,正数向下int GetLine(_In_ int nIndex, _Out_ LPTSTR lpszBuffer) const; //获取第几行文字(\r\n)// NOTE: may not return null characterint GetLine (int nIndex, LPTSTR lpszBuffer,int nMaxLength) const; //有缓冲区边界限制// OperationsBOOL FmtLines(BOOL bAddEOL);void LimitText(int nChars = 0);void ReplaceSel(LPCTSTR lpszNewText, BOOL bCanUndo = FALSE);void SetPasswordChar(TCHAR ch); //设置密码void SetRect(LPCRECT lpRect);void SetRectNP(LPCRECT lpRect);void SetSel(DWORD dwSelection, BOOL bNoScroll = FALSE);void SetSel(int nStartChar, int nEndChar, BOOL bNoScroll = FALSE);BOOL SetTabStops(int nTabStops, LPINT rgTabStops);void SetTabStops();BOOL SetTabStops(const int& cxEachStop); // takes an 'int'// Clipboard operationsBOOL CanUndo() const;void EmptyUndoBuffer(); //清空Undo的内容BOOL Undo();void Clear();void Copy();void Cut();void Paste();BOOL SetReadOnly(BOOL bReadOnly = TRUE);int GetFirstVisibleLine() const;TCHAR GetPasswordChar() const;
};
托盘技术
可参照下方链接: https://blog.csdn.net/japhydream/article/details/5062635
在主对话框的头文件添加相应消息
enum { UM_NOTIFYICON = WM_USER +888 };
初始化对话框时添加
auto hIcon = theApp.LoadIcon(IDR_MAINFRAME);
NOTIFYICONDATA nd;nd.cbSize = sizeof(NOTIFYICONDATA);nd.hWnd = m_hWnd;nd.uID = IDR_MAINFRAME; //编号 类似于SetTimer(1,nd.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;nd.uCallbackMessage = UM_NOTIFYICON; //用户消息nd.hIcon = hIcon;
_tcscpy_s(nd.szTip, _countof(nd.szTip), _T("记事本平台"));
Shell_NotifyIcon(NIM_ADD, &nd);
对话框关闭时添加删除
NOTIFYICONDATA nd{sizeof(nd),m_hWnd,IDR_MAINFRAME};//nd.cbSize = sizeof(NOTIFYICONDATA);//nd.hWnd = m_hWnd;//nd.uID = IDR_MAINFRAME; Shell_NotifyIcon(NIM_DELETE, &nd);
对对应消息的进行处理
void CMainDlg::ShowMenu()
{CMenu menu; //全部菜单的加载if (!menu.LoadMenu(IDR_MAINFRAME)) //加载托盘的菜单栏 return;CMenu* pPopup = menu.GetSubMenu(0); //获取第几列ASSERT(pPopup != NULL);CPoint Point;GetCursorPos(&Point);SetForegroundWindow();pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, Point.x, Point.y, this);
}
LRESULT CMainDlg::OnUmNotifyicon(WPARAM wParam, LPARAM lParam)
{switch (lParam){case WM_LBUTTONDBLCLK:ShowWindow(SW_SHOWNORMAL);//显示窗口主体break;case WM_RBUTTONDOWN://点击右键ShowMenu();break;default:break;}return 0;
}
进程的启动
启动进程方法:
a)system:只用于DOS程序(控制台程序)
b)WinExec:早期windows简单进程启动函数
c)ShellExecute(推荐):网页链接、文档和执行文件
d)CreateProcess
可参照下方链接:https://blog.csdn.net/weixin_34378969/article/details/92450910
void CMainDlg::OnFileNew()
{if(Prompt())m_edit.SetWindowText(_T(""));
}CString str =s;str += _T(" C:\\Finish.log");WinExec(_bstr_t(str), SW_SHOWNORMAL); //早期WindowsShellExecute(NULL, _T("open"), _T("C:\\Finish.log"), NULL, NULL, SW_SHOWNORMAL); //打开文档
ShellExecute(NULL, _T("open"), _T("http://www.4399.com"), NULL, NULL, SW_SHOWNORMAL);//打开链接
ShellExecute(NULL, _T("open"), _T("mspaint.exe"), _T("F:\\2023\\MFC开发\\6、MFC智能工业开发第21天\\MFC对话框的退出过程.png"), NULL, SW_SHOWNORMAL); //打开执行文件
void CMainDlg::OnFileNewWindow()
{TCHAR s[MAX_PATH];if (GetModuleFileName(theApp.m_hInstance, s, _countof(s)) <= 0){AfxMessageBox(_T("新建窗口失败"));return;}ShellExecute(m_hWnd, _T("open"), s, NULL, NULL, SW_SHOWNORMAL);//_tsystem(s); //建立新进程时会带有dos窗口 仅执行文件
}
附录1:启动线程方式
a)beginthread和beginthreadex:
b)API:CreateThread:类似于fopen的底层CreateFile,beginthread的底层也是CreateThread
c)AfxBeginThread好像也是重新改写一下参数,还是调用CreateThread底层API
d)MFC还有一个CWinThread类也可以启动线程。
CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
);
HANDLE WINAPI CreateThread(_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,_In_ SIZE_T dwStackSize,_In_ LPTHREAD_START_ROUTINE lpStartAddress,_In_opt_ LPVOID lpParameter,_In_ DWORD dwCreationFlags,_Out_opt_ LPDWORD lpThreadId
);