MENU

Fun & Interesting

Delphi: Let's talk about Alignment, Anchors, Constraints, Margins, Padding properties of components

Emailx45 48 lượt xem 1 month ago
Video Not Working? Fix It Now

Use Align to align a control to the top, bottom, left, or right of a form or panel and have it remain there even if the size of the form, panel, or component that contains the control changes. When the parent is resized, an aligned control also resizes so that it continues to span the top, bottom, left, or right edge of the parent.

For example, to use a panel component with various controls on it as a tool palette, change the panel's Align value to alLeft. The value of alLeft for the Align property of the panel guarantees that the tool palette remains on the left side of the form and always equals the client height of the form.

The default value of Align is alNone, which means a control remains where it is positioned on a form or panel.

Tip: If Align is set to alClient, the control fills the entire client area so that it is impossible to select the parent form by clicking it. In this case, select the parent by selecting the control on the form and pressing ESC, or by using the Object Inspector.
Any number of child components within a single parent can have the same Align value, in which case they stack up along the edge of the parent. The child controls stack up in z-order. To adjust the order in which the controls stack up, drag the controls into their desired positions.

Note: To cause a control to maintain a specified relationship with an edge of its parent, but not necessarily lie along one edge of the parent, use the Anchors property instead.

If AlignWithMargins is true, use the Margins property of the control to govern the spacing relative to other controls that are aligned with this one. The controls are not allowed to be any closer than the spacing specified in Margins. This spacing is maintained as controls are moved when the parent control resizes.

AlignWithMargins is true if the style csAlignWithMargins, a TControlStyle type, is in the control's ControlStyle property, and false otherwise.

Use Anchors to ensure that a control maintains its current position relative to an edge of its parent, even if the parent is resized. When its parent is resized, the control holds its position relative to the edges to which it is anchored.

If a control is anchored to opposite edges of its parent, the control stretches when its parent is resized. For example, if a control has its Anchors property set to [akLeft, akRight], the control stretches when the width of its parent changes.

Anchors is enforced only when the parent is resized. Thus, for example, if a control is anchored to opposite edges of a form at design time and the form is created in a maximized state, the control is not stretched because the form is not resized after the control is created.

Note: If a control should maintain contact with three edges of its parent (hugging one side of the parent and stretching the length of that side), use the Align property instead. Unlike Anchors, Align allows controls to adjust to changes in the size of other aligned sibling controls as well as changes to the parent's size.

Use the Margins property to set the margins of the control.

If AlignWithMargins is true, then the Margins property of the control governs the spacing relative to other controls that are aligned with this one. The controls are not allowed to be closer than the spacing specified in Margins.

Note: The Margins you set for the control have effect only when AlignWithMargins is set to true and Align is not set to alNone.

TPadding is used in the Padding property of TControl and its descendants. It specifies the padding of a control.

Padding adds space along the edge the control. Child controls that are aligned to the parent are positioned inside the control according to this spacing. Padding does not affect child controls which are not aligned to the parent control, nor does it affect the size of the ClientArea.

Padding is the opposite of Margins. Margins affects the positioning of the control itself inside the parent control, but Padding affects how all aligned child controls are positioned with respect to the parent control.

Comment