Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Windows 2000 style File Dialog box

Posted on 2002-05-18
Medium Priority
Last Modified: 2013-11-20
I'm just starting out with MFC and I'm wondering if there is a way to display the Win 2000 style dialog box instead of using the older style.

Here is the code I am using now:

void CSTUploadDoc::OnDataImport()
     TCHAR buffer[] = _T("Dat Files (*.dat)|*.dat|All Files (*.*)|*.*||");
     CFileDialog dlg(TRUE, _T("dat"), _T("*dat"), OFN_FILEMUSTEXIST, buffer);

     if (dlg.DoModal() == IDOK) {
          try {
               CStdioFile f(dlg.GetPathName(),CFile::modeReadWrite);
          catch (CFileException *e) {

Any help would be greatly appreciated.
Question by:boomshanker
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 12

Expert Comment

ID: 7019374
To use Win2K style dialog you have to set correct CFileDialog.m_ofn.lStructSize = sizeof(lStructSize)+3*sizeof(DWORD);
before calling DoModal;
I assume that your project doesn`t contain defines such as
_WIN32_WINNT = 0x0500;

Author Comment

ID: 7019902
Thanks for your response.

Could you possibly show me how to implement that with the code I have?

If I use the code you supplied as follows:

dlg.m_ofn.lStructSize = sizeof(lStructSize)+3*sizeof(DWORD);

I get an "lStructSize Undeclared Identifier" error upon compile.

How do I declare lStructSize?  Do I have to set any other data members of the struct?

Thank you in advance for any further help you may offer.
LVL 12

Accepted Solution

migel earned 400 total points
ID: 7019956
excuse me for this dirty CRACKed code :-)
this method can arise Access Violation  error and ugly bechavior.
Here is true code.
// FileDialogEx.h

#pragma once

// Windows 2000 version of OPENFILENAME.
// The new version has three extra members.
// This is copied from commdlg.h
  void *        pvReserved;
  DWORD         dwReserved;
  DWORD         FlagsEx;

// CFileDialogEx: Encapsulate Windows-2000 style open dialog.
class CFileDialogEx : public CFileDialog {
      CFileDialogEx(BOOL bOpenFileDialog, // TRUE for open,
                                          // FALSE for FileSaveAs
      LPCTSTR lpszDefExt = NULL,
      LPCTSTR lpszFileName = NULL,
      LPCTSTR lpszFilter = NULL,
      CWnd* pParentWnd = NULL);

   // override
   virtual int DoModal();

   OPENFILENAMEEX m_ofnEx; // new Windows 2000 version of OPENFILENAME

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

   // virtual fns that handle various notifications
   virtual BOOL OnFileNameOK();
   virtual void OnInitDone();
   virtual void OnFileNameChange();
   virtual void OnFolderChange();
   virtual void OnTypeChange();



#include "stdafx.h"
#include <afxpriv.h>
#include "FileDialogEx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;

static BOOL IsWin2000();

// CFileDialogEx


CFileDialogEx::CFileDialogEx(BOOL bOpenFileDialog,
   LPCTSTR lpszDefExt,
   LPCTSTR lpszFileName,
   DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
   CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName,
      dwFlags, lpszFilter, pParentWnd)

BEGIN_MESSAGE_MAP(CFileDialogEx, CFileDialog)

BOOL IsWin2000()
   BOOL bOsVersionInfoEx;

   // Try calling GetVersionEx using the OSVERSIONINFOEX structure,
   // which is supported on Windows 2000.
   // If that fails, try using the OSVERSIONINFO structure.

   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

   if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
      // 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;

   return FALSE;

int CFileDialogEx::DoModal()
   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,

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

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

   return nResult ? nResult : IDCANCEL;

BOOL CFileDialogEx::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);

// The following functions are provided just to demonstrate that they
// get called; ie, that MFC's internal dialog proc is in fact hooked
// up. You can delete them if you like.
BOOL CFileDialogEx::OnFileNameOK()
   return CFileDialog::OnFileNameOK();

void CFileDialogEx::OnInitDone()

void CFileDialogEx::OnFileNameChange()

void CFileDialogEx::OnFolderChange()

void CFileDialogEx::OnTypeChange()
   TRACE(_T("OnTypeChange(), index = %d\n"), m_ofn.nFilterIndex);

// EOF
Just add these 2 files into the your project and

replace CFileDialog to the CFileDialogEx in the your code.
Good Luck!

Author Comment

ID: 7020162
That's great!

Thank you so much for your help, and your quick response.
Much appreciated.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question