Blog

All Blog Posts  |  Next Post  |  Previous Post

The new File Open / Save dialogs in Windows Vista

Bookmarks: 

Wednesday, January 21, 2009

Introduction

In Windows Vista the common File Open and File Save dialogs have got a facelift as well. Unfortunately, Microsoft didn't make the new common file dialogs 100% backwards compatible with the existing GetOpenFileName, GetSaveFileName API calls. As soon as the flag OFN_ENABLEHOOK or OFN_ENABLETEMPLATE are used, the functions fall back to their old appearance. Unfortunately, Delphi uses by default OFN_ENABLEHOOK (just to center the dialog) and optionally OFN_ENABLETEMPLATE. This means that there is not a way to use the new Windows Vista style dialogs with TOpenDialog or TSaveDialog. When TOpenDialog is used in Delphi on Windows Vista, following dialog appears:

where the new Windows Vista style dialogs look like:

So, to start using the new Windows Vista Open File & Save File dialogs today from Delphi, either the VCL should be changed (which we suspect Borland will do in a future Delphi version) or a replacement function can be used. We've included here a quick replacement function for TOpenDialog and TSaveDialog that uses the new nice Windows Vista dialogs today:

function OpenSaveFileDialog(Parent: 
TWinControl; const DefExt, Filter, InitialDir, Title: string; var FileName: 
string; 
  MustExist, OverwritePrompt, NoChangeDir, DoOpen: Boolean): Boolean;
var
  ofn: TOpenFileName;
  szFile: array[0..MAX_PATH] of Char;
begin
  Result := False;
  FillChar(ofn, SizeOf(TOpenFileName), 0);
  with ofn do
  begin
    lStructSize := SizeOf(TOpenFileName);
    hwndOwner := Parent.Handle;
    lpstrFile := szFile;
    nMaxFile := SizeOf(szFile);
    if (Title <> '') then
      lpstrTitle := PChar(Title);
    if (InitialDir <> '') then
      lpstrInitialDir := PChar(InitialDir);
    StrPCopy(lpstrFile, FileName);
    lpstrFilter := PChar(ReplaceStr(Filter, '|', #0)+#0#0);
    if DefExt <> '' then
      lpstrDefExt := PChar(DefExt);
  end;

  if MustExist then
    ofn.Flags := ofn.Flags or OFN_FILEMUSTEXIST;

  if OverwritePrompt then
    ofn.Flags := ofn.Flags or OFN_OVERWRITEPROMPT;

  if NoChangeDir then
    ofn.Flags := ofn.Flags or OFN_NOCHANGEDIR;

  if DoOpen then
  begin
    if GetOpenFileName(ofn) then
    begin
      Result := True;
      FileName := StrPas(szFile);
    end;
  end
  else
  begin
    if GetSaveFileName(ofn) then
    begin
      Result := True;
      FileName := StrPas(szFile);
    end;
  end
end;



Bruno Fierens


Bookmarks: 

This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post