1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

How to Add Size Handles to Controls being Resized at Run-Time
When you allow a user to move and resize controls on a Delphi form, at run-time using the mouse, to fully mimic the design-time environment, you should add eight size handles to the control being resized. Here's how.
 More of this Feature
• Download Project Source

• Part 1
• Part 3
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
 Related Resources
• Using Delphi components
• Owner vs. Parent
• TScreen object
• Mouse events
• Dragging and dropping

While in most situations you will arrange all the controls on a Delphi form in a "fixed" position, there are situations where you need to allow a user to change the placement and dimension of controls at run-time. Here's how to enable dragging and resizing controls with mouse, while the application is running.

Sizing handles code by Alan Evans, article text by Zarko Gajic

Size Handles?
When working in the Delphi IDE (at design-time), to resize a control on a form, you click the control to be resized and drag one of the eight sizing handles.
If you allowed users of your application to move and resize controls at run-time, you might want to add those 8 sizing handles to the control being resized...

In theory...
"Drawing" eight sizing handles involves creating 8 TPanel objects and placing them *around* the control being selected.

Eight Size Handles... in practice
We'll start with the code provided in the previous part of this article.

8 Size Handles at Run-Time

The overriden Create constructor calls the CreateNodes function:

procedure TForm1.CreateNodes;
  Node: Integer;
  Panel: TPanel;
  for Node := 0 to 7 do
    Panel := TPanel.Create(Self);
    with Panel do
      BevelOuter := bvNone;
      Color := clBlack;
      Name := 'Node' + IntToStr(Node);
      Width := 5;
      Height := 5;
      Parent := Self;
      Visible := False;

      case Node of
        0,4: Cursor := crSizeNWSE;
        1,5: Cursor := crSizeNS;
        2,6: Cursor := crSizeNESW;
        3,7: Cursor := crSizeWE;
      OnMouseDown := NodeMouseDown;
      OnMouseMove := NodeMouseMove;
      OnMouseUp := NodeMouseUp;
end; (*CreateNodes*)

CreateNodes in short: a private FNodes variabe of type TObjectList is added to the form (and instantiated in the Create constructor):

  FNodes := TObjectList.Create(False);

Each of the 8 panels is assigned 3 mouse related event handling procedures (NodeMouseDown, NodeMouseMove, NodeMouseUp) that track which control is being resized and provide the programming logic to size the control.

Download the sample application and try for yourself

Key presses being processed for TCustomeEdit / TCustomMemo descendants?
You might have noticed that the edit box still accepts keys while in the reposition mode. Here's how to temporary disable key presses, when resizing and moving is enabled:

1. Set Form1.KeyPreview to TRUE (either using Object Inspector or from code)
2. Handle the OnKeyPress to "eat" all key presses if moving or resizing is taking place:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
  // "eat" keypresses in moving and resizing controls
  // Form1.KeyPreview = TRUE !!
  if chkPositionRunTime.Checked then Key := #0;
  //or if inReposition then Key := #0;

Note: A form's KeyPreview property determines whether form keyboard events are invoked before component keyboard events. The form can receive any keystrokes that the focused component can receive. To trap keystrokes at the form level instead of passing them to the form's components, set the form's KeyPreview property to True.
Read the "Keyboard symphony" article to learn more about keyboard related events.

If you need any kind of help at this point, please post to the Delphi Programming Forum where all the questions are answered and beginners are treated as experts.

Next part > Dynamically swap event handling procedures > Part 1, 2, 3

©2017 About.com. All rights reserved.