bokee.net

电子/电气工程师博客

正文 更多文章

学习CFileDialog类的微软代码

    学习CFileDialog类的微软代码
            王佰营  徐丽红
    wbymcs51.blog.bokee.net
  在VC编程中,要用好一个类,就必须对这个类深入了解;看到许多朋友
问CFileDialog类的一些用法,现将该类的一些代码整理如下,以便参考;
这样有利于借鉴和使用。
一、OPENFILENAMEA结构定义

typedef struct tagOFNW {

   DWORD        lStructSize;

   HWND         hwndOwner;

   HINSTANCE    hInstance;

   LPCWSTR      lpstrFilter;

   LPWSTR       lpstrCustomFilter;

   DWORD        nMaxCustFilter;

   DWORD        nFilterIndex;

   LPWSTR       lpstrFile;

   DWORD        nMaxFile;

   LPWSTR       lpstrFileTitle;

   DWORD        nMaxFileTitle;

   LPCWSTR      lpstrInitialDir;

   LPCWSTR      lpstrTitle;

   DWORD        Flags;

   WORD         nFileOffset;

   WORD         nFileExtension;

   LPCWSTR      lpstrDefExt;

   LPARAM       lCustData;

   LPOFNHOOKPROC lpfnHook;

   LPCWSTR      lpTemplateName;

#ifdef _MAC

   LPEDITMENU   lpEditInfo;

   LPCSTR       lpstrPrompt;

#endif

} OPENFILENAMEW, *LPOPENFILENAMEW;

#ifdef UNICODE

typedef OPENFILENAMEW OPENFILENAME;

typedef LPOPENFILENAMEW LPOPENFILENAME;

#undef AFX_DATA
#define AFX_DATA AFX_CORE_DATA

二、CCommonDialog类定义

// CCommonDialog - base class for all common dialogs

#ifdef _AFXDLL
class CCommonDialog : public CDialog
#else
class AFX_NOVTABLE CCommonDialog : public CDialog
#endif
{
public:
 CCommonDialog(CWnd* pParentWnd);

// Implementation
protected:
 virtual void OnOK();
 virtual void OnCancel();

 //{{AFX_MSG(CCommonDialog)
 afx_msg BOOL OnHelpInfo(HELPINFO*);
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

三、CcommonDialog类实现

#include "stdafx.h"
#include <dlgs.h>

#ifdef AFX_AUX_SEG
#pragma code_seg(AFX_AUX_SEG)
#endif

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define new DEBUG_NEW

AFX_COMDAT UINT _afxMsgLBSELCHANGE = 0;
AFX_COMDAT UINT _afxMsgSHAREVI = 0;
AFX_COMDAT UINT _afxMsgFILEOK = 0;
AFX_COMDAT UINT _afxMsgCOLOROK = 0;
AFX_COMDAT UINT _afxMsgHELP = 0;
AFX_COMDAT UINT _afxMsgSETRGB = 0;

BEGIN_MESSAGE_MAP(CCommonDialog, CDialog)
 //{{AFX_MSG_MAP(CCommonDialog)
 ON_WM_HELPINFO()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

UINT CALLBACK
_AfxCommDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 if (hWnd == NULL)
  return 0;

 _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
 if (pThreadState->m_pAlternateWndInit != NULL)
 {
  ASSERT_KINDOF(CFileDialog,pThreadState->m_pAlternateWndInit);
  pThreadState->m_pAlternateWndInit->SubclassWindow(hWnd);
  pThreadState->m_pAlternateWndInit = NULL;
 }
 ASSERT(pThreadState->m_pAlternateWndInit == NULL);

 if (message == WM_INITDIALOG)
 {
  _afxMsgLBSELCHANGE = ::RegisterWindowMessage(LBSELCHSTRING);
  _afxMsgSHAREVI = ::RegisterWindowMessage(SHAREVISTRING);
  _afxMsgFILEOK = ::RegisterWindowMessage(FILEOKSTRING);
  _afxMsgCOLOROK = ::RegisterWindowMessage(COLOROKSTRING);
  _afxMsgHELP = ::RegisterWindowMessage(HELPMSGSTRING);
  _afxMsgSETRGB = ::RegisterWindowMessage(SETRGBSTRING);
  return (UINT)AfxDlgProc(hWnd, message, wParam, lParam);
 }

 if (message == _afxMsgHELP ||
    (message == WM_COMMAND && LOWORD(wParam) == pshHelp))
 {
  // just translate the message into the AFX standard help command.
  SendMessage(hWnd, WM_COMMAND, ID_HELP, 0);
  return 1;
 }

 if (message < 0xC000)
 {
  // not a ::RegisterWindowMessage message
  return 0;
 }

 // assume it is already wired up to a permanent one
 CDialog* pDlg = (CDialog*)CWnd::FromHandlePermanent(hWnd);
 ASSERT(pDlg != NULL);
 ASSERT_KINDOF(CDialog, pDlg);

 if (pDlg->IsKindOf(RUNTIME_CLASS(CFileDialog)))
 {
  // If we’re exploring then we are not interested in the Registered messages
  if (((CFileDialog*)pDlg)->m_ofn.Flags & OFN_EXPLORER)
   return 0;
 }

 // RegisterWindowMessage - does not copy to lastState buffer, so
 // CWnd::GetCurrentMessage and CWnd::Default will NOT work
 // while in these handlers

 // Dispatch special commdlg messages through our virtual callbacks
 if (message == _afxMsgSHAREVI)
 {
  ASSERT_KINDOF(CFileDialog, pDlg);
  return ((CFileDialog*)pDlg)->OnShareViolation((LPCTSTR)lParam);
 }
 else if (message == _afxMsgFILEOK)
 {
  ASSERT_KINDOF(CFileDialog, pDlg);

  if (afxData.bWin4)
   ((CFileDialog*)pDlg)->m_pofnTemp = (OPENFILENAME*)lParam;

  BOOL bResult = ((CFileDialog*)pDlg)->OnFileNameOK();

  ((CFileDialog*)pDlg)->m_pofnTemp = NULL;

  return bResult;
 }
 else if (message == _afxMsgLBSELCHANGE)
 {
  ASSERT_KINDOF(CFileDialog, pDlg);
  ((CFileDialog*)pDlg)->OnLBSelChangedNotify(wParam, LOWORD(lParam),
    HIWORD(lParam));
  return 0;
 }
 else if (message == _afxMsgCOLOROK)
 {
  ASSERT_KINDOF(CColorDialog, pDlg);
  return ((CColorDialog*)pDlg)->OnColorOK();
 }
 else if (message == _afxMsgSETRGB)
 {
  // nothing to do here, since this is a SendMessage
  return 0;
 }
 return 0;
}

// CCommonDialog - common dialog helper class

void CCommonDialog::OnOK()
{
 ASSERT_VALID(this);

 if (!UpdateData(TRUE))
 {
  TRACE0("UpdateData failed during dialog termination.\n");
  // the UpdateData routine will set focus to correct item
  return;
 }

 // Common dialogs do not require ::EndDialog
 Default();
}

void CCommonDialog::OnCancel()
{
 ASSERT_VALID(this);

 // Common dialogs do not require ::EndDialog
 Default();
}

BOOL CCommonDialog::OnHelpInfo(HELPINFO*)
{
 return Default();
}

四、CFileDialog类定义
// CFileDialog - used for FileOpen... or FileSaveAs...

class CFileDialog : public CCommonDialog
{
 DECLARE_DYNAMIC(CFileDialog)

public:
// Attributes
 OPENFILENAME m_ofn; // open file parameter block

// Constructors
 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);

// Operations
 virtual int DoModal();

 // 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, LPCSTR lpsz);
 void HideControl(int nID);
 void SetDefExt(LPCSTR lpsz);

// Overridable callbacks
protected:
 friend UINT CALLBACK _AfxCommDlgProc(HWND, UINT, WPARAM, LPARAM);
 virtual UINT OnShareViolation(LPCTSTR lpszPathName);
 virtual BOOL OnFileNameOK();
 virtual void OnLBSelChangedNotify(UINT nIDBox, UINT iCurSel, UINT nCode);

 // only called back if OFN_EXPLORER is set
 virtual void OnInitDone();
 virtual void OnFileNameChange();
 virtual void OnFolderChange();
 virtual void OnTypeChange();

// Implementation
#ifdef _DEBUG
public:
 virtual void Dump(CDumpContext& dc) const;
#endif

protected:
 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[64];       // contains file title after return
 TCHAR m_szFileName[_MAX_PATH]; // contains full path name after return

 OPENFILENAME*  m_pofnTemp;

 virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);
};


五、CfileDialog类实现
#include "stdafx.h"
#include <dlgs.h>       // for standard control IDs for commdlg

#ifdef AFX_AUX_SEG
#pragma code_seg(AFX_AUX_SEG)
#endif

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define new DEBUG_NEW

////////////////////////////////////////////////////////////////////////////
// FileOpen/FileSaveAs common dialog helper

CFileDialog::CFileDialog(BOOL bOpenFileDialog,
 LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags,
 LPCTSTR lpszFilter, CWnd* pParentWnd) : CCommonDialog(pParentWnd)
{
 memset(&m_ofn, 0, sizeof(m_ofn)); // initialize structure to 0/NULL
 m_szFileName[0] = ’\0’;
 m_szFileTitle[0] = ’\0’;
 m_pofnTemp = NULL;

 m_bOpenFileDialog = bOpenFileDialog;
 m_nIDHelp = bOpenFileDialog ? AFX_IDD_FILEOPEN : AFX_IDD_FILESAVE;

 m_ofn.lStructSize = sizeof(m_ofn);
 m_ofn.lpstrFile = m_szFileName;
 m_ofn.nMaxFile = _countof(m_szFileName);
 m_ofn.lpstrDefExt = lpszDefExt;
 m_ofn.lpstrFileTitle = (LPTSTR)m_szFileTitle;
 m_ofn.nMaxFileTitle = _countof(m_szFileTitle);
 m_ofn.Flags |= dwFlags | OFN_ENABLEHOOK | OFN_ENABLESIZING;
 if (!afxData.bWin4 && AfxHelpEnabled())
  m_ofn.Flags |= OFN_SHOWHELP;
 if (afxData.bWin4)
 {
  m_ofn.Flags |= OFN_EXPLORER;
  m_ofn.hInstance = AfxGetResourceHandle();
 }
 m_ofn.lpfnHook = (COMMDLGPROC)_AfxCommDlgProc;

 // setup initial file name
 if (lpszFileName != NULL)
  lstrcpyn(m_szFileName, lpszFileName, _countof(m_szFileName));

 // Translate filter into commdlg format (lots of \0)
 if (lpszFilter != NULL)
 {
  m_strFilter = lpszFilter;
  LPTSTR pch = m_strFilter.GetBuffer(0); // modify the buffer in place
  // MFC delimits with ’|’ not ’\0’
  while ((pch = _tcschr(pch, ’|’)) != NULL)
   *pch++ = ’\0’;
  m_ofn.lpstrFilter = m_strFilter;
  // do not call ReleaseBuffer() since the string contains ’\0’ characters
 }
}

int CFileDialog::DoModal()
{
 ASSERT_VALID(this);
 ASSERT(m_ofn.Flags & OFN_ENABLEHOOK);
 ASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook

 // zero out the file buffer for consistent parsing later
 ASSERT(AfxIsValidAddress(m_ofn.lpstrFile, m_ofn.nMaxFile));
 DWORD nOffset = lstrlen(m_ofn.lpstrFile)+1;
 ASSERT(nOffset <= m_ofn.nMaxFile);
 memset(m_ofn.lpstrFile+nOffset, 0, (m_ofn.nMaxFile-nOffset)*sizeof(TCHAR));

 // WINBUG: This is a special case for the file open/save dialog,
 //  which sometimes pumps while it is coming up but before it has
 //  disabled the main window.
 HWND hWndFocus = ::GetFocus();
 BOOL bEnableParent = FALSE;
 m_ofn.hwndOwner = PreModal();
 AfxUnhookWindowCreate();
 if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner))
 {
  bEnableParent = TRUE;
  ::EnableWindow(m_ofn.hwndOwner, FALSE);
 }

 _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
 ASSERT(pThreadState->m_pAlternateWndInit == NULL);

 if (m_ofn.Flags & OFN_EXPLORER)
  pThreadState->m_pAlternateWndInit = this;
 else
  AfxHookWindowCreate(this);

 int nResult;
 if (m_bOpenFileDialog)
  nResult = ::GetOpenFileName(&m_ofn);
 else
  nResult = ::GetSaveFileName(&m_ofn);

 if (nResult)
  ASSERT(pThreadState->m_pAlternateWndInit == NULL);
 pThreadState->m_pAlternateWndInit = NULL;

 // WINBUG: Second part of special case for file open/save dialog.
 if (bEnableParent)
  ::EnableWindow(m_ofn.hwndOwner, TRUE);
 if (::IsWindow(hWndFocus))
  ::SetFocus(hWndFocus);

 PostModal();
 return nResult ? nResult : IDCANCEL;
}

CString CFileDialog::GetPathName() const
{
 if ((m_ofn.Flags & OFN_EXPLORER) && m_hWnd != NULL)
 {
  ASSERT(::IsWindow(m_hWnd));
  CString strResult;
  if (GetParent()->SendMessage(CDM_GETSPEC, (WPARAM)MAX_PATH,
   (LPARAM)strResult.GetBuffer(MAX_PATH)) < 0)
  {
   strResult.Empty();
  }
  else
  {
   strResult.ReleaseBuffer();
  }

  if (!strResult.IsEmpty())
  {
   if (GetParent()->SendMessage(CDM_GETFILEPATH, (WPARAM)MAX_PATH,
    (LPARAM)strResult.GetBuffer(MAX_PATH)) < 0)
    strResult.Empty();
   else
   {
    strResult.ReleaseBuffer();
    return strResult;
   }
  }
 }
 return m_ofn.lpstrFile;
}

CString CFileDialog::GetFileName() const
{
 if ((m_ofn.Flags & OFN_EXPLORER) && m_hWnd != NULL)
 {
  ASSERT(::IsWindow(m_hWnd));
  CString strResult;
  if (GetParent()->SendMessage(CDM_GETSPEC, (WPARAM)MAX_PATH,
   (LPARAM)strResult.GetBuffer(MAX_PATH)) < 0)
  {
   strResult.Empty();
  }
  else
  {
   strResult.ReleaseBuffer();
   return strResult;
  }
 }
 return m_ofn.lpstrFileTitle;
}

CString CFileDialog::GetFileExt() const
{
 if ((m_ofn.Flags & OFN_EXPLORER) && m_hWnd != NULL)
 {
  ASSERT(::IsWindow(m_hWnd));
  CString strResult;
  if (GetParent()->SendMessage(CDM_GETSPEC, (WPARAM)MAX_PATH,
   (LPARAM)strResult.GetBuffer(MAX_PATH)) < 0)
   strResult.Empty();
  else
  {
   strResult.ReleaseBuffer();
   int pos = strResult.ReverseFind(’.’);
   if (pos >= 0)
    return strResult.Right(strResult.GetLength() - pos - 1);
   strResult.Empty();
  }
  return strResult;
 }

 if (m_pofnTemp != NULL)
  if (m_pofnTemp->nFileExtension == 0)
   return &afxChNil;
  else
   return m_pofnTemp->lpstrFile + m_pofnTemp->nFileExtension;

 if (m_ofn.nFileExtension == 0)
  return &afxChNil;
 else
  return m_ofn.lpstrFile + m_ofn.nFileExtension;
}

CString CFileDialog::GetFileTitle() const
{
 CString strResult = GetFileName();
 int pos = strResult.ReverseFind(’.’);
 if (pos >= 0)
  return strResult.Left(pos);
 return strResult;
}

CString CFileDialog::GetNextPathName(POSITION& pos) const
{
 BOOL bExplorer = m_ofn.Flags & OFN_EXPLORER;
 TCHAR chDelimiter;
 if (bExplorer)
  chDelimiter = ’\0’;
 else
  chDelimiter = ’ ’;

 LPTSTR lpsz = (LPTSTR)pos;
 if (lpsz == m_ofn.lpstrFile) // first time
 {
  if ((m_ofn.Flags & OFN_ALLOWMULTISELECT) == 0)
  {
   pos = NULL;
   return m_ofn.lpstrFile;
  }

  // find char pos after first Delimiter
  while(*lpsz != chDelimiter && *lpsz != ’\0’)
   lpsz = _tcsinc(lpsz);
  lpsz = _tcsinc(lpsz);

  // if single selection then return only selection
  if (*lpsz == 0)
  {
   pos = NULL;
   return m_ofn.lpstrFile;
  }
 }

 CString strPath = m_ofn.lpstrFile;
 if (!bExplorer)
 {
  LPTSTR lpszPath = m_ofn.lpstrFile;
  while(*lpszPath != chDelimiter)
   lpszPath = _tcsinc(lpszPath);
  strPath = strPath.Left(lpszPath - m_ofn.lpstrFile);
 }

 LPTSTR lpszFileName = lpsz;
 CString strFileName = lpsz;

 // find char pos at next Delimiter
 while(*lpsz != chDelimiter && *lpsz != ’\0’)
  lpsz = _tcsinc(lpsz);

 if (!bExplorer && *lpsz == ’\0’)
  pos = NULL;
 else
 {
  if (!bExplorer)
   strFileName = strFileName.Left(lpsz - lpszFileName);

  lpsz = _tcsinc(lpsz);
  if (*lpsz == ’\0’) // if double terminated then done
   pos = NULL;
  else
   pos = (POSITION)lpsz;
 }

 // only add ’\\’ if it is needed
 if (!strPath.IsEmpty())
 {
  // check for last back-slash or forward slash (handles DBCS)
  LPCTSTR lpsz = _tcsrchr(strPath, ’\\’);
  if (lpsz == NULL)
   lpsz = _tcsrchr(strPath, ’/’);
  // if it is also the last character, then we don’t need an extra
  if (lpsz != NULL &&
   (lpsz - (LPCTSTR)strPath) == strPath.GetLength()-1)
  {
   ASSERT(*lpsz == ’\\’ || *lpsz == ’/’);
   return strPath + strFileName;
  }
 }
 return strPath + ’\\’ + strFileName;
}

void CFileDialog::SetTemplate(LPCTSTR lpWin3ID, LPCTSTR lpWin4ID)
{
 if (m_ofn.Flags & OFN_EXPLORER)
  m_ofn.lpTemplateName = lpWin4ID;
 else
  m_ofn.lpTemplateName = lpWin3ID;
 m_ofn.Flags |= OFN_ENABLETEMPLATE;
}

CString CFileDialog::GetFolderPath() const
{
 ASSERT(::IsWindow(m_hWnd));
 ASSERT(m_ofn.Flags & OFN_EXPLORER);

 CString strResult;
 if (GetParent()->SendMessage(CDM_GETFOLDERPATH, (WPARAM)MAX_PATH, (LPARAM)strResult.GetBuffer(MAX_PATH)) < 0)
  strResult.Empty();
 else
  strResult.ReleaseBuffer();
 return strResult;
}

void CFileDialog::SetControlText(int nID, LPCSTR lpsz)
{
 ASSERT(::IsWindow(m_hWnd));
 ASSERT(m_ofn.Flags & OFN_EXPLORER);
 GetParent()->SendMessage(CDM_SETCONTROLTEXT, (WPARAM)nID, (LPARAM)lpsz);
}

void CFileDialog::HideControl(int nID)
{
 ASSERT(::IsWindow(m_hWnd));
 ASSERT(m_ofn.Flags & OFN_EXPLORER);
 GetParent()->SendMessage(CDM_HIDECONTROL, (WPARAM)nID, 0);
}

void CFileDialog::SetDefExt(LPCSTR lpsz)
{
 ASSERT(::IsWindow(m_hWnd));
 ASSERT(m_ofn.Flags & OFN_EXPLORER);
 GetParent()->SendMessage(CDM_SETDEFEXT, 0, (LPARAM)lpsz);
}

UINT CFileDialog::OnShareViolation(LPCTSTR)
{
 ASSERT_VALID(this);

 // Do not call Default() if you override
 return OFN_SHAREWARN; // default
}

BOOL CFileDialog::OnFileNameOK()
{
 ASSERT_VALID(this);

 // Do not call Default() if you override
 return FALSE;
}

void CFileDialog::OnLBSelChangedNotify(UINT, UINT, UINT)
{
 ASSERT_VALID(this);

 // Do not call Default() if you override
 // no default processing needed
}

void CFileDialog::OnInitDone()
{
 ASSERT_VALID(this);
 GetParent()->CenterWindow();

 // Do not call Default() if you override
 // no default processing needed
}

void CFileDialog::OnFileNameChange()
{
 ASSERT_VALID(this);

 // Do not call Default() if you override
 // no default processing needed
}

void CFileDialog::OnFolderChange()
{
 ASSERT_VALID(this);

 // Do not call Default() if you override
 // no default processing needed
}

void CFileDialog::OnTypeChange()
{
 ASSERT_VALID(this);

 // Do not call Default() if you override
 // no default processing needed
}

BOOL CFileDialog::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
 ASSERT(pResult != NULL);

 // allow message map to override
 if (CCommonDialog::OnNotify(wParam, lParam, pResult))
  return TRUE;

 OFNOTIFY* pNotify = (OFNOTIFY*)lParam;
 switch(pNotify->hdr.code)
 {
 case CDN_INITDONE:
  OnInitDone();
  return TRUE;
 case CDN_SELCHANGE:
  OnFileNameChange();
  return TRUE;
 case CDN_FOLDERCHANGE:
  OnFolderChange();
  return TRUE;
 case CDN_SHAREVIOLATION:
  *pResult = OnShareViolation(pNotify->pszFile);
  return TRUE;
 case CDN_HELP:
  if (!SendMessage(WM_COMMAND, ID_HELP))
   SendMessage(WM_COMMANDHELP, 0, 0);
  return TRUE;
 case CDN_FILEOK:
  *pResult = OnFileNameOK();
  return TRUE;
 case CDN_TYPECHANGE:
  OnTypeChange();
  return TRUE;
 }

 return FALSE;   // not handled
}

///////////////////////////////////////////////////////////////////////
// CFileDialog diagnostics

#ifdef _DEBUG
void CFileDialog::Dump(CDumpContext& dc) const
{
 CDialog::Dump(dc);

 if (m_bOpenFileDialog)
  dc << "File open dialog";
 else
  dc << "File save dialog";
 dc << "\nm_ofn.hwndOwner = " << (UINT)m_ofn.hwndOwner;
 dc << "\nm_ofn.nFilterIndex = " << m_ofn.nFilterIndex;
 dc << "\nm_ofn.lpstrFile = " << m_ofn.lpstrFile;
 dc << "\nm_ofn.nMaxFile = " << m_ofn.nMaxFile;
 dc << "\nm_ofn.lpstrFileTitle = " << m_ofn.lpstrFileTitle;
 dc << "\nm_ofn.nMaxFileTitle = " << m_ofn.nMaxFileTitle;
 dc << "\nm_ofn.lpstrTitle = " << m_ofn.lpstrTitle;
 dc << "\nm_ofn.Flags = " << (LPVOID)m_ofn.Flags;
 dc << "\nm_ofn.lpstrDefExt = " << m_ofn.lpstrDefExt;
 dc << "\nm_ofn.nFileOffset = " << m_ofn.nFileOffset;
 dc << "\nm_ofn.nFileExtension = " << m_ofn.nFileExtension;

 dc << "\nm_ofn.lpstrFilter = ";
 LPCTSTR lpstrItem = m_ofn.lpstrFilter;
 LPTSTR lpszBreak = _T("|");

 while (lpstrItem != NULL && *lpstrItem != ’\0’)
 {
  dc << lpstrItem << lpszBreak;
  lpstrItem += lstrlen(lpstrItem) + 1;
 }
 if (lpstrItem != NULL)
  dc << lpszBreak;

 dc << "\nm_ofn.lpstrCustomFilter = ";
 lpstrItem = m_ofn.lpstrCustomFilter;
 while (lpstrItem != NULL && *lpstrItem != ’\0’)
 {
  dc << lpstrItem << lpszBreak;
  lpstrItem += lstrlen(lpstrItem) + 1;
 }
 if (lpstrItem != NULL)
  dc << lpszBreak;

 if (m_ofn.lpfnHook == (COMMDLGPROC)_AfxCommDlgProc)
  dc << "\nhook function set to standard MFC hook function";
 else
  dc << "\nhook function set to non-standard hook function";

 dc << "\n";
}
#endif //_DEBUG

#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif

IMPLEMENT_DYNAMIC(CFileDialog, CDialog)

六、CfileDialog对话框的控件ID

1.网上资料未验证

子类化CFileDialog时可能用到的控件对应的值。用PrevDlgCtrl和NextDlgCtrl遍历
得到。
Open Button
1
Cancel Button
2
file name(Edit Combo)
1152
folder(Combo)
1137
read only(Label)
1040
file extension(Combo)
1136
FolderView(ListView)
1
ToolBar
1088
补充:
FolderView的id和Open一样,是列举文件夹下文件名那个List,选择文件夹路径的下
拉框是1137。工具栏(1088,可以选择FolderView的排列方式)在NT4.0下没有,
Win2000和WinXP中才有。

 2.以下为微软的对话框资源:

Module Name:

    fileopen.dlg

Abstract:

    This module contains the resource descriptions for the Win32
    file open and save as common dialogs.

Revision History:

--*/

 

//
//  Include Files.
//

#include <shlobj.h>

 


//
//  Constant Declarations.
//

#define LBS_DISABLENOSCROLL   0x1000L

 


//
//  New File Open and Save As Dialogs.
//

NEWFILEOPENORD DIALOG DISCARDABLE  0, 0, 280, 164
STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION |
      WS_SYSMENU | DS_CONTEXTHELP | WS_CLIPCHILDREN
CAPTION "Open"
FONT 8, "MS Shell Dlg"
BEGIN
    LTEXT           "Look &in:", stc4, 7, 6, 27, 8, SS_NOTIFY
    COMBOBOX        cmb2, 36, 3, 138, 300,
                    CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS |
                    WS_VSCROLL | WS_TABSTOP

    LTEXT           "", stc1, 172, 2, 102, 17, NOT WS_GROUP | NOT WS_VISIBLE
    LISTBOX         lst1, 4, 20, 272, 85,
                    LBS_SORT | LBS_NOINTEGRALHEIGHT | LBS_MULTICOLUMN |
                    WS_HSCROLL | NOT WS_VISIBLE

    LTEXT           "File &name:", stc3, 5, 112, 48, 8, SS_NOTIFY
    EDITTEXT        edt1, 54, 111, 155, 12, ES_AUTOHSCROLL

    LTEXT           "Files of &type:", stc2, 5, 131, 48, 8, SS_NOTIFY
    COMBOBOX        cmb1, 54, 129, 155, 100,
                    CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP

    AUTOCHECKBOX    "Open as &read-only", chx1, 54, 148, 74, 10, WS_TABSTOP

    DEFPUSHBUTTON   "&Open", IDOK, 222, 110, 50, 14
    PUSHBUTTON      "Cancel", IDCANCEL, 222, 128, 50, 14
    PUSHBUTTON      "&Help", pshHelp, 222, 145, 50, 14
END

 

NEWOBJECTOPENORD DIALOG DISCARDABLE  0, 0, 280, 164
STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION |
      WS_SYSMENU | DS_CONTEXTHELP | WS_CLIPCHILDREN
CAPTION "Open"
FONT 8, "MS Shell Dlg"
BEGIN
    LTEXT           "Look &in:", stc4, 7, 6, 27, 8, SS_NOTIFY
    COMBOBOX        cmb2, 36, 3, 138, 300,
                    CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS |
                    WS_VSCROLL | WS_TABSTOP

    LTEXT           "", stc1, 172, 2, 102, 17, NOT WS_GROUP | NOT WS_VISIBLE
    LISTBOX         lst1, 4, 20, 272, 85,
                    LBS_SORT | LBS_NOINTEGRALHEIGHT | LBS_MULTICOLUMN |
                    WS_HSCROLL | NOT WS_VISIBLE

    LTEXT           "Object &name:", stc3, 5, 112, 54, 8, SS_NOTIFY
    EDITTEXT        edt1, 60, 111, 149, 12, ES_AUTOHSCROLL

    LTEXT           "Objects of &type:", stc2, 5, 131, 54, 8, SS_NOTIFY
    COMBOBOX        cmb1, 60, 129, 149, 100,
                    CBS_DROPDOWNLIST | WS_VSCROLL | WS_TABSTOP

    AUTOCHECKBOX    "Open as &read-only", chx1, 60, 148, 74, 10, WS_TABSTOP

    DEFPUSHBUTTON   "&Open", IDOK, 222, 110, 50, 14
    PUSHBUTTON      "Cancel", IDCANCEL, 222, 128, 50, 14
    PUSHBUTTON      "&Help", pshHelp, 222, 145, 50, 14
END

 


//
//  Old File Open and Save As Dialogs.
//

FILEOPENORD DIALOG LOADONCALL MOVEABLE DISCARDABLE  36, 24, 268, 134
STYLE WS_CAPTION | WS_SYSMENU | WS_POPUP | DS_MODALFRAME |
      DS_CONTEXTHELP | DS_3DLOOK
CAPTION "Open"
FONT 8, "MS Shell Dlg"
BEGIN
    LTEXT           "File &name:", stc3, 6, 6, 76, 9
    EDITTEXT        edt1, 6, 16, 90, 12,
                    ES_AUTOHSCROLL | WS_TABSTOP | ES_OEMCONVERT
    LISTBOX         lst1, 6, 32, 90, 68,
                    LBS_SORT | LBS_HASSTRINGS | LBS_NOTIFY |
                    LBS_DISABLENOSCROLL | WS_VSCROLL |
                    WS_TABSTOP | LBS_OWNERDRAWFIXED

    LTEXT           "&Folders:", -1, 110, 6, 96, 9
    LTEXT           "", stc1, 110, 16, 96, 9, SS_NOPREFIX
    LISTBOX         lst2, 110, 32, 96, 68,
                    LBS_SORT | LBS_HASSTRINGS | LBS_NOTIFY |
                    LBS_DISABLENOSCROLL | WS_VSCROLL |
                    WS_TABSTOP | LBS_OWNERDRAWFIXED

    LTEXT           "List files of &type:", stc2, 6, 104, 90, 9
    COMBOBOX        cmb1, 6, 114, 90, 96,
                    CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP

    LTEXT           "Dri&ves:", stc4, 110, 104, 96, 9
    COMBOBOX        cmb2, 110, 114, 96, 68,
                    CBS_SORT | CBS_HASSTRINGS | CBS_OWNERDRAWFIXED |
                    CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP

    DEFPUSHBUTTON   "OK", IDOK, 212, 6, 50, 14, WS_GROUP
    PUSHBUTTON      "Cancel", IDCANCEL, 212, 24, 50, 14, WS_GROUP

    PUSHBUTTON      "&Help", pshHelp, 212, 46, 50, 14, WS_GROUP
    AUTOCHECKBOX    "&Read only", chx1, 212, 68, 50, 12, WS_TABSTOP | WS_GROUP
END

 

MULTIFILEOPENORD DIALOG LOADONCALL MOVEABLE DISCARDABLE  36, 24, 268, 134
STYLE WS_CAPTION | WS_SYSMENU | WS_POPUP | DS_MODALFRAME |
      DS_CONTEXTHELP | DS_3DLOOK
CAPTION "Open"
FONT 8, "MS Shell Dlg"
BEGIN
    LTEXT           "File &name:", stc3, 6, 6, 76, 9
    EDITTEXT        edt1, 6, 16, 90, 12,
                    ES_AUTOHSCROLL | WS_TABSTOP | ES_OEMCONVERT
    LISTBOX         lst1, 6, 32, 90, 68,
                    LBS_SORT | LBS_NOTIFY | LBS_DISABLENOSCROLL | WS_VSCROLL |
                    WS_TABSTOP | LBS_EXTENDEDSEL | LBS_HASSTRINGS |
                    LBS_OWNERDRAWFIXED

    LTEXT           "&Folders:", -1, 110, 6, 96, 9
    LTEXT           "", stc1, 110, 16, 96, 9, SS_NOPREFIX
    LISTBOX         lst2, 110, 32, 96, 68,
                    LBS_SORT | LBS_HASSTRINGS | LBS_NOTIFY |
                    LBS_DISABLENOSCROLL | WS_VSCROLL |
                    WS_TABSTOP | LBS_OWNERDRAWFIXED

    LTEXT           "List files of &type:", stc2, 6, 104, 90, 9
    COMBOBOX        cmb1, 6, 114, 90, 96,
                    CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP

    LTEXT           "Dri&ves:", stc4, 110, 104, 96, 9
    COMBOBOX        cmb2, 110, 114, 96, 68,
                    CBS_SORT | CBS_HASSTRINGS | CBS_OWNERDRAWFIXED |
                    CBS_DROPDOWNLIST | CBS_AUTOHSCROLL | WS_VSCROLL | WS_TABSTOP

    DEFPUSHBUTTON   "OK", IDOK, 212, 6, 50, 14, WS_GROUP
    PUSHBUTTON      "Cancel", IDCANCEL, 212, 24, 50, 14, WS_GROUP

    PUSHBUTTON      "&Help", pshHelp, 212, 46, 50, 14, WS_GROUP
    AUTOCHECKBOX    "&Read only", chx1, 212, 68, 50, 12, WS_TABSTOP | WS_GROUP
END

3.微软ID号定义

/*++

Copyright (c) 1990-1997,  Microsoft Corporation  All rights reserved.

Module Name:

    dlgs.h

Abstract:

    This module contains the UI dialog header information.

--*/


#ifndef _DLGSH_INCLUDED_
#define _DLGSH_INCLUDED_


//
//  Constant Declarations.
//

#define ctlFirst    0x0400
#define ctlLast     0x04ff

//
//  Push buttons.
//
#define psh1        0x0400
#define psh2        0x0401
#define psh3        0x0402
#define psh4        0x0403
#define psh5        0x0404
#define psh6        0x0405
#define psh7        0x0406
#define psh8        0x0407
#define psh9        0x0408
#define psh10       0x0409
#define psh11       0x040a
#define psh12       0x040b
#define psh13       0x040c
#define psh14       0x040d
#define psh15       0x040e
#define pshHelp     psh15
#define psh16       0x040f

//
//  Checkboxes.
//
#define chx1        0x0410
#define chx2        0x0411
#define chx3        0x0412
#define chx4        0x0413
#define chx5        0x0414
#define chx6        0x0415
#define chx7        0x0416
#define chx8        0x0417
#define chx9        0x0418
#define chx10       0x0419
#define chx11       0x041a
#define chx12       0x041b
#define chx13       0x041c
#define chx14       0x041d
#define chx15       0x041e
#define chx16       0x041f

//
//  Radio buttons.
//
#define rad1        0x0420
#define rad2        0x0421
#define rad3        0x0422
#define rad4        0x0423
#define rad5        0x0424
#define rad6        0x0425
#define rad7        0x0426
#define rad8        0x0427
#define rad9        0x0428
#define rad10       0x0429
#define rad11       0x042a
#define rad12       0x042b
#define rad13       0x042c
#define rad14       0x042d
#define rad15       0x042e
#define rad16       0x042f

//
//  Groups, frames, rectangles, and icons.
//
#define grp1        0x0430
#define grp2        0x0431
#define grp3        0x0432
#define grp4        0x0433
#define frm1        0x0434
#define frm2        0x0435
#define frm3        0x0436
#define frm4        0x0437
#define rct1        0x0438
#define rct2        0x0439
#define rct3        0x043a
#define rct4        0x043b
#define ico1        0x043c
#define ico2        0x043d
#define ico3        0x043e
#define ico4        0x043f

//
//  Static text.
//
#define stc1        0x0440
#define stc2        0x0441
#define stc3        0x0442
#define stc4        0x0443
#define stc5        0x0444
#define stc6        0x0445
#define stc7        0x0446
#define stc8        0x0447
#define stc9        0x0448
#define stc10       0x0449
#define stc11       0x044a
#define stc12       0x044b
#define stc13       0x044c
#define stc14       0x044d
#define stc15       0x044e
#define stc16       0x044f
#define stc17       0x0450
#define stc18       0x0451
#define stc19       0x0452
#define stc20       0x0453
#define stc21       0x0454
#define stc22       0x0455
#define stc23       0x0456
#define stc24       0x0457
#define stc25       0x0458
#define stc26       0x0459
#define stc27       0x045a
#define stc28       0x045b
#define stc29       0x045c
#define stc30       0x045d
#define stc31       0x045e
#define stc32       0x045f

//
//  Listboxes.
//
#define lst1        0x0460
#define lst2        0x0461
#define lst3        0x0462
#define lst4        0x0463
#define lst5        0x0464
#define lst6        0x0465
#define lst7        0x0466
#define lst8        0x0467
#define lst9        0x0468
#define lst10       0x0469
#define lst11       0x046a
#define lst12       0x046b
#define lst13       0x046c
#define lst14       0x046d
#define lst15       0x046e
#define lst16       0x046f

//
//  Combo boxes.
//
#define cmb1        0x0470
#define cmb2        0x0471
#define cmb3        0x0472
#define cmb4        0x0473
#define cmb5        0x0474
#define cmb6        0x0475
#define cmb7        0x0476
#define cmb8        0x0477
#define cmb9        0x0478
#define cmb10       0x0479
#define cmb11       0x047a
#define cmb12       0x047b
#define cmb13       0x047c
#define cmb14       0x047d
#define cmb15       0x047e
#define cmb16       0x047f

//
//  Edit controls.
//
#define edt1        0x0480
#define edt2        0x0481
#define edt3        0x0482
#define edt4        0x0483
#define edt5        0x0484
#define edt6        0x0485
#define edt7        0x0486
#define edt8        0x0487
#define edt9        0x0488
#define edt10       0x0489
#define edt11       0x048a
#define edt12       0x048b
#define edt13       0x048c
#define edt14       0x048d
#define edt15       0x048e
#define edt16       0x048f

//
//  Scroll bars.
//
#define scr1        0x0490
#define scr2        0x0491
#define scr3        0x0492
#define scr4        0x0493
#define scr5        0x0494
#define scr6        0x0495
#define scr7        0x0496
#define scr8        0x0497

//
//  These dialog resource ordinals really start at 0x0600, but the
//  RC Compiler can’t handle hex for resource IDs, hence the decimal.
//
#define FILEOPENORD      1536
#define MULTIFILEOPENORD 1537
#define PRINTDLGORD      1538
#define PRNSETUPDLGORD   1539
#define FINDDLGORD       1540
#define REPLACEDLGORD    1541
#define FONTDLGORD       1542
#define FORMATDLGORD31   1543
#define FORMATDLGORD30   1544
#define RUNDLGORD        1545

#if (WINVER >= 0x400)
#define PAGESETUPDLGORD  1546
#define NEWFILEOPENORD   1547
#define NEWOBJECTOPENORD 1548
#endif /* WINVER >= 0x400) */

 


//
//  Typedef Declarations.
//
typedef struct tagCRGB
{
    BYTE    bRed;
    BYTE    bGreen;
    BYTE    bBlue;
    BYTE    bExtra;
} CRGB;           /* RGB Color */


#endif // _DLGSH_INCLUDED_

分享到:

上一篇:再谈通过VC编程修改注册表配置ODB

下一篇:

评论 (1条) 发表评论

  • 过客 (游客) : 除了copy MFC code 外无任何内容

    2009-01-24 14:18

发表评论
验证码