CFileDialog Override Question
rrigano@pumatech.com Tuesday, August 06, 1996 Environment: Windows95, Visual C++ 4.1 Hi. I'm working on a Windows 95 platform using Visual C++ 4.1. I am attempting to override the MFC CFileDialog class so that I can respond to a selection change in the "Save as type" combo box. I'd like to be able to change the "File Name" edit control text so that its file extension matches the type extension from the combo box. For example, if the file name is "a.x" with file type "X Files (*.X)", and I change the type to "Y Files (*.Y)", then I want to change the file name to "a.y". I have tried several methods without success. I need to get the current selection in the file type combo box and the text in the file name edit box. I have listed what I tried to do, along with the source code, below. Any help in getting this to work would be greatly appreciated. Please respond directly to my e-mail address as I am not on the general mfc-l mailing list. Thanks, - Bobby Rigano Puma Technology rrigano@pumatech.com What I have tried unsuccessfully: 1. As the class wizard does not permit you to directly create message handlers for the controls of a CFileDialog derived class, I tried to manually add a message handler for a selection change of the cmb1 combo box. The individual control ids are documented in Figure 6-6, Chapter 6 of "Programming the Windows 95 User Interface" by Nancy Cluts. The ids are defined in msdev\include\dlgs.h. This message handler, OnSelchangeType, never gets called. 2. I tried overriding the virtual CFileDialog::OnChangeType method. This method is called but I cannot get at the current selection within the cmb1 combo box or the file name in the edt1 edit control. Calls to GetDlgItem(cmb1) and GetDlgItem(edt1) return NULL. So I have no idea what the current values of these controls are. 3. I tried overriding the virtual CFileDialog::OnLBSelChangedNotify but it never gets called when I change file types on the dialog. ================== Source Code, Include File ========================= // FileDlg.h : header file // #define CFILE_NORM_ISEL 0 // The indices in cmb1 combo box. #define CFILE_COMP_ISEL 1 ////////////////////////////////////////////////////////////////////// /////// // CCardFileDlg dialog class CCardFileDlg : public CFileDialog { DECLARE_DYNAMIC(CCardFileDlg) public: CCardFileDlg(BOOL bOpenFileDialog, // TRUE for Open, FALSE for SaveAs LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL); // Overridable callbacks protected: virtual void OnTypeChange(); virtual void OnLBSelChangedNotify( UINT nIDBox, UINT iCurSel, UINT nCode); protected: //{{AFX_MSG(CCardFileDlg) virtual BOOL OnInitDialog(); afx_msg void OnSelchangeType(); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: CString m_csFileExt; // Current file type extension. }; ================== Source Code, Implementation File ============= // FileDlg.cpp : implementation file // #include "stdafx.h" #include "card.h" #include#include "FileDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ////////////////////////////////////////////////////////////////////// /////// // CCardFileDlg IMPLEMENT_DYNAMIC(CCardFileDlg, CFileDialog) CCardFileDlg::CCardFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) : CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd) { if ( lpszDefExt != NULL ) { m_csFileExt = lpszDefExt; m_csFileExt.MakeLower(); } } BEGIN_MESSAGE_MAP(CCardFileDlg, CFileDialog) //{{AFX_MSG_MAP(CCardFileDlg) ON_CBN_SELCHANGE(cmb1, OnSelchangeType) //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CCardFileDlg::OnInitDialog() { CFileDialog::OnInitDialog(); CComboBox* pCmb = (CComboBox*) GetDlgItem(cmb1); // These 2 calls CEdit* pEdt = (CEdit*) GetDlgItem(edt1); // return NULL! return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } /********************************************************************* ** ** Function : CCardFileDlg::OnSelchangeLanguage ** Description : Called whenever a file type is selected from the type ** listbox. It appends the updated file extension to the ** file name. ** Parameters : none ** Returns : void ********************************************************************** */ void CCardFileDlg::OnSelchangeType() { TRACE(" MADE IT TO OnSelchangeType method!!!\n "); } void CCardFileDlg::OnTypeChange() { TRACE( "MADE IT TO OnTypeChange!!!\n" ); // Get the current file type extension. HWND hWnd = ::GetDlgItem(GetSafeHwnd(), // handle of dialog box cmb1 // identifier of control ); CString csWhat (GetFileExt() ); // Always returns initial value. CComboBox* pCombo = (CComboBox*) GetDlgItem(cmb1); if ( !pCombo ) return; // pCombo is always NULL. int nSel = pCombo->GetCurSel(); switch( nSel ) { case CFILE_COMP_ISEL: m_csFileExt = ZCARD_FILEEXT; break; case CFILE_NORM_ISEL: m_csFileExt = ECARD_FILEEXT; break; default: return; } m_csFileExt.MakeLower(); // Get the current file name. CEdit* pEdit = (CEdit*) GetDlgItem(edt1); if ( !pEdit ) return; CString csFileName; pEdit->GetWindowText( csFileName ); // Replace the old file extension with the current one. if ( !csFileName.IsEmpty() ) { int nPos = csFileName.ReverseFind( TCHAR('.') ); if ( (nPos != -1) && (nPos + 3 == csFileName.GetLength()) ) { csFileName = csFileName.Left(nPos) + m_csFileExt; pEdit->SetWindowText( csFileName ); } } } void CCardFileDlg::OnLBSelChangedNotify( UINT nIDBox, UINT iCurSel, UINT nCode) { TRACE("MADE IT TO OnLBSelChangeNotify, nIDBox = 0%x\n", nIDBox ); }
Paul D. Bartholomew -- PaulDB@datastorm.com Friday, August 09, 1996 Environment: Windows95, Visual C++ 4.1 Bobby Rigano wrote: >> 2. I tried overriding the virtual CFileDialog::OnChangeType method. This method is called but I cannot get at the current selection within the cmb1 combo box or the file name in the edt1 edit control. Calls to GetDlgItem(cmb1) and GetDlgItem(edt1) return NULL. So I have no idea what the current values of these controls are.<< This is the method I use. Under Windows 95 Explorer dialog, though, these controls are buried one level deeper than they were under previous versions. I sub-classed the CFileDialog and replaced CFileDialog::OnTypeChange with my own version. I also added a function to return the parent of those file dialog controls as follows: CWnd* CEnhancedFileDialog::GetCommDlgWindow() { #ifdef WIN32 if (m_ofn.Flags & OFN_EXPLORER) return GetParent(); #endif return this; } In my sub-classed OnTypeChange, I use this as follows: void CEnhancedFileDialog::OnTypeChange(void) { CComboBox* pComboBox = (CComboBox*) GetCommDlgWindow()->GetDlgItem(cmb1); if (!pComboBox) return; etc. Hope this helps. -Paul pauldb@datastorm.com
| Вернуться в корень Архива |