Principle Analysis of grid controls in VCL

Source: Internet
Author: User
Principle Analysis of grid controls in VCL
Lxpbuaa)
2004-9-15
 

 
The Grid Control intuitively describes two-dimensional information. Therefore, it has two horizontal and vertical axes and is a two-dimensional table.
I. class inheritance Structure
Tcustomgrid
//
Tcustomdrawgrid tcustomdbgrid
Tdrawgrid TDBGrid
Tstringgrid
1. tcustomgrid is the parent class of all grid controls. It defines the main features of grid controls and the main functions of grid controls. Here, we understand two protection-level (protected) methods:
(1) Procedure paint;
All the child classes of twincontrol can draw their own shapes through painting. In tcustomgrid. Paint, two functions are implemented: Drawing grid lines and filling grid data. Specifically, the following drawcell completes the filling of grid data. In the following content, I will explain the painting in detail with the source code.
(2) Procedure drawcell (ACOl, Arow: longint; arect: trect; astate: tgriddrawstate); Virtual; abstract;
This is a pure virtual method called by painting to fill the grid data. Therefore, all tcustomgrid sub-classes can overwrite the override method and implement the filling method as needed.
2. tcustomdrawgrid is useless. It mainly completes two tasks:
(1) override the abstract method of tcustomgrid to implement it. Tcustomdrawgrid is no longer an abstract class.
(2) added some events.
For example, it overwrites tcustomgrid. drawcell and triggers the ondrawcell event. Therefore, we can add code to ondrawcell to change the data in a specific row/column mesh and Its filling mode. However, it should be noted that after tcustomdrawgrid overwrites drawcell, it does not actually implement data filling (because it does not know what the data is ). The simplified drawcell source code is as follows:
Procedure tcustomdrawgrid. drawcell (ACOl, Arow: longint; arect: trect;
Astate: tgriddrawstate );
Begin
If assigned (fondrawcell) then
Fondrawcell (self, ACOl, Arow, arect, astate );
End;
3. tdrawgrid and tstringgrid are the classes that you can use during design, or they are all controls. However, tdrawgrid is a simple packaging of tcustomdrawgrid. Therefore, drawcell only triggers the event ondrawcell, but does not actually implement data filling. Because of this, the use of tdrawgrid is quite flexible, and we can use it to draw a variety of information such as text and image.
Tstringgrid is derived from tdrawgrid and is used to describe text information. From the source code below, we can see that it truly achieves data filling:
Procedure tstringgrid. drawcell (ACOl, Arow: longint; arect: trect;
Astate: tgriddrawstate );
Begin
If defadradrawing then
Canvas. textrect (arect, arect. Left + 2, arect. Top + 2, cells [ACOl, Arow]); {This sentence}
Inherited drawcell (ACOl, Arow, arect, astate );
End;
4. TDBGrid is a data-sensitive grid control. It is a simple packaging of tcustomdbgrid. The implementation principle of tcustomdbgrid is similar to that of a common grid control. The main difference is that the data source is different. For example, tstringgrid data comes from tstringgrid. cells, while tcustomdbgrid data comes from tcustomdbgrid. datasource. dataset.
 
Ii. Main Functions of tcustomgrid
As mentioned above, tcustomgrid defines the main functions of grid controls and has the main features of grid controls. Therefore, to understand the basic principles of grid controls, we should focus on the two methods of tcustomgrid: paint and drawcell.
Drawcell is a pure virtual method called in painting (for specific procedures, see the following). Therefore, the emphasis is on two aspects:
(1) What is the use of paint and how it works.
(2) what is done in paint.
1. the operating mechanism of painting.
As mentioned above, paint is used to draw the shape of the control. A specific painting method is defined inside the paint, so you can change the control's appearance by calling the paint at the appropriate time and place.
In VCL, the paint method can be simply understood as the tcontrol reaction to the Windows Standard Message wm_paint. Calling the updatewindow, redrawwindow, invalidaterect of Win32 APIs, And the repaint, refresh, and update Methods of tcontrol in VCL will directly or indirectly trigger the corresponding wm_paint message.
Therefore, the basic operating principle of the grid control is: after the data or data source itself changes, call the paint method in an appropriate way to update the data filling. Take tstringgrid as an example. After the data of cells is changed:
Procedure tstringgrid. setcells (ACOl, Arow: integer; const value: string );
Begin
Tstringgridstrings (ensuredatarow (Arow) [ACOl]: = value;
Ensurecolrow (ACOl, true );
Ensurecolrow (Arow, false );
Update (ACOl, Arow); {The invalidaterect mark [ACOl, Arow] of the Win32 API is called internally. The system then sends a wm_paint message. Finally, the painting is executed .}
End;
2. Paint does the work. Let's take a look at the simplified source code to make it clearer. Take"★"For each function part division mark:
Procedure tcustomgrid. paint;
 
Procedure drawlines (dohorz, dovert: Boolean; Col, row: longint;
Const cellbounds: array of integer; oncolor, offcolor: tcolor );
Begin
{......}
End;
 
Procedure drawcells (ACOl, Arow: longint; startx, starty, stopx, stopy: integer; color: tcolor; includedrawstate: tgriddrawstate );
Begin
{......}
{The pure virtual method drawcell of tcustomgrid is called.
Therefore, the subclass of tcustomgrid can overwrite this method and customize the data filling mode}
Drawcell (curcol, currow, where, drawstate );
{......}
End;
 
Begin
{★0: Calculation of network drawing parameters}
Calcdrawinfo (drawinfo );
 
With drawinfo do
Begin
{★1: Draw grid lines (if the line width is greater than 0 )}
If (horz. effectivelinewidth> 0) or (Vert. effectivelinewidth> 0) then
Begin
{Fixed column in the upper left corner}
Drawlines (gofixedhorzline in options, gofixedvertline in options, 0, 0, [0, 0, horz. fixedboundary, Vert. fixedboundary], clblack, fixedcolor );
{Horizontal fixed column}
Drawlines (gofixedhorzline in options, gofixedvertline in options, leftcol, 0, [horz. fixedboundary, 0, horz. gridboundary, Vert. fixedboundary], clblack, fixedcolor );
{Vertical fixed column}
Drawlines (gofixedhorzline in options, gofixedvertline in options, 0, toprow, [0, Vert. fixedboundary, horz. fixedboundary, Vert. gridboundary], clblack, fixedcolor );
{Non-fixed column}
Drawlines (gohorzline in options, govertline in options, leftcol, toprow, [horz. fixedboundary, Vert. fixedboundary ,??? Horz. gridboundary, Vert. gridboundary], linecolor, color );
End;
 
{★2: Fill in data}
{Fixed column in the upper left corner}
Drawcells (0, 0, 0, 0, horz. fixedboundary, Vert. fixedboundary ,??? Fixedcolor, [gdfixed]);
{Horizontal fixed column}
Drawcells (leftcol, 0, horz. fixedboundary-fcoloffset, 0 ,??? Horz. gridboundary, Vert. fixedboundary, fixedcolor, [gdfixed]);
{Vertical fixed column}
Drawcells (0, toprow, 0, Vert. fixedboundary, horz. fixedboundary ,? Vert. gridboundary, fixedcolor, [gdfixed]);
{Non-fixed column}
Drawcells (leftcol, toprow, horz. fixedboundary-fcoloffset, Vert. fixedboundary, horz. gridboundary, Vert. gridboundary, color, []);
 
{★3: draw an external box for the selected grid}
Canvas. drawfocusrect (focrect );
 
{★4: Fill in areas not occupied by grids in the customer zone}
{Horizontal part}
If horz. gridboundary Begin
Canvas. Brush. Color: = color;
Canvas. fillrect (rect (horz. gridboundary, 0, horz. gridextent ,??? Vert. gridboundary ));
End;
{Vertical part}
If Vert. gridboundary <Vert. gridextent then
Begin
Canvas. Brush. Color: = color;
Canvas. fillrect (rect (0, Vert. gridboundary, horz. gridextent, Vert. gridextent ));
End;
End;
End;
 
From the code above, tcustomgrid. Paint can be divided into five parts. Where★0 is used to calculate the current drawing parameter, and the result is used for the last four parts. In the next four parts,★1 and★2 is the subject. Therefore, our focus is on★0,★1 and★2.★1 and★2. There are detailed annotations, so I will not explain them on a line-by-line basis. If you are interested but cannot understand them, you can think about them slowly. Last pair★0.
Drawinfo is of the tgriddrawinfo type and is defined as follows:
Tgriddrawinfo = record {network drawing parameters}
Horz, vert: tgridaxisdrawinfo; {divided into two parts: horizontal and vertical}
End;
 
The following uses a horizontal method as an example to explain the meaning of tgridaxisdrawinfo:
Tgridaxisdrawinfo = record
Effectivelinewidth: integer; {grid width}
Fixedboundary: integer; {Fixed Grid column width (including grid lines )}
Gridboundary: integer; {total width of each grid column (including gridlines and fixed columns )}
Gridextent: integer; {total width of the mesh customer zone}
Lastfullvisiblecell: longint; {the last column that has not exceeded the customer zone (that is, all can be seen}
Fullvisboundary: integer; {The total width (including grid lines) of all the currently visible columns )}
Fixedcellcount: integer; {number of fixed columns}
Firstgridcell: integer; {The first unfixed column, that is, leftcol (horizontal) or toprow (vertical )}
Gridcellcount: integer; {colcount, total number of columns}
Getextent: tgetextentsfunc; {a function used to obtain the column width, equivalent to colwidths [Index]}
End;

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.