Recent

Author Topic: [SOLVED]: TOpenDialog Screen Position  (Read 21499 times)

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
[SOLVED]: TOpenDialog Screen Position
« on: January 16, 2012, 08:52:57 pm »
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
« Last Edit: January 26, 2012, 07:14:16 pm by Pascaluvr »
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Bart

  • Hero Member
  • *****
  • Posts: 5573
    • Bart en Mariska's Webstek
Re: TOpenDialog Screen Position
« Reply #1 on: January 17, 2012, 10:17:52 am »
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

  • Full Member
  • ***
  • Posts: 216
Re: TOpenDialog Screen Position
« Reply #2 on: January 17, 2012, 06:59:28 pm »
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;
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: TOpenDialog Screen Position
« Reply #3 on: January 19, 2012, 12:20:37 pm »
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.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Bart

  • Hero Member
  • *****
  • Posts: 5573
    • Bart en Mariska's Webstek
Re: TOpenDialog Screen Position
« Reply #4 on: January 19, 2012, 02:13:46 pm »
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

  • Sr. Member
  • ****
  • Posts: 484
Re: TOpenDialog Screen Position
« Reply #5 on: January 19, 2012, 05:18:00 pm »
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.
« Last Edit: January 19, 2012, 05:19:52 pm by Troodon »
Lazarus/FPC on Linux

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: TOpenDialog Screen Position
« Reply #6 on: January 20, 2012, 02:24:17 am »
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
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: TOpenDialog Screen Position
« Reply #7 on: January 20, 2012, 04:10:07 am »
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.
Lazarus/FPC on Linux

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TOpenDialog Screen Position
« Reply #8 on: January 20, 2012, 10:30:54 am »
Quote
I 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

  • Sr. Member
  • ****
  • Posts: 484
Re: TOpenDialog Screen Position
« Reply #9 on: January 20, 2012, 08:31:24 pm »
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.
Lazarus/FPC on Linux

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: TOpenDialog Screen Position
« Reply #10 on: January 21, 2012, 08:49:26 am »
The 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

  • Full Member
  • ***
  • Posts: 216
Re: TOpenDialog Screen Position
« Reply #11 on: January 21, 2012, 06:18:54 pm »
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
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: TOpenDialog Screen Position
« Reply #12 on: January 21, 2012, 06:20:08 pm »
Thank you Troodon,

I have sent you a PM and look forward to testing you application.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: TOpenDialog Screen Position
« Reply #13 on: January 23, 2012, 09:36:32 am »
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.
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

Troodon

  • Sr. Member
  • ****
  • Posts: 484
Re: TOpenDialog Screen Position
« Reply #14 on: January 23, 2012, 05:21:13 pm »
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\<your_user_name>\Local Settings\Application Data\lazarus" first.
Lazarus/FPC on Linux

 

TinyPortal © 2005-2018