Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi How to handle window move? (https://www.delphipraxis.net/151750-how-handle-window-move.html)

WojTec 31. Mai 2010 10:09


How to handle window move?
 
Hello. I need to save window position to restore it on next start. But how to handle this event? TForm has only OnResise, what about move?

pixfreak 31. Mai 2010 10:43

Re: How to handle window move?
 
Hi,

in the OnDestroy event just save your window position (Left, Top, Width, Height) and maybe your windowstate (maximized...) in a file, ini-file or in a registry key.

Thereafter in OnCreate read your values back and the window will be displayed at the same position...


VG Pixfreak

Edit: see Manipulate INI Files and store window pos

WojTec 31. Mai 2010 12:03

Re: How to handle window move?
 
Yap, I thought about this, but this is wrong idea, because if window is maximized, it has desktop size and position is (0, 0). Finally last position in wsNormal state can't be stored. Do you know solution for this problem?

blackfin 31. Mai 2010 12:12

Re: How to handle window move?
 
Maybe this helps?:


Delphi-Quellcode:
var
 rc: TRect;
..
.
// get position
GetWindowRect(self.Handle,rc);


// set position
MoveWindow(self.Handle,rc.left,rc.top,(rc.right-rc.left),(rc.bottom-rc.top),true) ;

fnhofm 31. Mai 2010 12:16

Re: How to handle window move?
 
Hi,

you can use this code in the close or destroy event handler of the form (I used a TMemIniFile)

Delphi-Quellcode:
var
  LWindowPlacement: TWindowPlacement;
begin
  Ini.WriteInteger(Name, 'WindowState', Integer(WindowState));
  LWindowPlacement.length := SizeOf(TWindowPlacement);

  if GetWindowPlacement(Handle, LWindowPlacement) then
  begin
    Ini.WriteInteger(Name, 'Top', LWindowPlacement.rcNormalPosition.Top);
    Ini.WriteInteger(Name, 'Left', LWindowPlacement.rcNormalPosition.Left);
    Ini.WriteInteger(Name, 'Height',
      LWindowPlacement.rcNormalPosition.Bottom - LWindowPlacement.rcNormalPosition.Top);
    Ini.WriteInteger(Name, 'Width',
      LWindowPlacement.rcNormalPosition.Right - LWindowPlacement.rcNormalPosition.Left);
  end;
  Ini.UpdateFile;
end;
Hope this helps.

generic 31. Mai 2010 13:24

Re: How to handle window move?
 
I'm using GetWindowPlacement and SetWindowPlacement, too.
The difference is, i'm storing the record "TWindowPlacement" to registry as binary stream.
This saves some keys.

There is a small pitfall. If the monitor configuration changes by removing a second monitor, then the windows will still restore on the old position (on that not available monitor).
The cause that the user isn't able to see or move the window.

WojTec 31. Mai 2010 13:57

Re: How to handle window move?
 
Method with GetWindowPlacement() is exactly what I need - thanks!
I also tried with WM_MOVE, but above solution is the best.

Thanks guys :)

-- Added ---

@generic, how to fix problem you talking about? I have one monitor, so I can't de facto test it :(

ChrisE 31. Mai 2010 14:23

Re: How to handle window move?
 
Hello WojTec,

I think you test this with screen-resolutions. Because it's also possible that the resolution is changed. For example you stored your values at a resolution 1600x1200 and the program restarts at an resolution 1024x768.

I think you may check the values. If they are valid for the actual resolution / desctopssize you can set them. Otherwise you may set the position and size to default values.

Chris

Namenloser 31. Mai 2010 15:42

Re: How to handle window move?
 
Just check if the coordinates are out of bound and reset them to a default value if so. Storing the desktop resolution is overkill imo.


Alle Zeitangaben in WEZ +1. Es ist jetzt 09:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz