Maintain the widget position and size in C ++ Builder

Source: Internet
Author: User
Maintain the widget position and size in C ++ Builder
 
 
C ++ Builder/Delphi is a popular Visual C ++/Pascal development tool developed by Inprise (formerly Borland), which greatly accelerates application development. However, because it is a visual programming tool, after you drag the control to the form, the control's position is fixed. As the size of the form or the screen resolution changes, the positions of the control and form are often very messy. This article describes how to correctly locate the control and form.
Use the onresize event to change the widget location and size
The Form Control in C ++ Builder/Delphi has an onresize event. All the actions related to the size change of the form can trigger this event, this includes creating a form, maximizing/minimizing/restoring the form, and dragging and changing the size with the mouse. Therefore, dynamically changing the position of each control in this event can ensure that the relative position of the control in form is correct. Because cbuilder and Delphi programs share many similarities, only the code of cbuilder is listed here.
The following code ensures that the control is centered under any circumstances.
Procedure 1:
Void _ fastcall tform1: fofmresize (tobject * sender)
{
Int midloc = width/2; // obtain the midpoint of the form.
Label1-> left = midLoc-Label1-> width/2; // set label1 to center form
Button1-> left = midLoc-Button1-> width/2; // set the position of button1 to center form
}
Slightly modify this code to keep the control in any position you want. You can also use the onresize event to change the widget size. When there are many controls in form, it is very troublesome to adjust the positions of each control separately. In the tfrom class, there is a controls array to maintain all the controls in from, it allows you to conveniently operate on all controls. The source code is as follows:
Procedure 2:
Void _ fastcall tform1: formresize (tobject * sender)
{
Int midloc = width/2;
Tcontrol * childcontrol;
Fof (INT I = 0; I <controlcount; I ++)
{// Traverses the controls array. controlcount is the number of elements in the array.
Childcontrol = controls [I];
Childcotrol-> left = midloc-childcontrol-> width/2;
}
}
Use container to control group control positions
The code above makes it easy to maintain all controls to a uniform location, but it is not conducive to group controls.
In C ++ builder, some "Container" controls that can place other controls, such as tpanel and tgroupbox, can be placed in the "Container" control: then, the "Container" control is operated to control the control group. The result of using the "Container" control to control the control position is that all controls are automatically centered, but the child controls in the groupbox1 control are not centered, because the controls array only maintains the direct child controls of the form, the child control of the form control is powerless. Therefore, the child control in the groupbox1 control is still in the initial position, and the corresponding code must be written to dynamically control its position and size. This technology is very important, especially when the "Container" control is dynamically changed. In fact, the "Container" control has a controls array like from, as long as it performs operations similar to program 2. For the code, see Program 3.
Procedure 3
Void _ fastcall tform1: formresize (tobject * sender)
{
Int midloc = width/2;
Tcontrol * childcontrol;
For (INT I = 0; I <controlconnt; I ++)
{// Traverses the controls array. controlcount is the number of elements in the array.
Childcontrol = controls [I];
Childcontrol-> left = midloc-childcontrol-> width/2;
If (childcontrol = groupbox1)
{// If the control is groupbox, perform operations on its child Control
Int submidloc = groupbox1-> width/2;
For (Int J = 0; j <groupbox1-> controlcount; j ++)
{// Traverse the controls array of groupbox1. Controlcount is the number of array elements
Childcontrol = groupbox1-> controls [J];
Childcontro1-> left = submidloc-childcontrol-> width/2;
}
}
}
}
Maintain the form position and size at different resolutions
Commercial programs generally run at various resolutions. It is important to judge the current resolution correctly. The Windows function getdevicecaps can return the device size in any device description table.
Getdevicecaps has two parameters: the first is the device description table handle to be queried, and the second is the type of the parameter to be queried. horzres and vertres represent the horizontal and vertical resolutions of the screen, respectively. Program 4 is the code for obtaining the current screen resolution. Generally, you can place this code in a "project file" (such as project1.cpp) and define I and j as global variables, then, we can use the technology described above to control the size and position of various screen elements.
Procedure 4:
HDC = getdc (null); // obtain the handle of the on-screen device description table
Int I = getdevicecaps (HDC, horzres); // query the screen horizontal resolution and return it in variable I
Int J = getdevicecaps (HDC, vertres); // query the screen horizontal resolution and return it in variable J
Releasedc (null, HDC); // release the screen device description table
In the same way, you can control the printing of different sizes of paper. I will not repeat it here. If you are interested, you can view getdevice.
Caps help and print help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.