Anchors := [akCenter]?

天大地大妈咪最大 提交于 2019-11-26 20:56:25

问题


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?


回答1:


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 := [].

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.



来源:https://stackoverflow.com/questions/12614453/anchors-akcenter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!