delphidesignerdelphi-2007form-designer

Equivalent to designer guidelines in code


The VCL form designer offers pink guidelines for aligning controls at their respective text base lines: Guidelines in form designer
But as far as I can tell this doesn't work for labels and checkboxes. Update: It works for labels if you place the controls exactly, e.g. by Ctrl-arrow. It kind of works for checkboxes - see screenshot.

Now, on some forms I'm creating controls in code, e.g.

ed := TEdit.Create(Self);
ed.SetBounds(...);
ed.Parent := SomePanel;

etc. How can I ensure that their text base lines are aligned? I'd like to have this for edits, comboboxes, labels and checkboxes. The result should look like this (without the red line, of course :-)): base line aligned

Edit: My current approach is to call something like AlignTop(8, [Edit1, ComboBox1], [CheckBox1, Label1]); with

procedure ControlArray_SetTop(const AControls: array of TControl; ATop: Integer);
var
  i: Integer;
begin
  for i := Low(AControls) to High(AControls) do
    AControls[i].Top := ATop;
end;

procedure AlignTop(ATop: Integer; const AControls: array of TControl; const ALabelLikeControls: array of TControl);
begin
  ControlArray_SetTop(AControls, ATop);
  ControlArray_SetTop(ALabelLikeControls, ATop + 3);
end;

My goal is to replace it with something more robust and less hacky.


Solution

  • The guidelines are implemented in designtime code which license prohibits you to ship with your app so you can only use it to learn from it and then reimplement it yourself. Look up

    DesignIntf.TBaseComponentGuidelines
    DesignEditors.TComponentGuidelines
    VCLEditors.TControlGuidelines
    

    classes (in "{RADStudio\version}\source\ToolsAPI directory"). Perhaps it comes down to something simple as

    Label1.Top := (Edit1.Top + Edit1.Height) - Label1.Height + GetMagicConstant;  
    

    where GetMagicConstant is similar to TControlGuidelines.GetTextBaseline().