📄 atldlgs.h
字号:
// Windows Template Library - WTL version 7.1
// Copyright (C) 1997-2003 Microsoft Corporation
// All rights reserved.
//
// This file is a part of the Windows Template Library.
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
#ifndef __ATLDLGS_H__
#define __ATLDLGS_H__
#pragma once
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLAPP_H__
#error atldlgs.h requires atlapp.h to be included first
#endif
#ifndef __ATLWIN_H__
#error atldlgs.h requires atlwin.h to be included first
#endif
#include <commdlg.h>
#include <shlobj.h>
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CFileDialogImpl<T>
// CFileDialog
// CFolderDialogImpl<T>
// CFolderDialog
// CFontDialogImpl<T>
// CFontDialog
// CRichEditFontDialogImpl<T>
// CRichEditFontDialog
// CColorDialogImpl<T>
// CColorDialog
// CPrintDialogImpl<T>
// CPrintDialog
// CPrintDialogExImpl<T>
// CPrintDialogEx
// CPageSetupDialogImpl<T>
// CPageSetupDialog
// CFindReplaceDialogImpl<T>
// CFindReplaceDialog
//
// CPropertySheetWindow
// CPropertySheetImpl<T, TBase>
// CPropertySheet
// CPropertyPageWindow
// CPropertyPageImpl<T, TBase>
// CPropertyPage<t_wDlgTemplateID>
// CAxPropertyPageImpl<T, TBase>
// CAxPropertyPage<t_wDlgTemplateID>
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// CFileDialogImpl - used for File Open or File Save As
// compatibility with the old (vc6.0) headers
#if (_WIN32_WINNT >= 0x0500) && !defined(OPENFILENAME_SIZE_VERSION_400)
#ifndef CDSIZEOF_STRUCT
#define CDSIZEOF_STRUCT(structname, member) (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
#endif
#define OPENFILENAME_SIZE_VERSION_400A CDSIZEOF_STRUCT(OPENFILENAMEA,lpTemplateName)
#define OPENFILENAME_SIZE_VERSION_400W CDSIZEOF_STRUCT(OPENFILENAMEW,lpTemplateName)
#ifdef UNICODE
#define OPENFILENAME_SIZE_VERSION_400 OPENFILENAME_SIZE_VERSION_400W
#else
#define OPENFILENAME_SIZE_VERSION_400 OPENFILENAME_SIZE_VERSION_400A
#endif // !UNICODE
#endif // (_WIN32_WINNT >= 0x0500) && !defined(OPENFILENAME_SIZE_VERSION_400)
template <class T>
class ATL_NO_VTABLE CFileDialogImpl : public ATL::CDialogImplBase
{
public:
OPENFILENAME m_ofn;
BOOL m_bOpenFileDialog; // TRUE for file open, FALSE for file save
TCHAR m_szFileTitle[_MAX_FNAME]; // contains file title after return
TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return
CFileDialogImpl(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
HWND hWndParent = NULL)
{
memset(&m_ofn, 0, sizeof(m_ofn)); // initialize structure to 0/NULL
m_szFileName[0] = '\0';
m_szFileTitle[0] = '\0';
m_bOpenFileDialog = bOpenFileDialog;
m_ofn.lStructSize = sizeof(m_ofn);
#if (_WIN32_WINNT >= 0x0500)
// adjust struct size if running on older version of Windows
if(AtlIsOldWindows())
{
ATLASSERT(sizeof(m_ofn) > OPENFILENAME_SIZE_VERSION_400); // must be
m_ofn.lStructSize = OPENFILENAME_SIZE_VERSION_400;
}
#endif //(_WIN32_WINNT >= 0x0500)
m_ofn.lpstrFile = m_szFileName;
m_ofn.nMaxFile = _MAX_PATH;
m_ofn.lpstrDefExt = lpszDefExt;
m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
m_ofn.nMaxFileTitle = _MAX_FNAME;
#ifndef _WIN32_WCE
m_ofn.Flags = dwFlags | OFN_EXPLORER | OFN_ENABLEHOOK | OFN_ENABLESIZING;
#else // CE specific
m_ofn.Flags = dwFlags | OFN_EXPLORER | OFN_ENABLEHOOK;
#endif //!_WIN32_WCE
m_ofn.lpstrFilter = lpszFilter;
#if (_ATL_VER >= 0x0700)
m_ofn.hInstance = ATL::_AtlBaseModule.GetResourceInstance();
#else //!(_ATL_VER >= 0x0700)
m_ofn.hInstance = _Module.GetResourceInstance();
#endif //!(_ATL_VER >= 0x0700)
m_ofn.lpfnHook = (LPOFNHOOKPROC)T::StartDialogProc;
m_ofn.hwndOwner = hWndParent;
// setup initial file name
if(lpszFileName != NULL)
lstrcpyn(m_szFileName, lpszFileName, _MAX_PATH);
}
INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
{
ATLASSERT((m_ofn.Flags & OFN_ENABLEHOOK) != 0);
ATLASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
if(m_ofn.hwndOwner == NULL) // set only if not specified before
m_ofn.hwndOwner = hWndParent;
ATLASSERT(m_hWnd == NULL);
#if (_ATL_VER >= 0x0700)
ATL::_AtlWinModule.AddCreateWndData(&m_thunk.cd, (ATL::CDialogImplBase*)this);
#else //!(_ATL_VER >= 0x0700)
_Module.AddCreateWndData(&m_thunk.cd, (ATL::CDialogImplBase*)this);
#endif //!(_ATL_VER >= 0x0700)
BOOL bRet;
if(m_bOpenFileDialog)
bRet = ::GetOpenFileName(&m_ofn);
else
bRet = ::GetSaveFileName(&m_ofn);
m_hWnd = NULL;
return bRet ? IDOK : IDCANCEL;
}
// Attributes
ATL::CWindow GetFileDialogWindow() const
{
ATLASSERT(::IsWindow(m_hWnd));
return ATL::CWindow(GetParent());
}
int GetFilePath(LPTSTR lpstrFilePath, int nLength) const
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
return (int)GetFileDialogWindow().SendMessage(CDM_GETFILEPATH, nLength, (LPARAM)lpstrFilePath);
}
int GetFolderIDList(LPVOID lpBuff, int nLength) const
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
return (int)GetFileDialogWindow().SendMessage(CDM_GETFOLDERIDLIST, nLength, (LPARAM)lpBuff);
}
int GetFolderPath(LPTSTR lpstrFolderPath, int nLength) const
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
return (int)GetFileDialogWindow().SendMessage(CDM_GETFOLDERPATH, nLength, (LPARAM)lpstrFolderPath);
}
int GetSpec(LPTSTR lpstrSpec, int nLength) const
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
return (int)GetFileDialogWindow().SendMessage(CDM_GETSPEC, nLength, (LPARAM)lpstrSpec);
}
void SetControlText(int nCtrlID, LPCTSTR lpstrText)
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
GetFileDialogWindow().SendMessage(CDM_SETCONTROLTEXT, nCtrlID, (LPARAM)lpstrText);
}
void SetDefExt(LPCTSTR lpstrExt)
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
GetFileDialogWindow().SendMessage(CDM_SETDEFEXT, 0, (LPARAM)lpstrExt);
}
BOOL GetReadOnlyPref() const // return TRUE if readonly checked
{
return ((m_ofn.Flags & OFN_READONLY) != 0) ? TRUE : FALSE;
}
// Operations
void HideControl(int nCtrlID)
{
ATLASSERT(::IsWindow(m_hWnd));
ATLASSERT((m_ofn.Flags & OFN_EXPLORER) != 0);
GetFileDialogWindow().SendMessage(CDM_HIDECONTROL, nCtrlID);
}
// Special override for common dialogs
BOOL EndDialog(INT_PTR /*nRetCode*/ = 0)
{
ATLASSERT(::IsWindow(m_hWnd));
GetFileDialogWindow().SendMessage(WM_COMMAND, MAKEWPARAM(IDCANCEL, 0));
return TRUE;
}
// Message map and handlers
BEGIN_MSG_MAP(CFileDialogImpl)
NOTIFY_CODE_HANDLER(CDN_FILEOK, _OnFileOK)
NOTIFY_CODE_HANDLER(CDN_FOLDERCHANGE, _OnFolderChange)
NOTIFY_CODE_HANDLER(CDN_HELP, _OnHelp)
NOTIFY_CODE_HANDLER(CDN_INITDONE, _OnInitDone)
NOTIFY_CODE_HANDLER(CDN_SELCHANGE, _OnSelChange)
NOTIFY_CODE_HANDLER(CDN_SHAREVIOLATION, _OnShareViolation)
NOTIFY_CODE_HANDLER(CDN_TYPECHANGE, _OnTypeChange)
END_MSG_MAP()
LRESULT _OnFileOK(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
ATLASSERT(::IsWindow(m_hWnd));
T* pT = static_cast<T*>(this);
return !pT->OnFileOK((LPOFNOTIFY)pnmh);
}
LRESULT _OnFolderChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
ATLASSERT(::IsWindow(m_hWnd));
T* pT = static_cast<T*>(this);
pT->OnFolderChange((LPOFNOTIFY)pnmh);
return 0;
}
LRESULT _OnHelp(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
ATLASSERT(::IsWindow(m_hWnd));
T* pT = static_cast<T*>(this);
pT->OnHelp((LPOFNOTIFY)pnmh);
return 0;
}
LRESULT _OnInitDone(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
ATLASSERT(::IsWindow(m_hWnd));
T* pT = static_cast<T*>(this);
pT->OnInitDone((LPOFNOTIFY)pnmh);
return 0;
}
LRESULT _OnSelChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
ATLASSERT(::IsWindow(m_hWnd));
T* pT = static_cast<T*>(this);
pT->OnSelChange((LPOFNOTIFY)pnmh);
return 0;
}
LRESULT _OnShareViolation(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
ATLASSERT(::IsWindow(m_hWnd));
T* pT = static_cast<T*>(this);
return pT->OnShareViolation((LPOFNOTIFY)pnmh);
}
LRESULT _OnTypeChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
ATLASSERT(::IsWindow(m_hWnd));
T* pT = static_cast<T*>(this);
pT->OnTypeChange((LPOFNOTIFY)pnmh);
return 0;
}
// Overrideables
BOOL OnFileOK(LPOFNOTIFY /*lpon*/)
{
return TRUE;
}
void OnFolderChange(LPOFNOTIFY /*lpon*/)
{
}
void OnHelp(LPOFNOTIFY /*lpon*/)
{
}
void OnInitDone(LPOFNOTIFY /*lpon*/)
{
}
void OnSelChange(LPOFNOTIFY /*lpon*/)
{
}
int OnShareViolation(LPOFNOTIFY /*lpon*/)
{
return 0;
}
void OnTypeChange(LPOFNOTIFY /*lpon*/)
{
}
};
class CFileDialog : public CFileDialogImpl<CFileDialog>
{
public:
CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
HWND hWndParent = NULL)
: CFileDialogImpl<CFileDialog>(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, hWndParent)
{ }
// override base class map and references to handlers
DECLARE_EMPTY_MSG_MAP()
};
///////////////////////////////////////////////////////////////////////////////
// CFolderDialogImpl - used for browsing for a folder
#ifndef _WIN32_WCE
template <class T>
class ATL_NO_VTABLE CFolderDialogImpl
{
public:
BROWSEINFO m_bi;
TCHAR m_szFolderDisplayName[MAX_PATH];
TCHAR m_szFolderPath[MAX_PATH];
HWND m_hWnd; // used only in the callback function
// Constructor
CFolderDialogImpl(HWND hWndParent = NULL, LPCTSTR lpstrTitle = NULL, UINT uFlags = BIF_RETURNONLYFSDIRS)
{
memset(&m_bi, 0, sizeof(m_bi)); // initialize structure to 0/NULL
m_bi.hwndOwner = hWndParent;
m_bi.pidlRoot = NULL;
m_bi.pszDisplayName = m_szFolderDisplayName;
m_bi.lpszTitle = lpstrTitle;
m_bi.ulFlags = uFlags;
m_bi.lpfn = BrowseCallbackProc;
m_bi.lParam = (LPARAM)static_cast<T*>(this);
m_szFolderPath[0] = 0;
m_szFolderDisplayName[0] = 0;
m_hWnd = NULL;
}
// Operations
INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
{
if(m_bi.hwndOwner == NULL) // set only if not specified before
m_bi.hwndOwner = hWndParent;
INT_PTR nRet = -1;
LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&m_bi);
if(pItemIDList != NULL)
{
if(::SHGetPathFromIDList(pItemIDList, m_szFolderPath))
{
IMalloc* pMalloc = NULL;
if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
{
pMalloc->Free(pItemIDList);
pMalloc->Release();
}
nRet = IDOK;
}
else
{
nRet = IDCANCEL;
}
}
return nRet;
}
// filled after a call to DoModal
LPCTSTR GetFolderPath() const
{
return m_szFolderPath;
}
LPCTSTR GetFolderDisplayName() const
{
return m_szFolderDisplayName;
}
int GetFolderImageIndex() const
{
return m_bi.iImage;
}
// Callback function and overrideables
static int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
#ifndef BFFM_VALIDATEFAILED
#ifdef UNICODE
const int BFFM_VALIDATEFAILED = 4;
#else
const int BFFM_VALIDATEFAILED = 3;
#endif
#endif //!BFFM_VALIDATEFAILED
int nRet = 0;
T* pT = (T*)lpData;
bool bClear = false;
if(pT->m_hWnd == NULL)
{
pT->m_hWnd = hWnd;
bClear = true;
}
else
{
ATLASSERT(pT->m_hWnd == hWnd);
}
switch(uMsg)
{
case BFFM_INITIALIZED:
pT->OnInitialized();
break;
case BFFM_SELCHANGED:
pT->OnSelChanged((LPITEMIDLIST)lParam);
break;
case BFFM_VALIDATEFAILED:
nRet = pT->OnValidateFailed((LPCTSTR)lParam);
break;
default:
ATLTRACE2(atlTraceUI, 0, _T("Unknown message received in CFolderDialogImpl::BrowseCallbackProc\n"));
break;
}
if(bClear)
pT->m_hWnd = NULL;
return nRet;
}
void OnInitialized()
{
}
void OnSelChanged(LPITEMIDLIST /*pItemIDList*/)
{
}
int OnValidateFailed(LPCTSTR /*lpstrFolderPath*/)
{
return 1; // 1=continue, 0=EndDialog
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -