home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / METAKIT.ZIP / EXAMPLES / CATFISH / PICKDIR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-09  |  4.3 KB  |  141 lines

  1. //    pickdir.cpp  -  directory picker sample code
  2. //
  3. //    This is a part of the MetaKit library.
  4. //    Copyright (c) 1996 Meta Four Software.
  5. //    All rights reserved.
  6. /////////////////////////////////////////////////////////////////////////////
  7.  
  8. #include "stdafx.h"
  9.  
  10. #ifdef _DEBUG
  11. #undef THIS_FILE
  12. static char BASED_CODE THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CMyFileDlg dialog, adapted from DIRPKR sample code (EMS_9502.1\CUTIL) 
  17.  
  18. #include "dlgs.h"
  19.  
  20. class CMyFileDlg : public CFileDialog
  21. {
  22. public:
  23.     
  24. // Public data members
  25.  
  26.     BOOL m_bDlgJustCameUp;
  27.     
  28. // Constructors
  29.  
  30.     CMyFileDlg(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
  31.                LPCSTR lpszDefExt = NULL,
  32.                LPCSTR lpszFileName = NULL,
  33.                DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
  34.                LPCSTR lpszFilter = NULL,
  35.                CWnd* pParentWnd = NULL);
  36.                                           
  37. // Implementation
  38. protected:
  39.     //{{AFX_MSG(CMyFileDlg)
  40.     virtual BOOL OnInitDialog();
  41.     afx_msg void OnPaint();
  42.     //}}AFX_MSG
  43.     DECLARE_MESSAGE_MAP()
  44. };
  45.  
  46. BEGIN_MESSAGE_MAP(CMyFileDlg, CFileDialog)
  47.     //{{AFX_MSG_MAP(CMyFileDlg)
  48.     ON_WM_PAINT()
  49.     //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51.  
  52. CMyFileDlg::CMyFileDlg (BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
  53.                         LPCSTR lpszDefExt, LPCSTR lpszFileName,
  54.                         DWORD dwFlags, LPCSTR lpszFilter, CWnd* pParentWnd)
  55.     : CFileDialog (bOpenFileDialog, lpszDefExt, lpszFileName,
  56.                     dwFlags, lpszFilter, pParentWnd)
  57. {
  58.     //{{AFX_DATA_INIT(CMyFileDlg)
  59.     //}}AFX_DATA_INIT
  60. }
  61.  
  62. BOOL CMyFileDlg::OnInitDialog()
  63. {  
  64.     CenterWindow();
  65.     
  66. //Let's hide these windows so the user cannot tab to them.  Note that in
  67. //the private template (in cddemo.dlg) the coordinates for these guys are
  68. //*outside* the coordinates of the dlg window itself.  Without the following
  69. //ShowWindow()'s you would not see them, but could still tab to them.
  70.     
  71.     GetDlgItem(stc2)->ShowWindow(SW_HIDE);
  72.     GetDlgItem(stc3)->ShowWindow(SW_HIDE);
  73.     GetDlgItem(edt1)->ShowWindow(SW_HIDE);
  74.     GetDlgItem(lst1)->ShowWindow(SW_HIDE);
  75.     GetDlgItem(cmb1)->ShowWindow(SW_HIDE);
  76.     
  77. //We must put something in this field, even though it is hidden.  This is
  78. //because if this field is empty, or has something like "*.txt" in it,
  79. //and the user hits OK, the dlg will NOT close.  We'll jam something in
  80. //there (like "Junk") so when the user hits OK, the dlg terminates.
  81. //Note that we'll deal with the "Junk" during return processing (see below)
  82.  
  83.     SetDlgItemText(edt1, "Junk");
  84.     
  85. //Now set the focus to the directories listbox.  Due to some painting
  86. //problems, we *must* also process the first WM_PAINT that comes through
  87. //and set the current selection at that point.  Setting the selection
  88. //here will NOT work.  See comment below in the on paint handler.
  89.             
  90.     GetDlgItem(lst2)->SetFocus();
  91.                 
  92.     m_bDlgJustCameUp=TRUE;
  93.                  
  94.     CFileDialog::OnInitDialog();
  95.        
  96.     return(FALSE);
  97. }
  98.  
  99. void CMyFileDlg::OnPaint()
  100. {
  101.     CPaintDC dc(this); // device context for painting
  102.     
  103. //This code makes the directory listbox "highlight" an entry when it first
  104. //comes up.  W/O this code, the focus is on the directory listbox, but no
  105. //focus rectangle is drawn and no entries are selected.  Ho hum.
  106.  
  107.     if (m_bDlgJustCameUp)
  108.     {
  109.         m_bDlgJustCameUp=FALSE;
  110.         SendDlgItemMessage(lst2, LB_SETCURSEL, 0, 0L);
  111.     }
  112.     
  113.     // Do not call CFileDialog::OnPaint() for painting messages
  114. }
  115.  
  116. CString PickDirectory(CWnd* pParentWnd)
  117. {
  118.     if (!pParentWnd)
  119.         pParentWnd = AfxGetApp()->m_pMainWnd;
  120.         
  121.     CMyFileDlg  cfdlg(FALSE, NULL, NULL, OFN_SHOWHELP | OFN_HIDEREADONLY |
  122.                       OFN_OVERWRITEPROMPT | OFN_ENABLETEMPLATE | OFN_NOCHANGEDIR,
  123.                       NULL, pParentWnd);
  124.     cfdlg.m_ofn.hInstance = AfxGetInstanceHandle();
  125.     cfdlg.m_ofn.lpTemplateName = MAKEINTRESOURCE(FILEOPENORD);
  126.  
  127. #ifdef _WIN32
  128.     cfdlg.m_ofn.Flags &= ~ OFN_EXPLORER;
  129. #endif
  130.  
  131.     if (cfdlg.DoModal() != IDOK)
  132.         return "";
  133.  
  134.     cfdlg.m_ofn.lpstrFile[cfdlg.m_ofn.nFileOffset-1] = 0; //Nuke the "Junk"
  135.     
  136.     return cfdlg.m_ofn.lpstrFile;
  137. }
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140. // $Id: pickdir.cpp,v 1.2 1996/12/04 14:49:30 jcw Exp $
  141.