Keep the position and size of the control in C ++ Builder

zhaozj2021-02-17  73

C Builder / Delphi is an invisible C / PASCAL development tool that is popular in Inprise (original Borland), which uses it highly speeds up the development speed of the application. However, it is precisely because it is a visual programming tool. After dragging and dropping the control, the location of the control is fixed. With the size of Form or change in screen resolution, the location of controls and Form itself is often very confusing. This article will introduce some of the correct positioning controls and FORM methods.

Use the onResize event to change the position of the control and size

The FORM control in C Builder / Delphi has an OnResize event, all of the Action related to the size of the size can trigger this event, including the creation of Form, maximize / minimize / restore, and the mouse drag changes to the size. Therefore, the position of each control is dynamically changed in this event ensures that its relative position in the Form is correct. Because CBuilder has a lot of common sites with Delphi, only the CBUilder code is listed here.

The following code will ensure that the control is in any case.

Program 1:

Void __fastcall tform1 :: fofmresize (TOBJECT * SENDER)

{

INT MIDLOC = Width / 2; // Form's midpoint

Label1-> left = midloc-label1-> width / 2; // Setting the location of Label1 is FORM

Button1-> left = midloc-button1-> width / 2; // Setting the bitton1 position is form

}

Slightly modify this code, allow the control to remain in any desired location. Similarly, the onResize event can also be used to change the size of the control. When Controls in Form, you have a very troublesome thing for each control separately, and there is a Controls array in the TFROM class to maintain controls in all from, which can easily operate all controls. Here is its source code:

Procedure 2:

Void __fastcall tform1 :: formresize (TOBJECT * Sender)

{

INT midloc = width / 2;

TControl * childControl;

FOF (int i = 0; i

{// Traverse Controls array, controlcount is the number of array elements

ChildControl = Controls [i];

Childcotrol-> left = midloc-childControl-> width / 2;

}

}

Use "container" to control the position of group control

The above code is very convenient to maintain all control to a unified location, but it is not conducive to the operation of the group control.

Some "container" controls that can place other controls can be placed in C Builder, such as TPANEL, TGROUPBOX, etc., you can place the control in the "container" control: then operate the "container" control to achieve group control. The purpose of the control. Using the "Container" control control control control location, all controls are automatically centered, but the submissions in the GroupBox1 control are not in place, because the Controls array only maintains the FORM direct child control, the sub-control of the control of the Form Capable. Therefore, the child control in the Groupbox1 control is still in the initial position, and must write the corresponding code dynamically controlled its location and size. This technology is very important, especially when dynamically changed the "container" control. In fact, the "container" control is as a controls array as FROM, as long as it performs a similar operation with the program 2. Code see programs 3

Program three void __fastcall tform1 :: formresize (Tobject * Sender)

{

INT midloc = width / 2;

TControl * childControl;

For (int i = 0; i

{// Traverse Controls array, controlcount is the number of array elements

ChildControl = Controls [i];

ChildControl-> left = midloc-childControl-> width / 2;

if (ChildControl == GroupBox1)

{// If the control is GroupBox, operate its sub-control

INT submidloc = groupbox1-> width / 2;

For (int J = 0; j 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 and size under different resolutions

Business procedures generally have to run at various resolutions, and correctly determine that the current resolution is very important. The Windows function getDeviceCaps can return device sizes in any device description table.

GetDeviceCaps has two parameters, the first is the device descriptor handle to query, the second is the type of query parameter, where Horzres, Vertres represent the horizontal and vertical resolution, respectively. The program 4 is a code for the current screen resolution. This code can generally be placed in "Project1.cpp) and define I, J as global variables, and then the technique of the previously described screen elements can be utilized to control the size and location of various screen elements.

Program 4:

HDC HDC = getdc (null); // Get screen device description table handle

INT i = getDeviceCaps (HDC, Horzres); // Query the horizontal resolution of the screen and return to the variable I

INT j = getDeviceCaps (HDC, Vertres); // Query the horizontal resolution of the screen and returns in the variable J

ReleaseDC (NULL, HDC); // Release Screen Device Description Table

With the same method, printing of different size paper can be controlled, and details are not described here. Interested friends can view CAPS help and print help.

转载请注明原文地址:https://www.9cbs.com/read-31395.html

New Post(0)