我是在WIN7下装的VC2010中用CFileDialog实现的打开文件对话框,但想让弹出的打开文件对话框为XP风格下的老面孔,不知如何实现。

解决方案 »

  1.   

    既然都用win7了,为何还要XP风格???
      

  2.   

    一些皮肤对WIN7风格的文件打开对话框支持不是很好。
      

  3.   

    用下面这个://XFileDialog.h
    #ifndef XFILEDIALOG_H
    #define XFILEDIALOG_Hstruct OPENFILENAMEEX : public OPENFILENAME
    {
    void * pvReserved;
    DWORD dwReserved;
    DWORD FlagsEx;
    };/////////////////////////////////////////////////////////////////////////////
    // CXFileDialog: Encapsulate Windows-2000 style open dialog.
    //
    class CXFileDialog : public CFileDialog
    {
    DECLARE_DYNAMIC(CXFileDialog)// Construction
    public:
    CXFileDialog(BOOL bOpenFileDialog, // TRUE for Open, FALSE for Save As
     LPCTSTR lpszDefExt = NULL,
     LPCTSTR lpszFileName = NULL,
     DWORD dwFlags = NULL,//OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
     LPCTSTR lpszFilter = NULL,
     CWnd* pParentWnd = NULL);// Dialog Data
    //{{AFX_DATA(CXFileDialog)
    ////CHistoryCombo m_cmbRecentFolders;
    //CHistoryCombo m_cmbCombo1;
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CXFileDialog)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    //}}AFX_VIRTUAL// Operations
    public:
    // override
    virtual int DoModal();// Attributes
    public:
    enum XFILEDIALOG_OS_VERSION
    {
    XFILEDIALOG_AUTO_DETECT_OS_VERSION = 0,
    XFILEDIALOG_OS_VERSION_4,
    XFILEDIALOG_OS_VERSION_5
    };
    CString GetPath();
    enum XFILEDIALOG_OS_VERSION GetOsVersion() { return m_eOsVersion; }
    void SetOsVersion(enum XFILEDIALOG_OS_VERSION eOsVersion) { m_eOsVersion = eOsVersion; }
    void SetTitle(LPCTSTR lpszTitle) { m_strTitle = lpszTitle; }// Implementation
    protected:
    OPENFILENAMEEX m_ofnEx; // new Windows 2000 version of OPENFILENAME
    CString m_strTitle;
    DWORD m_dwFlags; enum XFILEDIALOG_OS_VERSION m_eOsVersion; BOOL IsWin2000();
    virtual BOOL OnFileNameOK();
    virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult); DECLARE_MESSAGE_MAP()
    //{{AFX_MSG(CXFileDialog)
    virtual BOOL OnInitDialog();
    //afx_msg void OnSize(UINT nType, int cx, int cy);
    //afx_msg void OnSelendokMruCombo();
    //}}AFX_MSG
    };#endif//XFileDialog.cpp
    #include "stdafx.h"
    #include <afxpriv.h>
    #include "XFileDialog.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #endif///////////////////////////////////////////////////////////////////////////////
    // CXFileDialogIMPLEMENT_DYNAMIC(CXFileDialog, CFileDialog)BEGIN_MESSAGE_MAP(CXFileDialog, CFileDialog)
    //{{AFX_MSG_MAP(CXFileDialog)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////////
    // ctor
    CXFileDialog::CXFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt,
    LPCTSTR lpszFileName, DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
    CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName,
    dwFlags, lpszFilter, pParentWnd)
    {
    m_dwFlags              = dwFlags;
    m_eOsVersion           = XFILEDIALOG_OS_VERSION_4;
    m_strTitle             = _T("");
    }///////////////////////////////////////////////////////////////////////////////
    // DoDataExchange
    void CXFileDialog::DoDataExchange(CDataExchange* pDX)
    {
    CFileDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CXFileDialog)
    //DDX_Control(pDX, IDC_MRU_COMBO, m_cmbRecentFolders);
    //}}AFX_DATA_MAP
    }///////////////////////////////////////////////////////////////////////////////
    // DoModal override copied mostly from MFC, with modification to use
    // m_ofnEx instead of m_ofn.
    int CXFileDialog::DoModal()
    {
    //TRACE(_T("in CXFileDialog::DoModal\n")); 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); memset(&m_ofnEx, 0, sizeof(m_ofnEx));
    memcpy(&m_ofnEx, &m_ofn, sizeof(m_ofn)); if (IsWin2000())
    {
    m_ofnEx.lStructSize = sizeof(m_ofnEx);
    } int nResult;
    if (m_bOpenFileDialog)
    nResult = ::GetOpenFileName((OPENFILENAME*)&m_ofnEx);
    else
    nResult = ::GetSaveFileName((OPENFILENAME*)&m_ofnEx); memcpy(&m_ofn, &m_ofnEx, sizeof(m_ofn));
    m_ofn.lStructSize = sizeof(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_ofnEx.hwndOwner, TRUE);
    if (::IsWindow(hWndFocus))
    ::SetFocus(hWndFocus); PostModal(); return nResult ? nResult : IDCANCEL;
    }///////////////////////////////////////////////////////////////////////////////
    //
    BOOL CXFileDialog::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
    memcpy(&m_ofn, &m_ofnEx, sizeof(m_ofn));
    m_ofn.lStructSize = sizeof(m_ofn); return CFileDialog::OnNotify( wParam, lParam, pResult);
    }///////////////////////////////////////////////////////////////////////////////
    // OnFileNameOK
    BOOL CXFileDialog::OnFileNameOK()
    {
    //TRACE(_T("CXFileDialog::OnFileNameOK\n")); // save current folder to history lis return CFileDialog::OnFileNameOK();
    }///////////////////////////////////////////////////////////////////////////////
    // OnInitDialog
    BOOL CXFileDialog::OnInitDialog()
    {
    //TRACE(_T("in CXFileDialog::OnInitDialog\n")); CFileDialog::OnInitDialog(); // set title if specified
    if (!m_strTitle.IsEmpty())
    GetParent()->SetWindowText(m_strTitle); return TRUE; // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
    }///////////////////////////////////////////////////////////////////////////////
    // GetPath
    CString CXFileDialog::GetPath()
    {
    CString str = _T(""); str = GetPathName();
    if (!str.IsEmpty())
    {
    int index = str.ReverseFind(_T('\\'));
    if (index != -1)
    {
    str = str.Left(index);
    }
    } TRACE(_T("str=%s\n"), str);
    return str;
    }///////////////////////////////////////////////////////////////////////////////
    // IsWin2000
    BOOL CXFileDialog::IsWin2000()
    {
    if (GetOsVersion() == XFILEDIALOG_OS_VERSION_4)
    return FALSE;
    else if (GetOsVersion() == XFILEDIALOG_OS_VERSION_5)
    return TRUE; // Try calling GetVersionEx using the OSVERSIONINFOEX structure,
    // which is supported on Windows 2000.
    //
    // If that fails, try using the OSVERSIONINFO structure. OSVERSIONINFOEX osvi;
    ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); BOOL bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *) &osvi);
    if (!bOsVersionInfoEx)
    {
    // If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    if (!GetVersionEx((OSVERSIONINFO *) &osvi))
    return FALSE;
    } switch (osvi.dwPlatformId)
    {
    case VER_PLATFORM_WIN32_NT:
    if (osvi.dwMajorVersion >= 5)
    return TRUE;
    break;
    }
    return FALSE;
    }
      

  4.   

    好像要这样用才行,
    CXFileDialog dlg(TRUE, _T("*.mdb"), NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, _T("数据库文件(*.mdb)|*.mdb||"), this);
    dlg.SetOsVersion((enum CXFileDialog::XFILEDIALOG_OS_VERSION) 1);不过还是不错的,给分了。