Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Vista Common File Dialog+Read Only

, 30 Aug 2014
Rate this:
Please Sign up or sign in to vote.
How to use read only option in Vista common file dialog

Introduction

This tip explains how to use read only option in Vista common file dialog. In Windows Vista, the common file dialog has no Read-only check box.

Background

Screenshot

Using the Code

Use the following code to add read-only to Open file dialog:

void CMainFrame::OnFileOpen()
{
CFileDialog dlgOpen(TRUE, NULL, NULL,
OFN_ALLOWMULTISELECT | OFN_EXPLORER | OFN_DONTADDTORECENT | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ENABLESIZING,
_T("All Files (*.*)|*.*||"));

OSVERSIONINFO vi;
vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx(&vi);
BOOL bVista = (vi.dwMajorVersion >= 6);

if (bVista)
{
     IFileDialogCustomize* pCustomize = dlgOpen.GetIFileDialogCustomize();
     if (pCustomize != NULL)
     {
            pCustomize->EnableOpenDropDown(1);
            pCustomize->AddControlItem(1, 1, _T("Open"));
            pCustomize->AddControlItem(1, 2, _T("Open as read-only"));
            pCustomize->Release();
      }
}

if (dlgOpen.DoModal() == IDOK)
{
     BOOL bReadOnly = FALSE;

     if (bVista)
     {
          IFileDialogCustomize* pCustomize = dlgOpen.GetIFileDialogCustomize();
          if (pCustomize != NULL)
          {
              DWORD dwID;
              pCustomize->GetSelectedControlItem(1, &dwID);
              pCustomize->Release();
              bReadOnly = (dwID == 2);
          }
      }
     else
     {
          bReadOnly = dlgOpen.GetReadOnlyPref();
      }

      CString str;
      POSITION pos = dlgOpen.GetStartPosition();
      while (pos != NULL)
      {
           CString strFileName = dlgOpen.GetNextPathName(pos);
           str.FormatMessage(IDS_MESSAGE_OPENING, strFileName);
           SetMessageText(str);
           OpenDocumentFile(strFileName, bReadOnly);
      }

      SetMessageText(AFX_IDS_IDLEMESSAGE);
    }
}

Important code line is as follows:

IFileDialogCustomize* pCustomize = dlgOpen.GetIFileDialogCustomize(); 
if (pCustomize != NULL) 
{ 
pCustomize->EnableOpenDropDown(1); 
pCustomize->AddControlItem(1, 1, _T("Open")); // First item label set as open button caption
pCustomize->AddControlItem(1, 2, _T("Open as read-only")); 
pCustomize->Release(); // Don't forget to release
} 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

drjackool

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Aug 2014
Article Copyright 2014 by drjackool
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid