C + + Builder/delphi is inprise (formerly Borland) Company's popular Visual c++/pascal development tool, which can greatly accelerate the development of the application. However, because it is a visual programming tool, the position of the control is fixed dead when the control is dragged and dropped onto the form. As the size of the form or the screen resolution changes, the position of the control and form itself tends to become very confusing. This article describes some ways to properly position controls and form.
Change control location and size with onresize events
The form control in C + + Builder/delphi has a onresize event, and all of the form's actions related to the size change can trigger this event, including form creation, maximizing/minimizing/restoring, resizing by dragging the mouse, and so on. Therefore, changing the position of each control dynamically in this event ensures that it is positioned correctly relative to the form. Because Cbuilder and Delphi program have a lot in common, so here only list Cbuilder code.
The following code ensures that the control is centered in all cases.
Program One:
void __fastcall TForm1::FofmResize(TObject*Sender)
{
int midLoc=Width/2; //取Form的中点
Label1->Left=midLoc-Label1->Width/2; //设置Label1的位置为Form居中
Button1->Left=midLoc-Button1->Width/2; //设置Button1的位置为Form居中
}
By slightly modifying this code, you can keep the control where you want it. Similarly, you can use the OnResize event to change the size of the control. When there are many controls in the form, it is very cumbersome to individually position each control separately, and there is a controls array in the Tfrom class that maintains all controls in from, making it easy to manipulate all controls. Here's the source code:
Program Two:
void __fastcall TForm1::FormResize(TObject*Sender)
{
int midLoc=Width/2;
TControl * ChildControl;
fof(int i=0; i<ControlCount; i++)
{//遍历Controls数组,ControlCount是数组元素个数
ChildControl = Controls[i];
ChildCotrol->Left = midLoc-ChildControl->Width/2;
}
}
Controlling the location of groups of controls with the container
The code above is convenient for maintaining all control to a unified location, but it is not conducive to manipulating groups of controls.
In C++builder, you provide a container control that can place other controls, such as Tpanel, Tgroupbox, and so on, and you can put the control in a container control: then manipulate the container control to achieve the purpose of a group control control. The container control controls the result of the position of the control, and all the controls are centered automatically, but the child controls in the GroupBox1 control are not centered because the controls array maintains only the direct child controls of the form, and the controls in the form are powerless against the child controls. Therefore, the child controls in the GroupBox1 control are still in their original position and must be coded to dynamically control their location and size. This technique is very important, especially when changing the container control dynamically for a large hour. In fact, the container control has a controls array as from, as long as it is similar to program 2. Code See program 3
Cheng
void __fastcall TForm1::FormResize(TObject*Sender)
{
int midLoc=Width/2;
TControl * ChildControl;
for(int i=0;i<ControlConnt; i++)
{//遍历Controls数组,ControlCount是数组元素个数
ChildControl = Controls[i];
ChildControl->Left = midLoc-ChildControl->Width/2;
if (ChildControl ==GroupBox1)
{//若控件是GroupBox,对其子控件进行操作
int SubMidLoc=GroupBox1->Width/2;
for(int j=0;j<GroupBox1->ControlCount;j++)
{//遍历GroupBox1的Controls数组。ControlCount是数组元素个数
ChildControl=GroupBox1->Controls[j];
ChildContro1->Left=SubMidLoc-ChildControl->Width/2;
}
}
}
}
Maintain form position and size at different resolutions
Business processes typically operate at various resolutions, and it is important to correctly determine the current resolution. Windows function GetDeviceCaps can return device size in any Device description table.
GetDeviceCaps has two parameters, the first is the device description table handle to query, and the second is the type of the parameter to query, where horzres,vertres represents the screen horizontal and vertical resolution respectively. Program 4 is the code that obtains the current screen resolution. You can typically place this code in a "project file" (such as Project1.cpp) and define I and J as global variables, and then use the techniques described earlier to control the size and location of various screen elements.
Program Four:
HDC Hdc=getdc (NULL); Get the screen Device description table handle
int I=getdevicecaps (hdc,horzres); Query screen horizontal resolution, and return in the variable i
int J=getdevicecaps (hdc,vertres); Query screen horizontal resolution, and return to the variable J
ReleaseDC (NULL,HDC); Release Screen device Description table
In the same way, you can control the printing of different sizes of paper, here no longer repeat, interested friends can see GetDevice caps help and printing help.