Creating a form editor in Delphi

My goal is to create a simple form editor, such as the one we found in the Delphi IDE.

Now the user can select and add components that make him the parent for TPanel, which is the form holder. For simplicity, please also consider TPanel as visual components added to the form.

I have 2 missing parts. I want to know ideas / code that will help to execute:

1 - how to move the created visual component? The same effect as in the IDE for moving a visual component, for example Tpanel, around its upper and left position 2 - how to draw these hooks for a component with focus on the form editor 3 - how to resize using hooks

I need a part related to processing the visual part. I am not creating DFM or anything like that.

+4
source share
3 answers

Simply put, your moving code should do this:

  • When the mouse goes down, check to see if the mouse position is above the control that you can drag. If so, set a variable named FDragControl to refer to this control. This code lives in the OnMouseDown event handler.
  • When the mouse moves, if FDragControl not zero, move the control. This code is in the OnMouseMove event handler.
  • When the mouse rises, set FDragControl to zero.

That’s almost all you need. The main caveat is that you must also remember the X, Y values ​​of the mouse when you start dragging. Therefore, in your OnMouseDown handler, you write:

 FStartMousePos := Point(X, Y); FStartDragControlPos := Point(FDragControl.Left, FDragControl.Top); 

And then in OnMouseMove your position code reads:

 FDragControl.Left := FStartDragControlPos.X + (X-FStartX); FDragControl.Top := FStartDragControlPos.Y + (Y-FStartY); 

You will also need to grab the mouse when you start to drag.

The resize code is the same. Again, you need to decide in OnMouseDown that you are resizing rather than dragging and dropping, but the code still includes mouse processing, moving, and events.

As for drawing, you need to force redrawing when one of your event handlers changes a property that will affect the appearance of your form. You can use the FDragControl value to decide whether to use a special drawing of your control and indicate that it is being dragged. And also for resizing.

I did not code a full working implementation, since your question is high and conceptual. The implementation is up to you.

+5
source

// I accepted this answer as I just read your last update, which really should have been done as editing the original question, but anyway.

You can download the Cindy Components Pack and use the cyResizer component, which will do almost everything you need and is also very customizable.

You can download it here: http://sourceforge.net/projects/tcycomponents/

+2
source

In search of an answer, you can find the following articles:

How to move and resize controls at runtime http://delphi.about.com/library/weekly/aa102505a.htm

How to add dimensional grips for controls that change at runtime http://delphi.about.com/library/weekly/aa110105a.htm

Pretty much with all the information to complete this task using the sample source code.

These articles demonstrate how to implement and use the TMover class. I did it and it works correctly.

I also downloaded the TcyComponents Pack and used TcyResizer. This is a full-featured form editor with almost everything you need for a Delphi form editor. I recommend. It comes with source code and works great with the XE2 version.

+1
source

Source: https://habr.com/ru/post/1480108/


All Articles