DelphiFAQ Home Search:

Force the TOpenDialog to open in icon view

 

comments1 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

Question:

My application uses a TOpenDialog and I need it to open in 'icon' view (as opposed to in 'list' view, as it seems to start on my machine). I see no such property in the TOpenDialog class - how can I set the startup view?

Answer:

The Windows API does not provide a direct way to achieve this. The unit below hooks in the OpenDialog's OnShow event and retrieves there the window handle of the open dialog. Unfortunately, at this point, the dialog's controls (e.g. the listview control) are not ready to receive messages. So instead it posts a user message WM_USER+1 to the owning form (Form1) with the handle as the wParam.

It's important that this happens asynchronously (PostMessage() instead of SendMessage()).

By the time this message gets processed, the listview control in the file open dialog can be found with a call to FindWindowEx and the message to change the view can be sent synchronously. The wParam takes the desired display e.g. FCIDM_SHVIEW_LARGEICON.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Button1    : TButton;
    procedure Button1Click(Sender: TObject);
    procedure OpenDialog1Show(Sender: TObject);
  private
    { private declarations }
    procedure WMUser(var Msg: TMessage);
      message WM_USER + 1;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
  FCIDM_SHVIEW_LARGEICON = 28713;
  FCIDM_SHVIEW_SMALLICON = 28714;
  FCIDM_SHVIEW_LIST      = 28715;
  FCIDM_SHVIEW_REPORT    = 28716;
  FCIDM_SHVIEW_THUMBNAIL = 28717; // XP only
  FCIDM_SHVIEW_TILE      = 28718; // XP only

procedure TForm1.WMUser(var Msg: TMessage);
var
  DlgWnd : HWND;
  CtrlWnd: HWND;
begin { TForm1.WMUser }
  DlgWnd := Msg.WParam;
  CtrlWnd := FindWindowEx(DlgWnd, 0, PChar('SHELLDLL_DefView'), nil);
  if CtrlWnd<>0 then
  begin
    SendMessage(CtrlWnd, WM_COMMAND, FCIDM_SHVIEW_LARGEICON, 0)
  end; { Ctrl<>0 }
end; { TForm1.WMUser }


procedure TForm1.OpenDialog1Show(Sender: TObject);
var
  Dlg: HWND;
begin { TForm1.OpenDialog1Show }
  Dlg := GetParent((Sender as TOpenDialog).Handle); 
  PostMessage(Handle, WM_USER + 1, Dlg, 0)
end; { TForm1.OpenDialog1Show } 


procedure TForm1.Button1Click(Sender: TObject); 
begin { TForm1.Button1Click } 
  OpenDialog1.Execute;
end; { TForm1.Button1Click } 


end. 

Comments:

2006-07-31, 18:03:16
anonymous from Austria  
rating

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: