1. Description
Recently joined a project team, the use of development tools is delphi6, think again to start this tool a little sad, but no way who let me is a handyman ...
The requirement is to display a table similar to Word/excel, which can merge columns and so on. Originally want to use Dbgrideh to solve, just tried for a half day found can not merge columns, I went to a. Then think of the Web page to deal with, but try to give up immediately, and finally all kinds of search after no way oh. Think about the use of Tstringgrid to try, after all, is a form, but also a large number of customization, OK. Just try it with it.
2, Technical points
The core functionality is also provided by Tstringgrid, which already has the ability to display a two-dimensional table, and on the other hand provides flexible settings to open various custom functions, such as whether to display grid lines, whether rows or columns can be resized, etc. With the basic settings, the data on the load can be displayed. The next thing to do is make the table content appear as I expected.
Because the table needs to resemble the kind of table in Word, mainly merge cells and so on, the style can be drawn as required. So the main technical points:
1, self-painting method, directly with Ondrawcell event processing
2, table header, you can use the properties of Fixcol and Fixrow
3. Text Wrapping
3. Development3.1. Property settings
Defaultdrawing: Set to False so that it does not draw before Ondrawcell, otherwise the drawing function of the parent class is first done, and then the Ondrawcell is called, because many of the contents I draw myself so set it to false
RowCount: The default initial number of rows must be greater than or equal to 2, otherwise the fixedrows will be cleared to 0, so there is no table header. This property can be used to increase or decrease the number of rows, +1 on more than one row.
Fixedrows: Fixed number of rows, set to 1, because a row is a table header
Fixedcols: Fixed number of columns, set 1, with a fixed column can display the ordinal, but also for the mouse to adjust the height of the row
Options.govertline: The vertical gridline for the data region, which defaults to true, displays the cell's vertical border by default. I had some trouble with the drawing, I changed it to false, I painted the frame myself.
Options.gohorzline: Horizontal style line for data area. Also set to False
Options.gorowsizing: Row height adjustable, row height can be adjusted by mouse at runtime
Options.gocolsizing: Column widths can be adjusted to adjust the width of the column at run time by the mouse
3.2. Calculate the text height of each line
This I have been looking for a long while did not find a relatively simple and easy to use method, and finally used Tlabel this control to complete. A little detour, but the feeling is very practical. Oh.... Oh..
Label1. Caption: = sData; Stringgrid1.rowheights[irow]:ten;
Assign the text to the label, and then use the label's height to set the row height of Stringgrid so that the text of the multiline is displayed. But there is a problem, AutoSize is true when can not automatically wrap, drunk. Let's find a way to solve the problem later.
Another way to use it later is pretty good:
0 ; Rect. Top:0; Rect. Right: $; Rect. Bottom: Iheight:= DrawText (Self.Label1.Canvas.Handle, PChar (SData), Length (SData), rect, dt_wordbreak);
Set a fixed rectangular box, and then use the DrawText method to draw once you can get height, haha, good. This gives the actual text height.
3.3. Ondrawcell Events
The Drawcell event is the drawing function for each cell:
procedure Tform1.stringgrid1drawcell (sender:tobject; Acol, Arow:integer; Rect:trect; State:tgriddrawstate);
The parameters are useful:
Acol,arow: Is the column, row
Rect: is the cell's rectangle
State: Refers to the status of the current cell, selection, focus, fixed
With this content you can do the exercises you want:
Draw border:
sg. Canvas.rectangle (Rect);
Draw text:
DrawText (sg. Canvas.handle,pchar (SData), Length (SData), Rect, dt_wordbreak or Dt_vcenter or Dt_left or dt_singleline);
Almost all of these features. That is, draw a box, and then draw a text.
Study notes: Delphi's Tstringgrid