ControlMargins (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demostrates the usage of the Margins and AlignWithMargins properties. Note that Margins will only work when AlignWithMargins is set to True and Align is not alNone.


Code

#define TopPanelMargin 10
#define BottomPanelMargin 20

TPanel *TopPanel, *BottomPanel;

__fastcall TForm2::TForm2(TComponent* Owner)
	: TForm(Owner)
{
	/* Create a new panel and align it to top. */
	TopPanel = new TPanel(this);
	TopPanel->Parent = this;
	TopPanel->Align = alTop;
	TopPanel->Height = (ClientHeight / 2);

	/* Create a new panel and align it to the remaining client size. */
	BottomPanel = new TPanel(this);
	BottomPanel->Parent = this;
	BottomPanel->Align = alClient;

	/* Set Top panel margins .*/
	/* Note: Margins will not have an effect if Align is alNone. */
	TopPanel->Margins->SetBounds(TopPanelMargin, TopPanelMargin,
								 TopPanelMargin, TopPanelMargin);
	TopPanel->AlignWithMargins = true;
	TopPanel->Caption = "TopPanel";

	/* Set Top Bottom margins. */
	/* Note: Margins will not have an effect if Align is alNone. */
	BottomPanel->Margins->SetBounds(BottomPanelMargin, BottomPanelMargin,
								   BottomPanelMargin, BottomPanelMargin);
	BottomPanel->AlignWithMargins = true;
	BottomPanel->Caption = "BottomPanel";

	if ((TopPanel->Top != TopPanelMargin) ||
	   (TopPanel->Left != TopPanelMargin) ||
	   (TopPanel->Width != (ClientWidth - (TopPanelMargin * 2))) ||
	   (BottomPanel->Top != (TopPanel->Height + (TopPanelMargin * 2) +
											   BottomPanelMargin)) ||
	   (BottomPanel->Left != BottomPanelMargin) ||
	   (BottomPanel->Width != (ClientWidth - (BottomPanelMargin * 2))))
	   {
		  MessageDlg(String(
			"This should not happen! Position and size are ")+
			"calculated relative to the margins",
			mtError, TMsgDlgButtons() << mbOK, 0);
	   }
}

Uses