how Anchors := [akCenter]?



How can I center a control within its parent? And how to keep it centered when the parent resizes?

I know I could write a Parent.OnResize event handler to move the control manually, but not all container components have an OnResize event, and I would like to do this automatically.

Is there an akCenter anchor?





The anchor akCenter does not exist. But you can mimic akRelative by setting the anchors property empty which results in relative movement of the control when the parent resizes. Combining that with centered placement will result in a centered-alike “anchor”.

To center horizontally at designtime:

  • In the IDE, choose the context menu of the control (right click),
  • Position > Align > Horizontal > Center in Window,
  • Set Anchors.akLeft := False, Anchors.akRight := False in the object inspector,
  • Et voilà : akHorzCenter.

The same goes for vertical, in which case you set akTop and akBottom false. To center in both directions: Anchors := [].

Animation of akRelative in action

Disclaimer: I have not found documentation of this anchors usage and I do not know whether this is the intended functioning, but it works very nice in D7 as well as in XE2.


Facebook Comments

Post a comment