Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

delphi - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...