Click here to Skip to main content
12,453,873 members (74,516 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4K views
1 bookmarked
Posted

Vista Common File Dialog+Read Only

, 30 Aug 2014 CPOL
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

Javad Taheri (drjackool)
Iran (Islamic Republic of) Iran (Islamic Republic of)
Visual C++ Enjoyer.
http://www.softpedia.com/publisher/Javad-Taheri-91660.html

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 30 Aug 2014
Article Copyright 2014 by Javad Taheri (drjackool)
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid