6.4.3 the connection to a file control
In this example we used a new set of controls: Tabset, Directoryoutline, FileListBox, for displaying and selecting drives, directories, and files. Using this group of controls requires a small amount of code support compared to the methods used in (6.3).
Tabset's contact with Directoryoutline is established during the Tabset click event Process:
With Drivetabset do
Directoryoutline.drive: = tabs[tabindex][1];
The connection between Directoryoutline and FileListBox is established during the Directoryoutline Change event process:
Filelist.directory: = directoryoutline.directory;
Filelist.update;
6.4.4 Drivetabset's self painting style display
Dephi provides a custom style display for some controls, such as ListBox, ComboBox, Tabset, and so on. By default, these controls display text automatically. In the custom style, the form that owns the control draws itself out of each item of the control during run time.
Self-painting style display the usual application is to add graphic display for items in addition to text. Controls that can be displayed in a custom style have a common feature: they all have a tstrings type of project chain. Because of the characteristics of the Tstrings Class (chapter III), they can all add an object associated with the corresponding text. And this is the key to the display of the style of painting.
Typically, it takes three steps to create a custom painting style:
1. Set self painting style;
2. Add a drawing object to the string list;
3. Draw from the painting project.
6.4.4.1 set self painting style
Control property style is used to set the custom style. For Drivetabset, we set the Style property to Tsownerdraw.
For controls such as ListBox, ComboBox, and Tabset, the reader can refer to the online Help documentation for a slightly different setting.
6.4.4.2 add a drawing object to a string list
1. Add picture parts to the application
In this program we set up three picture parts network, floppy, Fixed, and three bitmap files Network.bmp, floppy.bmp, fixed.bmp associated.
2. Add a picture to a list of strings
Depending on the nature of the string list, we can relate the object to an existing string or add strings and objects at the same time. Here we use the latter method.
In the OnCreate event processing of a child window, we use a loop to detect, in turn, the existence of a drive from A to Z and the type of drive. This utilizes the Windwos API function GetDriveType and returns 0 if the drive does not exist, otherwise the type of drive is returned (drive_removable, drive_fixed, Drive_remote). Depending on the drive type, we can determine the different graphic objects that are added to the tabs with the text (drive name). During the add process, the Drivetabset TabIndex is set to the current drive.
The list of procedures is as follows:
Procedure Tfmform.formcreate (Sender:tobject);
Var
Drive, Addedindex:integer;
Driveletter:char;
Begin
For Drive: = 0 Todo
Begin
DriveLetter: = Chr (Drive + ord (' a '));
Case GetDriveType (Drive) of
Drive_removable:
Addedindex: = DriveTabSet.Tabs.AddObject (DriveLetter, Floppy.Picture.Graphic);
Drive_fixed:
Addedindex: = DriveTabSet.Tabs.AddObject (DriveLetter, Fixed.Picture.Graphic);
Drive_remote:
Addedindex: = DriveTabSet.Tabs.AddObject (DriveLetter, Network.Picture.Graphic);
End
If UpCase (driveletter) = UpCase (filelist.drive) Then
Drivetabset.tabindex: = Addedindex;
End
End
6.4.4.3 Drawing Project
When you set the style of a control to self painting, Windows is no longer responsible for drawing the control's items on the screen, but for each visible item to produce a custom event. An application can draw a control's project by handling a custom drawing event.
1. Determine the size of your own drawing items
For Tabset, this is done during the Onmeasuretab event process. We need to increase the width of each label drivetabset enough to drop both the text and the bitmap at the same time.
Procedure Tfmform.drivetabsetmeasuretab (Sender:tobject; Index:integer;
var tabwidth:integer);
Var
Bitmapwidth:integer;
Begin
Bitmapwidth: = Tbitmap (Drivetabset.tabs.objects[index]). Width;
INC (tabwidth, 2 + bitmapwidth);
End
Because the objects in the Tstrings objects attribute are tobject types and do not have a width property, they need to be converted to an object of type Tbitmap:
Bitmapwidth: = Tbitmap (Drivetabset.tabs.objects[index]). Width;