Recent

Author Topic: Get a form height, width, top, left poperties  (Read 8264 times)

NelsonN

  • Jr. Member
  • **
  • Posts: 69
    • Beam Me Up!
Get a form height, width, top, left poperties
« on: October 03, 2011, 07:08:35 pm »
Is there a way to get a form's height, width, top, left values prior to a maximize or minimize?

I can get these but only if I set the windows to wsNormal again with RestoredWidth, RestoredHeigh, RestoredTop, RestoredLeft.

Obviously the above is is a kludge and will show the form being normalized, if I set the Visible property to false prior to wsNormal, I get incorrect values.

This is for a cross platform app, so I am trying to not go for API calls specific platforms.

Any help is appreciated.
Lazarus trunk / FPC 3.0.4 / 32-bit and 64-bit with Windows 10.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Get a form height, width, top, left poperties
« Reply #1 on: October 03, 2011, 07:32:28 pm »
Make your own fields:  MyHeight and MyWidth and set them in method OnResize:
Code: [Select]
procedure TForm1.FormResize(Sender: TObject);
begin
  if WindowState=wsNormal then
    begin
      MyHeight:=Height;
      MyWidth:=Width;
    end;
end;   
you will always know size of wsNormal form. (The same for Top & Left).
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

NelsonN

  • Jr. Member
  • **
  • Posts: 69
    • Beam Me Up!
Re: Get a form height, width, top, left poperties
« Reply #2 on: October 03, 2011, 07:44:05 pm »
I tried that and more, including in the WindowStateChange event.

It seems that on FormResize is not even called when maximizing in some instances.

This is the reason I want to do it prior to any maximize or minimize, I need a way to intercept that and grab the those property values myself.

Update: I just tried it again, it seems that even when the form is maximized with the above code the WindowState = wsNormal is true and gets the current values of the maximized window. (tested in Windows)
« Last Edit: October 03, 2011, 07:48:55 pm by NelsonN »
Lazarus trunk / FPC 3.0.4 / 32-bit and 64-bit with Windows 10.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Get a form height, width, top, left poperties
« Reply #3 on: October 03, 2011, 07:52:59 pm »
Maybe store the values only when windowstate is not maximized or minimized.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Get a form height, width, top, left poperties
« Reply #4 on: October 03, 2011, 08:13:45 pm »
It works on Linux+Qt.
Code: [Select]
procedure TForm1.FormResize(Sender: TObject);
begin
  if WindowState=wsNormal then
    begin
      writeln('h'+inttostr(Height));
    end;
end;   
I have verticaly 1050 pixels. When I start wsNormal Form, output is 812. When I maximalize and back, it writes 812 again. When I minimalize and back, nothing happens.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

NelsonN

  • Jr. Member
  • **
  • Posts: 69
    • Beam Me Up!
Re: Get a form height, width, top, left poperties
« Reply #5 on: October 03, 2011, 08:29:02 pm »
It works on Linux+Qt.
Code: [Select]
procedure TForm1.FormResize(Sender: TObject);
begin
  if WindowState=wsNormal then
    begin
      writeln('h'+inttostr(Height));
    end;
end;   

I got code working in the OnWindowStateChange event:

Code: [Select]
  if WindowState = wsNormal then
  begin
    cRestoredTop := Top;
    cRestoredLeft := Left;
    cRestoredWidth := Width;
    cRestoredHeight := Height;
  end;

The above will not work in Mac OS X. When I go to save the values they are those of the Maximized window, or close to them, for some odd reason.

So I am left with setting the Form to wsNormal prior to saving the values (the correct values). It seems that depending on the widgetset, things don't always seem to work the same. I would attribute that to bugs. This is a cross platform app, so I have to get the behavior the same on all of them: Windows, Linux (GTK2), and Mac OS X.

Also, I noticed that Top and Left values are not always returned exactly, they can have negative values, I see there is some bug reports on that.
Lazarus trunk / FPC 3.0.4 / 32-bit and 64-bit with Windows 10.

NelsonN

  • Jr. Member
  • **
  • Posts: 69
    • Beam Me Up!
Re: Get a form height, width, top, left poperties
« Reply #6 on: October 04, 2011, 12:38:09 am »
There are several reasons why when maximizing or minimizing a window and trying to get the values via FormResize or WindowState events doesn't work to try and get the correct wsNormal values after, I force a wsNormal prior to closing the window.

There is an issue with the GTK version:

http://bugs.freepascal.org/view.php?id=8576

This code, in the FormClose event, is what I am using to get the values, but it forces me to do a wsNormal if the window is maximized or minimized:

Code: [Select]
  if (WindowState = wsMaximized) or (WindowState = wsMinimized) then
  begin
    if WindowState = wsMaximized then
      cMaximized := 1;

    WindowState := wsNormal;

    {$IFDEF LGLGTK2}
    cHeight := RestoredHeight;
    cWidth := RestoredWidth;
    cTop := RestoredTop;
    cLeft := RestoredLeft;
    {$ELSE}
    cHeight := Height;
    cWidth := Width;
    cTop := Top;
    cLeft := Left;
    {$ENDIF}
  end

The GTK version can't grabbed the Height, etc., it needs the Restored values. How odd.

Some reasons for doing it my way are:

1) If the app opens with the windows maximized, there is no Restored values, they are zero. (For first time run height, etc., are set in the code or can be read from the config file prior to running the app).

2) If you move the window and then maximize or minimize, and never restore the window, the Restored values will never be had. (Moving a window around on the screen never calls FormResize or WindowState events).

There are a few more. All this for just trying to get the value to store in a state config file for when the next time the app is opened your windows will be in the same spot (or maximized).
Lazarus trunk / FPC 3.0.4 / 32-bit and 64-bit with Windows 10.

 

TinyPortal © 2005-2018