[SOLVED]: TOpenDialog Screen Position lazarus / 31-LCL ---------------------------------------------------------------------- Pascaluvr · 2012-01-16 19:52 I would like to be able to set the position on the screen where TOpenDialog is displayed. I have a 2 screen system and if I move my application to the second screen OpenDialog is displayed half on the right of screen 1 and half on the left of screen 2 - which of course is a little annoying. Any ideas, or will I need to write my own OpenDialog? Thanks in advance ---------------------------------------------------------------------- Bart · 2012-01-17 09:17 You can take a look at my ExtMsgDlg unit. By default it centers MessageDialogs over the application's mainform, but you can also set postition. Take a look at the sources, and adapt for your needs. It's modified LGPL. Bart ---------------------------------------------------------------------- Pascaluvr · 2012-01-17 17:59 Thanks Bart, Yes, I could certainly use some of the ideas from that if I am forced to write my own OpenDialog function.  While a basic version would not be too hard, adding virtual folders (My Computer, for example) would turn into a much bigger task. Thank you for the reply and download; ---------------------------------------------------------------------- Pascaluvr · 2012-01-19 11:20 Just an update: I moved the Lazarus IDE to my second screen and clicked 'Open file' - and yes - exactly the same problem - the open file dialog was split across my 2 monitors.  I guess that tells me there is no solution within FPC/Lazarus. I will try to write my own replacement component for this, but I have no experience in OOP and so it will probably be just a standard .pas unit.  If there is any interest, I will post it here for others to turn into a 'real' component. ---------------------------------------------------------------------- Bart · 2012-01-19 13:13 AFAIK, TOpenDialog uses Widgetset native dialogs. On WinVista and up it uses IFileDialog, not sure if it will let you set it's position. Bart ---------------------------------------------------------------------- Troodon · 2012-01-19 16:18 These are system dialogs, you cannot change their position programmatically. I too have a dual monitor Windows system and did not experience the issue that you described. You may need to unselect an option titled "Enable window spanning across multiple displays" in your Display or video card settings. ---------------------------------------------------------------------- Pascaluvr · 2012-01-20 01:24 You may need to unselect an option titled "Enable window spanning across multiple displays" in your Display or video card settings. ---------------------------------------------------------------------- Thanks for your reply. I have WinXP and  I am unable to find that option anywhere.  It seems it may solve my problem though, would appreciate if you could help me find it. Thanks again ---------------------------------------------------------------------- Troodon · 2012-01-20 03:10 It may depend on your video card and driver. If you downloaded and installed your video driver from the manufacturer's web site then there is probably a utility it came with that should allow you to change the graphics settings. Sorry I cannot help more. ---------------------------------------------------------------------- ludob · 2012-01-20 09:30 QuoteI have WinXP and  I am unable to find that option anywhere.On XP you can use the following code to reposition the dialog using the Onshow event of TOpenFileDialog : Code: [Select]uses windows; ....   TForm1 = class(TForm) ...   private     OldHook:LPOFNHOOKPROC; ...   end; function OpenFileDialogCallBack(Wnd: HWND; uMsg: UINT; wParam: WPARAM;   lParam: LPARAM): UINT_PTR; stdcall; begin   Result := 0;   if uMsg = WM_INITDIALOG then     begin     SetWindowPos(GetParent(Wnd), HWND_TOP, NewLeft, NewTop, 0, 0, SWP_NOSIZE);     LPOPENFILENAME(Form1.OpenDialog1.Handle)^.lpfnHook:=Form1.OldHook;     end   else     result:=Form1.OldHook(Wnd,uMsg,wParam,lParam); end; procedure TForm1.OpenDialog1Show(Sender: TObject); begin   OldHook:=LPOPENFILENAME(OpenDialog1.Handle)^.lpfnHook;   LPOPENFILENAME(OpenDialog1.Handle)^.lpfnHook:=@OpenFileDialogCallBack; end; Set NewLeft, NewTop to your liking. This is not a hack but the official MS way to position the dialog created with the GetOpenFileName API call used in the LCL. As Bart stated, on Vista or higher IFileDialog is used instead of GetOpenFileName. As a result the LPOPENFILENAME(OpenDialog1.Handle) cast is rubish and the code can potentially crash your program or do some other strange things. A work-around for this (the use of IFileDialog in vista and up) is to remove {$DEFINE UseVistaDialogs} in lcl\interfaces\win32\win32defines.inc and recompile the LCL. ---------------------------------------------------------------------- Troodon · 2012-01-20 19:31 The point is, he should not need to do that. Pascal, I wonder if you would care to try running on your computer a small application (500 KB) that I developed in Lazarus/FPC a while ago to see if it can reproduce the annoyance you mentioned. If you do, please PM me with an email address. Thanks. ---------------------------------------------------------------------- ludob · 2012-01-21 07:49 Quote from: Troodon on January 20, 2012, 08:31:24 pmThe point is, he should not need to do that. Is this a philosophical statement? Internally the LCL Windows widgetset (XP) is using OpenFileDialogCallBack where it is putting the dialog in the middle of the screen. So it is the LCL that puts the window in the middle, not a Windows default/setting or driver setting or whatever external component. The code I proposed is chaining a new OpenFileDialogCallBack with the LCL one, overriding the LCL WM_INITDIALOG message handling. ---------------------------------------------------------------------- Pascaluvr · 2012-01-21 17:18 Hi Ludob, Thanks for that snippet it looks exactly like I need. A bit busy at work for a couple more days but will definitely be trying it as soon as time permits. Thanks again ---------------------------------------------------------------------- Pascaluvr · 2012-01-21 17:20 Thank you Troodon, I have sent you a PM and look forward to testing you application. ---------------------------------------------------------------------- Pascaluvr · 2012-01-23 08:36 Hi Troodon. Now I am totally confused.  Your application is perfectly well-behaved on my PC = so why then does my application mess up?  Additionally, why does even the Lazarus IDE 'misbehave' on my PC?  Could you try that on yours please? Ludob, my work schedule still sucks, but hopefully will try your solution in 2 days. Thanks to all for their responses. ---------------------------------------------------------------------- Troodon · 2012-01-23 16:21 Will send you source code for comparison later this afternoon (I am on EST). In the meantime, you can try reinstalling Lazarus/FPC -- the latest stable release; also delete the Lazarus preferences folder "C:\Documents and Settings\\Local Settings\Application Data\lazarus" first. ---------------------------------------------------------------------- Troodon · 2012-01-24 00:11 Decided to post it here instead. The main form contains Button1, Label1, and OpenDialog1. This is the code for the main form. Code: [Select]unit Unit1; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)     Button1: TButton;     Label1: TLabel;     OpenDialog1: TOpenDialog;     procedure Button1Click(Sender: TObject);   private     { private declarations }   public     { public declarations }   end; var   Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject); begin   if OpenDialog1.Execute() then     label1.Caption := OpenDialog1.FileName   else     label1.Caption := ''; end; end. ---------------------------------------------------------------------- Pascaluvr · 2012-01-24 05:38 Hi Troodon, Thanks - Partially solved. I had used:   var  OFN: TOpenDialog; instead of adding TOpenDialog to my Form.  (I copied a Delphi example). BUT - If i OpenDialog while my application is still in Screen1, it behaves. If I then OpenDialog after I move my application to Screen2 - it still behaves. If I then OpenDialog after I move my application back to Screen1, the dialog still opens in Screen2.    This happens in your test program as well. So, I say its partially solved - as it only works if I only move my application ONE time. I accept that this is a little unusual to have this requirement, but without giving a detailed description, I assure you I do need to move my application around the screens. Thanks for you assistance thus far. ---------------------------------------------------------------------- User137 · 2012-01-24 07:56 Ah, a custom TForm as a dialog. That goes in between screens only if its position property is set to DesktopCenter. It should be ScreenCenter. ---------------------------------------------------------------------- Pascaluvr · 2012-01-24 08:12 Ah, a custom TForm as a dialog. That goes in between screens only if its position property is set to DesktopCenter. It should be ScreenCenter. ======================================================= Nope, that makes no difference to the scenario I previously posted. Thanks anyways. ---------------------------------------------------------------------- Pascaluvr · 2012-01-25 20:52 Hi Ludob, I've had some time to try your suggestion and I still have problems. Start my program and click OFN and it works fine. or Start my program and move it to the 2nd screen and click OFN - works fine. but in either case, if I then move my application to another position, and click OFN, the dialog opens in the same position as the first time. In other words, The SetWindowPos only seems to work for the first time the callback is raised, although I know the Callback is being executed as I put in a ShowMessage with NewLeft and NewTop being displayed and that came up each time. Any more ideas? Thabnks for your help to date. ---------------------------------------------------------------------- ludob · 2012-01-26 08:15 Did some further testing with opening a dialog for a second time. Windows seems to store the first position of the dialog and restore it the second time. SetWindowPos doesn't return an error but the position is overridden somewhat later by windows. The fact that windows stores some settings is somewhat confirmed by the following: - create a TOpenDialog at run time and destroy. The first time, on my slow computer, there is a fair delay before the dialog is opened the first time. The second time, the dialog opens immediately. This corresponds with the loading of the common dialogs library. Repositioning doesn't work for the second dialog. - execute a TSaveDialog after the TOpenDialog and the position is the same as for the TOpenDialog although the LCL tries to put it in the middle. Another problem I found, in the LCL this time, but not related to positioning of the dialog is that OnShow is called twice. I'll raise a bug report for that. The following workaround is working for me but, on my slow computer, you notice the repositioning of the screen as a short flash: Code: [Select]function OpenFileDialogCallBack(Wnd: HWND; uMsg: UINT; wParam: WPARAM;   lParam: LPARAM): UINT_PTR; stdcall; begin   Result := 0;   if uMsg = WM_INITDIALOG then     begin     SetWindowPos(GetParent(Wnd), HWND_TOP, Form1.Left, Form1.Top, 0, 0, SWP_NOSIZE);     end   else if (uMsg = WM_NOTIFY) and (lParam<>0) and (LPOFNOTIFY(lParam)^.hdr.code= CDN_SELCHANGE) then     begin     SetWindowPos(GetParent(Wnd), HWND_TOP, Form1.Left, Form1.Top, 0, 0, SWP_NOSIZE);     result:=Form1.OldHook(Wnd,uMsg,wParam,lParam);     end   else     result:=Form1.OldHook(Wnd,uMsg,wParam,lParam); end; procedure TForm1.OpenDialog1Show(Sender: TObject); begin   if LPOPENFILENAME(OpenDialog1.Handle)^.lpfnHook<>@OpenFileDialogCallBack then     begin     OldHook:=LPOPENFILENAME(OpenDialog1.Handle)^.lpfnHook;     LPOPENFILENAME(OpenDialog1.Handle)^.lpfnHook:=@OpenFileDialogCallBack;     end   else     LPOPENFILENAME(Form1.OpenDialog1.Handle)^.lpfnHook:=Form1.OldHook; end; The dialog is positioned here to match the upper left corner of the form. Change to your own liking. The CDN_SELCHANGE command is sent when the dialog is fully populated and shown. Thereafter it will be triggered every time you change the file selected. As a result, when you manually move the dialog to another position, it will re-position whenever you change the selection.  If this is too annoying just use a flag that is set in OpenDialog1Show and cleared after the first CDN_SELCHANGE. Call SetWindowPos only when flag is set. I tried other events and commands but they all happen before windows repositions the dialog to the old position. The code change in OpenDialog1Show is to work around the LCL double Onshow issue. ---------------------------------------------------------------------- Pascaluvr · 2012-01-26 18:13 Wow Ludob, thank you so much for going the extra mile and actually finding a solution for me.  It is truly appreciated. I made all your changes including setting a flag to avoid flicker.  It all works perfectly. Many thanks for all your efforts.