Sign in Join
Wiki copytemporary

Anchor Sides/ja: what changed

Revision 3 to 4. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11コントロールのレイアウトを自動的に行うために、いくつかの新しいプロパティ、メソッドが存在します。コントロールの座標を、他のコントロールから一定の距離を保つように設定したり、他のコントロールを基準にして中央揃えにしたりすることができます。下記の例を参照してください。
22
33Each of the four sides of a control (Left, Top, Right, Bottom) can now be anchored/bound to a side of another control. For example you can now anchor the left side of TEdit to the right side of a TLabel. Everytime the Label is moved or resized the Edit's left side will follow, which normally results in moving the Edit parallel to the Label.
44
55# 例 1
66
7-```
7+```text
88+--------+ +-------+
99| Label1 | | Edit1 |
1010+--------+ | |
1111 +-------+
1212```
1313
1414## コーディング内容
1515
1616``` pascal
1717Edit1.AnchorSide[akLeft].Side := asrRight;
1818Edit1.AnchorSide[akLeft].Control := Label1;
1919Edit1.Anchors := Edit1.Anchors + [akLeft];
2020```
2121
2222BorderSpacing プロパティで距離を指定できます。
2323
2424``` pascal
2525Edit1.BorderSpacing.Left := 10;
2626```
2727
2828メソッドを使うことでもできます。
2929
3030``` pascal
3131Edit1.AnchorToNeighbour(akLeft, 10, Label1);
3232```
3333
3434## Notes
3535
3636The Edit1.Left will follow Label1.Left+Label1.Width, not the other way around. That means, moving Label1 will move Edit1. But moving Edit1 will be undone by the LCL. If you also anchor the right side of Label1 to the left side of Edit1, you created a circle, and this can result together with some other autosize properties in a loop. This is ignored by the LCL or automagically repaired when the Parent.AutoSize is true.
3737
3838## Via the Anchor Editor
3939
4040The anchor editor is a floating window that is available via the menu **View / Anchor Editor** or via the button on the **Anchors** property in the object inspector.
4141
4242![](/wiki-images/6/6d/Anchoreditor.png)
4343
4444# 例 2
4545
4646You can anchor the Edit's top side to follow the Label's top side:
4747
4848 +--------+ +-------+
4949 | Label1 | | Edit1 |
5050 +--------+ | |
5151 +-------+
5252
5353``` pascal
5454Edit1.AnchorSide[akTop].Side := asrTop;
5555Edit1.AnchorSide[akTop].Control := Label1;
5656Edit1.Anchors := Edit1.Anchors + [akTop];
5757```
5858
5959メソッドを使うことでもできます。
6060
6161``` pascal
6262Edit1.AnchorParallel(akTop,0,Label1);
6363```
6464
6565# 例 3
6666
6767Centering a Label vertically to an Edit:
6868
6969 +-------+
7070 +--------+ | |
7171 | Label1 | | Edit1 |
7272 +--------+ | |
7373 +-------+
7474
7575``` pascal
7676Edit1.AnchorSide[akTop].Side := asrCenter;
7777Edit1.AnchorSide[akTop].Control := Label1;
7878Edit1.Anchors := Edit1.Anchors + [akTop] - [akBottom];
7979```
8080
8181メソッドを使うことでもできます。
8282
8383``` pascal
8484Edit1.AnchorVerticalCenterTo(Label1);
8585```
8686
8787Obviously anchoring the bottom side of Edit1 does not make sense when centering.
8888
8989# 新しいプロパティ
9090
9191``` pascal
9292property AnchorSide[Kind: TAnchorKind]: TAnchorSide read GetAnchorSide;
9393```
9494
9595This is not published in the [[IDE Window: Object Inspector|object inspector]]. You can edit it in the designer via the [[IDE Window: Anchor Editor|anchor editor]] (Menu: View -\> View anchor editor, or click on button of 'Anchors' property).
9696
9797# New methods to easily configure common layouts
9898
9999``` pascal
100100procedure AnchorToNeighbour(Side: TAnchorKind; Space: integer;
101101 Sibling: TControl);
102102procedure AnchorParallel(Side: TAnchorKind; Space: integer;
103103 Sibling: TControl);
104104procedure AnchorHorizontalCenterTo(Sibling: TControl);
105105procedure AnchorVerticalCenterTo(Sibling: TControl);
106106procedure AnchorAsAlign(TheAlign: TAlign; Space: Integer);
107107```
108108
109109AnchorVerticalCenterTo works with Parent too. Then it will center on the client area, that means the center of the control is at *ClientHeight div 2*.
110110
111111Center anchoring is not yet fully supported when computing the size of the parent. For example when you put a label center anchored into a Groupbox1 and set Groupbox1.AutoSize to true then Groupbox1 height will shrink leaving no space for the label. The solution is to center to a control that is not centered. For example center a Label1 to a ComboBox and use for the ComboBox the default anchors (Anchors=\[akLeft,akTop\]).
112112
113113# Anchoring to invisible controls
114114
115115Since 0.9.25 rev 12800 anchoring to invisible controls has changed to work more intuitively. For example: The below controls A, B, C are anchored (C.Left to B.Right and B.Left to A.Right):
116116
117-```
117+```text
118118 +---+ +---+ +---+
119119 | A | | B | | C |
120120 +---+ +---+ +---+
121121```
122122
123123If B is hidden (Visible:=false) then C.Left will skip B and use the right of A, resulting in
124124
125-```
125+```text
126126 +---+ +---+
127127 | A | | C |
128128 +---+ +---+
129129```
130130
131131# Circular references
132132
133133You can build circular references by anchoring two sides to each other, which creates an impossible anchoring. The LCL notices that, but will not raise an exception, because it can be a temporary circle. For example: A row of buttons. Button1 is left of Button2, Button2 is left of Button3. Now the order is changed to: Button3, Button1, Button2. If the reanchoring is started with Button3, a circle is created temporarily and is fixed when the reordering is completed. The LCL detects circles and will not move the Button. Circles are not the only anomaly.
134134
135135There is one exception to this rule. If the Parent.AutoSize is true, then the LCL will automatically break circles on autosize and writes a warning via debugln (windows: --debug-log.txt, linux, et al: stdout). Which anchor is broken depends on the order of controls and the sides. So, temporary circles are still allowed with AutoSize=true if you enclose the change in
136136
137137``` pascal
138138Parent.DisableAutosizing;
139139try
140140 // change anchors, aligns, bounds...
141141finally
142142 Parent.EnableAutosizing;
143143end;
144144```
145145
146146The AutoSize algorithm does not only break circles, but fixes Align/AnchorSide inconsistencies too.
147147
148148Eventually it would be good to add some hints in the designer or a tool to list the circles and inconsistencies.
149149
150150# 関連情報
151151
152152- [[Autosize / Layout]]
153153- [[Example: Anchors. How to reliably align dynamically created controls under changing visibility]]
154154
155155---
156156*From the Free Pascal wiki, page "Anchor Sides/ja", as it stood on 2020-02-09 after a change by GAN, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history