3.2.3 Draw a Self painting project
When the style property of a part is self drawn, Windows no longer draws the part, instead Windows generates events for each visual item, and the application must draw the project in the event.
Before the application draws its own control, Windows generates a measurement project event that tells the program where the project is displayed.
Windows determines the size of the project display, but the application can handle the event and select the display area for itself. For example, if a program wants to use a bitmap instead of a text display, you need to set the area to the size of the bitmap. The name of the measurement project event differs from the name of the part, and for list boxes and combo boxes, the event is called OnMeasureItem. For Tabset, the event is called Onmeasuretab.
There are two important parameters for measuring project events: the project index number and the project size. This size is variable. The output position of the successor project is determined by the size of the preceding item. For example, in a custom list box, if the application sets the height of the first item to 5 pixel points, the second item starts outputting at the sixth pixel point. list boxes and combo boxes, the application can only be set to the height of the item, and the width of the item is the height of the part. In Tabset, the width of the tabs is variable, while the height is fixed. Self-drawn grids allow applications to change the height and width of grid cells.
OnMeasureItem's statement reads as follows:
ListBox1 MeasureItem (Control:twincontrol;index:integer; var height:integer);
The code that responds to the OnMeasureItem event in a routine is as follows:
Procedure Tform1.listbox1measureitem (Control:twincontrol; Index:integer;
var height:integer);
Begin
With Listbox1.canvas do
Begin
Font.Name: = Listbox1.items[index];
Height: = TextHeight (' A ');
End
End
Procedure Tform1.tabsetmeasuretab (Sender:tobject; Index:integer;
var tabwidth:integer);
Var
Bitmapwidth:integer;
Begin
Bitmapwidth: = Tbitmap (Tabset1.tabs.objects[index]). Width;
INC (tabwidth, 2 + bitmapwidth);
End
After the OnMeasureItem event, Windows fires an event called OnDrawItem, which is also different from the name of the part, often with OnDrawItem, OnDrawTab, and Ondrawcell.
OnMeasureItem's statement reads as follows:
DrawItem (Control:twincontrol; Index:integer; Rect:trect; State:townerdraw);
Where control is a part reference that contains the project
Index is the number of the item
Rect is the rectangle drawn
State is the status of the project, such as SELECT, get focus, etc.
In the list box for a routine, the items listed are various font names that are supported by the screen, and when a OnDrawItem event occurs in a list box, the program sets the output font to the font that the item represents, so the items in the list box appear in a different font, with the following code:
Procedure Tform1.drawitem (Control:twincontrol; Index:integer;
Rect:trect; State:townerdrawstate);
Begin
With Listbox1.canvas do
Begin
FillRect (Rect);
Font.Name: = Listbox1.items[index];
TextOut (Rect.left, Rect.top, Listbox1.items[index]);
End
End
In the Tabset part, the bitmap is printed with the text at the same time, with the following code:
Procedure Tform1.tabset1drawtab (Sender:tobject; Tabcanvas:tcanvas;
R:trect; Index:integer; Selected:boolean);
Var
Bitmap:tbitmap;
Begin
Bitmap: = Tbitmap (Tabset1.tabs.objects[index]);
With Tabcanvas do
Begin
Draw (R.left, R.top + 4, Bitmap);
TextOut (R.left + 2 + bitmap.width,
R.top + 2, Tabset1.tabs[index]);
End
End