Vista Common File Dialog+Read Only





0/5 (0 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
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
}