Mega Code Archive

 
Categories / Delphi / Examples
 

Force the topendialog to open in icon view

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.