tags:

views:

68

answers:

1

If I am using Delphi at design time, then when I copy/paste a group of components at design time, the pasted components are slightly offset from their originals.

However, I am developing a GUI which allows users to develop a GUI for another app. When I cut/paste the pasted components lie directly over the originals. Is there are way to offset them slightly?

+3  A: 

Since you have the basic copy/paste working, the next bit is a fairly simple extension to what you already have.

Pasting does of course - presumably - paste the copied controls with their copied property values, so yes they will over-lay on top of the original controls from which they were copied.

To get off-setting behaviour, you will have to do some "nudging" of the pasted controls after they have been pasted. If you cannot get a list of the pasted controls more directly, then one way to derive such a list would be to take a copy of the Components collection of the target form before pasting, then after pasting iterate over the Components collection once again - any item now in the collection and not in the "original" list must have been pasted and you can apply your Left/Top nudges to those as required.

  list := TList.Create;
  try
    for i := 0 to Pred(dest.ComponentCount) do
      list.Add(dest.Components[i]);

    // Do your pasting

    for i := 0 to Pred(dest.ComponentCount) do
      if list.IndexOf(dest.Components[i]) = -1 then
        // Nudge dest.Components[i]
  finally
    list.Free;
  end;

That should get you at least heading down the right direction I think.

NOTE: The above code assumes you are dealing (potentially) with TComponent derived classes (i.e. non-visual components). If you are dealing only with TControl descendants then you could optimise by working with the ControlCount and Controls of the destination container control (not necessarily the form).

Also, the Left/Top position of a TComponent is stored in the lo/hi word of the public DesignInfo property, so your nudge code will need to take that into account and deal with TComponent derived controls differently from TControl if you are working with non-visual components as well as visual controls.

Deltics
+1 that looks great - I am off to try it. Thanks
Mawg
Mawg