Jump to content
Sign in to follow this  
jarto

Making a resizable and draggable window

Recommended Posts

With the latest fixes in the development-channel, it's child's play to make a resizable and draggable window:

  1. Add a panel in the form (BottomPanel)
  2. Add another panel inside BottomPanel (TitlePanel) and set its Align to alTop
  3. Add yet another panel inside BottomPanel (ContentPanel) and set its Align to alClient

Then code:

unit Form1;

interface

uses 
  System.Types, System.Types.Convert, System.Objects, System.Time,
  System.IOUtils, System.Device.Storage,
  SmartCL.System, SmartCL.Time, SmartCL.Graphics, SmartCL.Components,
  SmartCL.FileUtils, SmartCL.Device.Storage, SmartCL.Forms, SmartCL.Fonts,
  SmartCL.Theme, SmartCL.Borders, SmartCL.Application,
  SmartCL.Controller.EdgeSense, SmartCL.Controls.Panel;

type
  TForm1 = class(TW3Form)
  private
    {$I 'Form1:intf'}
    FResizeCursor: TCursor;
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

{ TForm1 }

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  var ESense:=TW3EdgeSenseController.Create(BottomPanel);
  ESense.EdgeSize:=5;
  ESense.Attach(BottomPanel);

  TitlePanel.OnMouseDown:=lambda
    TitlePanel.SetCapture;
  end;
  TitlePanel.OnMouseUp:=lambda
    TitlePanel.ReleaseCapture;
  end;
  TitlePanel.OnAllMovement:=procedure(Sender: TObject; dx,dy: Integer)
    begin
      BottomPanel.MoveTo(BottomPanel.Left+dx,BottomPanel.Top+dy);
    end;

  BottomPanel.OnMouseDown:=lambda
    FResizeCursor:=BottomPanel.Cursor;
    BottomPanel.SetCapture;
  end;
  BottomPanel.OnMouseUp:=lambda
    BottomPanel.ReleaseCapture;
  end;
  BottomPanel.OnAllMovement:=procedure(Sender: TObject; dx,dy: Integer)
    begin
      var PRect:=BottomPanel.BoundsRect;
      case FResizeCursor of
        crNResize: PRect.Top:=PRect.Top+dy;
        crSResize: PRect.Bottom:=PRect.Bottom+dy;
        crEResize: PRect.Right:=PRect.Right+dx;
        crWResize: PRect.Left:=PRect.Left+dx;
        crNEResize: begin PRect.Top:=PRect.Top+dy; PRect.Right:=PRect.Right+dx; end;
        crNWResize: begin PRect.Top:=PRect.Top+dy; PRect.Left:=PRect.Left+dx; end;
        crSEResize: begin PRect.Bottom:=PRect.Bottom+dy; PRect.Right:=PRect.Right+dx; end;
        crSWResize: begin PRect.Bottom:=PRect.Bottom+dy; PRect.Left:=PRect.Left+dx; end;
      end;
      BottomPanel.SetBounds(PRect.Left,PRect.Top,PRect.Width,PRect.Height);
    end;
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;
 
procedure TForm1.Resize;
begin
  inherited;
end;
 
initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×