CCommonDialog
通用對話框 CCommonDialog
這些對話框類封裝 Windows 公共對話框。 它們提供了易于使用的復(fù)雜對話框?qū)崿F(xiàn)。
CFileDialog
提供用于打開或保存文件的標(biāo)準(zhǔn)對話框。
CColorDialog
提供用于選擇顏色的標(biāo)準(zhǔn)對話框。
CFontDialog
提供用于選擇字體的標(biāo)準(zhǔn)對話框。
CFindReplaceDialog
為搜索和替換操作提供標(biāo)準(zhǔn)對話框。
CPrintDialog
提供用于打印文件的標(biāo)準(zhǔn)對話框。
CPrintDialogEx
提供 Windows 打印屬性表。
CPageSetupDialog
顯示和配置頁面設(shè)置的標(biāo)準(zhǔn)對話框
CFileDialog
其中較為常用的是CFileDialog的構(gòu)造函數(shù)以及GetPathName()
GetPathName()是CFileDialog類的一個(gè)成員函數(shù),用于獲取用戶選擇的文件的完整路徑名。當(dāng)用戶在打開或保存對話框中選擇了一個(gè)文件后,可以通過調(diào)用GetPathName()函數(shù)來獲取該文件的完整路徑名。該函數(shù)的返回值是一個(gè)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;
// Constructors
explicit CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR 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();
// Operations
virtual 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 set
CString GetPathName() const; // return full path and filename
CString GetFileName() const; // return only filename
CString GetFileExt() const; // return only ext
CString GetFileTitle() const; // return file title
BOOL GetReadOnlyPref() const; // return TRUE if readonly checked
// Enumerating multiple file selections
POSITION GetStartPosition() const;
CString GetNextPathName(POSITION& pos) const;
// Helpers for custom templates
void SetTemplate(UINT nWin3ID, UINT nWin4ID);
void SetTemplate(LPCTSTR lpWin3ID, LPCTSTR lpWin4ID);
// Other operations available while the dialog is visible
CString GetFolderPath() const; // return full path
void 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 save
CString m_strFilter; // filter string
// separate fields with '|', terminate with '||\0'
TCHAR m_szFileTitle[_MAX_FNAME]; // contains file title after return
TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return
OPENFILENAME* m_pofnTemp;
};
下面對其構(gòu)造函數(shù)參數(shù)詳解與CFileDialog的相應(yīng)屬性
explicit CFileDialog(
BOOL bOpenFileDialog, //指定什么類型對話框去創(chuàng)建
LPCTSTR lpszDefExt = NULL, //指定默認(rèn)的文件擴(kuò)展名??梢詾镹ULL,表示沒有默認(rèn)擴(kuò)展名。
LPCTSTR lpszFileName = NULL, //指定默認(rèn)的文件名??梢詾镹ULL,表示沒有默認(rèn)文件名
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, //指定對話框的標(biāo)志
LPCTSTR lpszFilter = NULL, //指定文件過濾器。用于過濾文件類型,使用戶只能選擇指定類型的文件
CWnd* pParentWnd = NULL, //指定父窗口??梢詾镹ULL,表示使用默認(rèn)的父窗口
DWORD dwSize = 0, //指定對話框的大小??梢詾?,表示使用默認(rèn)大小。
BOOL bVistaStyle = TRUE //指定對話框的樣式是否為Vista風(fēng)格。如果為TRUE,表示使用Vista風(fēng)格;如果為FALSE,表示使用舊的樣式。
);
Set it to TRUE to construct a File Open dialog box. 代入這個(gè)參數(shù)(TRUE)去構(gòu)建文件打開的對話框。
Set it to FALSE to construct a File Save As dialog box.代入FALSE去構(gòu)建一個(gè)另存為對話框。
#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中的一個(gè)類,用于創(chuàng)建和操作單行或多行的文本框控件。CEdit類繼承自CWnd類,并提供了一系列函數(shù)和屬性來管理文本框的內(nèi)容、樣式和行為。
通過CEdit類,你可以創(chuàng)建一個(gè)文本框控件,并對其進(jìn)行各種操作,如設(shè)置文本內(nèi)容、獲取文本內(nèi)容、設(shè)置文本樣式、處理文本框消息等。
// 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; //被修改過的標(biāo)識
void SetModify(BOOL bModified = TRUE); //清理或者設(shè)置修改標(biāo)識
#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)
// Attributes
int GetLineCount() const; //獲取文本框中的行數(shù)
void GetRect(LPRECT lpRect) const; //獲取矩形
DWORD GetSel() const; //獲取選中文字的位置
void GetSel(int& nStartChar, int& nEndChar) const;
void SetMargins(UINT nLeft, UINT nRight); //設(shè)置邊欄
DWORD GetMargins() const;
void SetLimitText(UINT nMax);//設(shè)置編輯框最大文字?jǐn)?shù)量
UINT GetLimitText() const;
CPoint PosFromChar(UINT nChar) const; //坐標(biāo)與字符索引的映射
int CharFromPos(CPoint pt) const;//相反的映射
int LineFromChar(int nIndex = -1) const; //行數(shù)和字符索引的映射
int LineIndex(int nLine = -1) const;//反向映射
int LineLength(int nLine = -1) const; //獲取第幾行的長度
void LineScroll(int nLines, int nChars = 0);//滾動多少行,負(fù)數(shù)向上,正數(shù)向下
int GetLine(_In_ int nIndex, _Out_ LPTSTR lpszBuffer) const; //獲取第幾行文字(\r\n)
// NOTE: may not return null character
int GetLine (int nIndex, LPTSTR lpszBuffer,int nMaxLength) const; //有緩沖區(qū)邊界限制
// Operations
BOOL FmtLines(BOOL bAddEOL);
void LimitText(int nChars = 0);
void ReplaceSel(LPCTSTR lpszNewText, BOOL bCanUndo = FALSE);
void SetPasswordChar(TCHAR ch); //設(shè)置密碼
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 operations
BOOL CanUndo() const;
void EmptyUndoBuffer(); //清空Undo的內(nèi)容
BOOL Undo();
void Clear();
void Copy();
void Cut();
void Paste();
BOOL SetReadOnly(BOOL bReadOnly = TRUE);
int GetFirstVisibleLine() const;
TCHAR GetPasswordChar() const;
};
托盤技術(shù)
可參照下方鏈接: https://blog.csdn.net/japhydream/article/details/5062635
在主對話框的頭文件添加相應(yīng)消息
enum { UM_NOTIFYICON = WM_USER +888 };
初始化對話框時(shí)添加
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);
對話框關(guān)閉時(shí)添加刪除
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);
對對應(yīng)消息的進(jìn)行處理
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://點(diǎn)擊右鍵
ShowMenu();
break;
default:
break;
}
return 0;
}
進(jìn)程的啟動
啟動進(jìn)程方法:
a)system:只用于DOS程序(控制臺程序)
b)WinExec:早期windows簡單進(jìn)程啟動函數(shù)
c)ShellExecute(推薦):網(wǎng)頁鏈接、文檔和執(zhí)行文件
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); //早期Windows
ShellExecute(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開發(fā)\\6、MFC智能工業(yè)開發(fā)第21天\\MFC對話框的退出過程.png"), NULL, SW_SHOWNORMAL); //打開執(zhí)行文件
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); //建立新進(jìn)程時(shí)會帶有dos窗口 僅執(zhí)行文件
}
附錄1:啟動線程方式
a)beginthread和beginthreadex:
b)API:CreateThread:類似于fopen的底層CreateFile,beginthread的底層也是CreateThread
c)AfxBeginThread好像也是重新改寫一下參數(shù),還是調(diào)用CreateThread底層API
d)MFC還有一個(gè)CWinThread類也可以啟動線程。文章來源:http://www.zghlxwxcb.cn/news/detail-570685.html
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
);
附錄2:MFC對話框的退出過程
文章來源地址http://www.zghlxwxcb.cn/news/detail-570685.html
到了這里,關(guān)于MFC第十六天 CFileDialog、CEdit簡介、(線程)進(jìn)程的啟動,以及Notepad的開發(fā)(托盤技術(shù)-->菜單功能)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!