Forum > LCL

Distinguish between a form resize through code and a user-initiated resize

(1/3) > >>

trab10:
I have a form that automatically resizes (height) based on the varying contents of a listbox.  However, if the user manually sets the form height (by dragging with the mouse), then the program should respect that choice and stop the auto-sizing behavior.

I've set up a private boolean called ManualSized, set to False at default, and have a handler for the form's OnResize event to change ManualSized to True.  The auto-resize code looks at the boolean, and skips resizing if it's True, but OnResize is called regardless of whether the resize is from User action, or from code, so once it "autoresizes", ManualSized is set to True, and no more auto-sizing happens.

Is there a way to distinguish between a coded form resize and a user-initiated resize?

Thanks,

bart

Lazarus 3.2 release version, FPC 3.2.2 Win64
Windows 10

Handoko:
Hello trab10,
Welcome to the forum.

This works on Ubuntu, I haven't tried but I believe it should work on Windows too:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, Forms, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormResize(Sender: TObject);  private    FCodeResizeEnabled: Boolean;    FResizingByCode:    Boolean;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  if not(FCodeResizeEnabled) then  begin    ShowMessage('User resizing already performed,' + LineEnding +      'code resizing is not allowed');    Exit;  end;  FResizingByCode := True;  Width           := Random(400) + 400;  Height          := Random(300) + 300;  Application.ProcessMessages;  FResizingByCode := False;end; procedure TForm1.FormCreate(Sender: TObject);begin  FCodeResizeEnabled := True;  FResizingByCode    := False;end; procedure TForm1.FormResize(Sender: TObject);const  FirstTime: Boolean = True;begin  if FirstTime then  begin    FirstTime := False;    Exit;  end;  if not(FResizingByCode) then // resizing is perform by user    FCodeResizeEnabled := False;end; end.

jamie:
Read this.

https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-sizing

The LCL does not process this message but, you can add it and process it yourself.

 This message gets sent when even the user is dragging or changing its size etc.

 if interested, I could supply a code snippet.

 This i am sure works only for windows.

trab10:
Handoko:
This seems to work, thank you.  It took some time to wrap my head around it, but I believe I understand how it works.  Very clever.

Jamie:
Thanks for the pointer to the Win SDK; I know Lazarus can do this, and it is the best way to accomplish some things, but I try to avoid it if possible.  I will try it, though, so please do send a code snippet.

Again, thanks all.

b

jamie:
I don't know if this will work on other targets. Although HitTest message is supported in other targets, because its in the LMessages unit, there does not seem to be any of the test area defines, which are in the windows unit.

In any case, here is a code snippet that uses the Onresize event and how you can tell if the user is doing it or code.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Windows,Classes, SysUtils, Forms, Controls, Graphics, Dialogs,Lmessages,lclType,types; type   { TForm1 }   TForm1 = class(TForm)    procedure FormResize(Sender: TObject);  private  public  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.FormResize(Sender: TObject);begin  if (GetKeyState(VK_LButton)and $8000<>0)and(SendMessage(Handle,LM_NCHITTEST,0,  MakeLong(Mouse.CursorPos.X,Mouse.CurSorPos.Y)) in  [HTBottom,HTLEFT,HTRIGHT,HTTOP,HTBOTTOMLEFT,HTBOTTOMRIGHT])  Then   Caption := 'user is sizing it' else   Caption := 'Code Is sizing it';end;  end.  

Navigation

[0] Message Index

[#] Next page

Go to full version