본문 바로가기
WORK/Sotfware

CFileFolderDialog class

by KANG Stroy 2013. 2. 5.
728x90
728x90

CFileFolderDialog class

[펌] [http://www.codeguru.com/cpp/w-d/dislog/dialogforselectingfolders/article.php/c1905/Class-That-Allows-For-Selecting-Files-or-Folders.htm] 
This class extends the basic CFileDialog class. However, using this class, you can switch between viewing only files or viewing only folders. This way, you don't have to programmatically use both the CFileDialog and the CFolderDialog classes. The interface to the CFileFolderDialog class is nearly identical to the CFileDialog class.

  1. CFileFolderDialog dlg(TRUE);
  2. if(dlg.DoModal()==IDOK)
  3. {
  4. CString pathname = dlg.GetPathName;
  5. }
This class subclasses the window function in order to hook the "Open"(IDOK) button.
  1. /*
  2. CFileFolderDialog.h
  3. by tsuneo 1999/07/08
  4. Extended Common DialogBox for Open/Save Filename or Folder.
  5. if textbox is empty and "open" button is pushed, selected folder is returned.
  6. The usage is nearly same as CFileDialog.
  7. ex)
  8. CFileFolderDialog dlg(TRUE);
  9. if(dlg.DoModal()==IDOK){
  10. CString pathname = dlg.GetPathName;
  11. }
  12. */
  13. #if !defined(AFX_FILEFOLDERDIALOG_H__A87A6F25_3446_11D3_AA89_00A02475A9BC__INCLUDED_)
  14. #define AFX_FILEFOLDERDIALOG_H__A87A6F25_3446_11D3_AA89_00A02475A9BC__INCLUDED_
  15. class CFileFolderDialog : public CFileDialog
  16. {
  17. DECLARE_DYNAMIC(CFileFolderDialog)
  18. public:
  19. CFileFolderDialog(BOOL bOpenFileDialog, // FileOpen(TRUE) or FileSave(FALSE)
  20. LPCTSTR lpszDefExt = NULL,
  21. LPCTSTR lpszFileName = NULL,
  22. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  23. LPCTSTR lpszFilter = NULL,
  24. CWnd* pParentWnd = NULL);
  25. virtual CString GetPathName() const;
  26. virtual CString GetFileName() const;
  27. virtual CString GetFileExt() const;
  28. virtual CString GetFileTitle() const;
  29. protected:
  30. // CString m_strPathName; // selected full path name
  31. protected:
  32. //{{AFX_MSG(CFileFolderDialog)
  33. virtual BOOL OnInitDialog();
  34. //}}AFX_MSG
  35. DECLARE_MESSAGE_MAP()
  36. };
  37. //{{AFX_INSERT_LOCATION}}
  38. #endif // !defined(AFX_FILEFOLDERDIALOG_H__A87A6F25_3446_11D3_AA89_00A02475A9BC__INCLUDED_)
  39. //////////////////////////////////////////////////////////////////////
  40. // FileFolderDialog.cpp : Implementation File
  41. //
  42. #include "stdafx.h"
  43. #include "FileFolderDialog.h"
  44. #include
  45. #ifdef _DEBUG
  46. #define new DEBUG_NEW
  47. #undef THIS_FILE
  48. static char THIS_FILE[] = __FILE__;
  49. #endif
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CFileFolderDialog
  52. /* dummy file name for end dialog */
  53. #define DUMMY_FILE "__dummyfile__"
  54. IMPLEMENT_DYNAMIC(CFileFolderDialog, CFileDialog)
  55. CFileFolderDialog::CFileFolderDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
  56. DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
  57. CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
  58. {
  59. }
  60. BEGIN_MESSAGE_MAP(CFileFolderDialog, CFileDialog)
  61. //{{AFX_MSG_MAP(CFileFolderDialog)
  62. //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64. WNDPROC oldWindowProc; /* original window function to save*/
  65. /* hooked window funcction */
  66. static LRESULT CALLBACK SubclassWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  67. {
  68. if(uMsg == WM_COMMAND){
  69. if(LOWORD(wParam) == IDOK){
  70. /* if OK button is pushed and text box is empty, enter dummy filename for end dialog */
  71. char filename[1000];
  72. if(GetDlgItemText(hwnd, edt1, filename, sizeof(filename)) == 0){
  73. SetDlgItemText(hwnd, edt1, DUMMY_FILE);
  74. }
  75. }
  76. }
  77. return CallWindowProc(oldWindowProc, hwnd, uMsg, wParam, lParam);
  78. }
  79. BOOL CFileFolderDialog::OnInitDialog()
  80. {
  81. CFileDialog::OnInitDialog();
  82. oldWindowProc = (WNDPROC)::SetWindowLong(GetParent()->m_hWnd, GWL_WNDPROC, (DWORD)SubclassWindowProc);
  83. return TRUE;
  84. }
  85. CString CFileFolderDialog::GetPathName() const{
  86. CString pathname(m_szFileName);
  87. if(CString(m_szFileTitle) == DUMMY_FILE){
  88. int slashpos = pathname.ReverseFind('\\');
  89. if(slashpos != -1){ pathname = pathname.Left(slashpos); }
  90. }
  91. return pathname;
  92. }
  93. CString CFileFolderDialog::GetFileName() const{
  94. CString pathname = GetPathName();
  95. int slashpos = pathname.ReverseFind('\\');
  96. return pathname.Mid(slashpos + 1);
  97. }
  98. CString CFileFolderDialog::GetFileExt() const{
  99. CString filename = GetFileName();
  100. int dotpos = filename.ReverseFind('.');
  101. return (dotpos != -1) ? filename.Mid(dotpos+1) : "";
  102. }
  103. CString CFileFolderDialog::GetFileTitle() const{
  104. CString filename = GetFileName();
  105. int dotpos = filename.ReverseFind('.');
  106. return filename.Left(dotpos);
  107. }

728x90

'WORK > Sotfware' 카테고리의 다른 글

warning: control reaches end of non-void function  (0) 2013.06.11
HexChar To Int  (0) 2013.02.22
VC c++ 6.0 버튼에 이미지 넣기  (0) 2013.02.04
응용개발자의 자료저장소  (0) 2013.02.01
ShellExecute()의 12가지 사용방법  (0) 2013.02.01

댓글