Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict minimum form's width in FireMonkey?

How do I restrict a minimum form's width in FireMonkey? It used to be so easy in VCL - it just had Max and Min constraints in forms properties.

like image 865
tdog2 Avatar asked Nov 07 '11 00:11

tdog2


5 Answers

Just found out TForm has a Constraints property in Delphi 11.

demo

Works perfectly for me without flickering.

like image 106
Bas van der Linden Avatar answered Oct 20 '22 21:10

Bas van der Linden


Below is an updated version to Sunec's answer, to get rid of flickering.

According to MSDN Mouse_Event has been superseded and SendInput should be used instead: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event

uses WinApi.Windows;

procedure TForm1.FormResize(Sender: TObject);
var
  LInput: TInput;
begin
  if ClientHeight < MIN_HEIGHT then
  begin
    ClientHeight := MIN_HEIGHT;
    FillMemory(@LInput, SizeOf(LInput), 0);
    LInput.Itype := INPUT_MOUSE;
    LInput.mi.dwFlags := MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP;
    SendInput(1, LInput, SizeOf(LInput));
  end;
  if ClientWidth < MIN_WIDTH then
  begin
    ClientWidth := MIN_WIDTH;
    FillMemory(@LInput, SizeOf(LInput), 0);
    LInput.Itype := INPUT_MOUSE;
    LInput.mi.dwFlags := MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP;
    SendInput(1, LInput, SizeOf(LInput));
  end;
end;
like image 37
Batsheva Illfeld Avatar answered Oct 20 '22 20:10

Batsheva Illfeld


LaKraven, simulate a mouseUp event to get rid of that flickering.

if (Width > maxWidth) then
begin
  Width := maxWidth;
  Mouse_Event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
end;
like image 34
Sunec Avatar answered Oct 20 '22 21:10

Sunec


Place this on the form's "OnResize" event, replace the values as appropriate. Granted, not the best solution in the world, but it'll get you by until the properties are reintroduced!

procedure TForm1.FormResize(Sender: TObject);
begin
  if Width < 400 then
    Width := 400;
  if Height < 400 then
    Height := 400;
end;

The above code is easy enough to change for any combination of maximums or minimums, so have fun!

like image 24
LaKraven Avatar answered Oct 20 '22 22:10

LaKraven


Additionally for LaKraven's answer about FormResize based solution, use ClientWidth and ClientHeight instead of Width and Height to prevent stretching of the form.

procedure TForm1.FormResize(Sender: TObject);
begin
    if ClientWidth < 400 then
        ClientWidth := 400;
    if ClientHeight < 400 then
        ClientHeight := 400;
end;
like image 42
idearibosome Avatar answered Oct 20 '22 21:10

idearibosome