1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
//! Values for horizontal and vertical alignement of a form on another control
typedef enum { afthLeft =-1,
afthCenter= 0,
afthRight =+1,
afthIgnore=+2 } EHorizontalAlignFormTo;
//! Values for vertical alignement of a form on another control
typedef enum { aftvTop =-1,
aftvMiddle= 0,
aftvBottom=+1,
aftvIgnore=+2 } EVerticalAlignFormTo;
//---------------------------------------------------------------------------
//! Align a form on a parent form
/*! \param formToAlign is the form to change the position of
\param modelControl is the master control to get the position as template for
alignement (if NULL, then use screen coordinated and center
the form in the screen with the required margin values)
\param hPosition is the horizontal alignement position (-1 value to
align the form on the left, zero to center and +1
value to align right, other value=no alignement)
\param vPosition is the same for vertical alignement (-1=top, 0=middel,
+1=bottom, other values=no alignement)
\param leftMargin is the margin to let between the left border of the
form and the closest border of the master
\param rightMargin is the margin to let between the right border of the
form and the closest border of the master
\param topMargin is the margin to let between the top border of the form
and the closest border of the master
\param bottomMargin is the margin to let between the bottom border of the
form and the closest border of the master
\param minWidth is the minimum width of the form after alignement (ignored
if negative)
\param minHeight is the minimum height of the form after alignement (ignored
if negative)
\param maxWidth is the maximum width of the form after alignement (ignored
if negative)
\param maxHeight is the maximum height of the form after alignement (ignored
if negative)
*/
void PACKAGE AlignFormTo(TForm* formToAlign,const TControl* modelControl,
EHorizontalAlignFormTo hPosition,
EVerticalAlignFormTo vPosition,
int leftMargin,int rightMargin,
int topMargin,int bottomMargin,
int minWidth,int minHeight,
int maxWidth,int maxHeight) {
// Check parameters
if (formToAlign==NULL) return;
// Get the dimensions of the master form (or screen)
TRect master;
if (modelControl) {
master=const_cast<TControl*>(modelControl)->BoundsRect;
if (modelControl->Parent) {
TPoint topleft(master.left,master.top);
topleft=const_cast<TWinControl*>(modelControl->Parent)->ClientToScreen(topleft);
master.left=topleft.x;
master.top =topleft.y;
}
} else {
master=Screen->WorkAreaRect;
hPosition=vPosition=0;
}
// Get the dimensions of the form to align
TRect form=formToAlign->BoundsRect;
// Horizontal alignement
if (hPosition==-1) { int w=form.Width(); form.left=master.left-form.Width()-rightMargin; form.right=form.left+w; }
else if (hPosition==+1) { int w=form.Width(); form.left=master.left+master.Width()+leftMargin;form.right=form.left+w; }
else if (hPosition== 0) {
form.left =master.left+leftMargin;
form.right=master.left+master.Width()-rightMargin;
}
// Vertical alignement
if (vPosition==-1) { int h=form.Height(); form.top=master.top-form.Height()-bottomMargin;form.bottom=form.top+h; }
else if (vPosition==+1) { int h=form.Height(); form.top=master.top+master.Height()+topMargin; form.bottom=form.top+h; }
else if (vPosition== 0) {
form.top =master.top+topMargin;
form.bottom=master.top+master.Height()-bottomMargin;
}
// Bound form with screen
TRect screen=Screen->WorkAreaRect;
if (form.left>screen.Width()) { int w=form.Width(); form.left=screen.Width()-form.Width(); form.right=form.left+w; }
if (form.left<0) { form.left=0; form.right=std::max((long)0,form.right); }
if (maxWidth>=0) { form.right =form.left+std::min(form.Width(),maxWidth); }
if (minWidth>=0) { form.right =form.left+std::max(form.Width(),minWidth); }
if (form.right>screen.Width()) { int w=form.Width(); form.left=form.left-(form.right-screen.Width()); form.right=form.left+w; }
if (form.top>screen.Height()) { int h=form.Height(); form.top=screen.Height()-form.Height(); form.bottom=form.top+h; }
if (form.top<0) { form.top=0; form.bottom=std::max((long)0,form.bottom); }
if (maxHeight>=0) { form.bottom=form.top+std::min(form.Height(),maxHeight); }
if (minHeight>=0) { form.bottom=form.top+std::max(form.Height() ,minHeight); }
if (form.bottom>screen.Height()) { int h=form.Height(); form.top=form.top-(form.bottom+screen.Height()); form.bottom=form.top+h; }
// Assign new dimensions of the form
formToAlign->Width =form.Width(); formToAlign->Left=form.left;
formToAlign->Height=form.Height(); formToAlign->Top =form.top;
// Call resize function
if (formToAlign->OnResize) formToAlign->OnResize(NULL);
} |
Partager