In the writing of applications, common components such as ComboBox and ListBox are usually used not only to display text, but also to display icons related to the text. In general Windows applications, the display of these icons will change as the listed display text changes. For example, when all files in the current directory are listed in a combo box, the display on the left side of the combo box is related to the file name. Connected icons are called dynamic icons. The steps to use dynamic icons in Delphi are as follows: 1. Obtaining icons
To use dynamic icons, the first thing to solve is how to obtain the display text and the icon handle associated with it. The icon is determined by the system registry through file association, and in Windows programming, the same file (or subdirectory, or folder) may also have two display results on the desktop, which are the DOS file name and display name (Display Name) . If our application does not need to have the same effect as the Windows Resource Browser, we can use the FindFirst() and FindNext() functions and the FindClose() process to obtain the DOS file name. Otherwise, we should use the Windows API to obtain the display name. While obtaining the file name, you can obtain its icon handle HICON by using the SHGetFileInfo() function in ShellAPI.pas. The instructions are as follows:
function SHGetFileInfo(pszPath: PAnsiChar; dwFileAttributes: DWord;var psfi: TSHFileInfo; cbFileInfo,uFlags: UINT): DWORDl;
pszPath parameter: the specified file name. When the value of uFlags does not contain SHGFI_PIDL, it can be specified directly; otherwise, pszPath must be obtained through calculation and cannot be specified directly;
dwFileAttributes parameter: file attributes, only valid when the value of uFlags contains SHGFI_USEFILEATTRIBUTES, this parameter is generally not used;
psfi parameter: Returns the obtained file information, which is a record type with the following fields:
hIcon: HICON; //Icon handle of the file
iIcon: Integer; //System index number of the icon
dwAttributes: DWORD; //Attribute values of the file
szDisplayName: array [0..MAX_PATH-1] of AnsiChar; //The display name of the file
szTypeName: array [0..79] of AnsiChar; //The type name of the file
cbFileInfo parameter: bit value of psfi;
uFlags parameter: Indicates the file information identifier that needs to be returned. Commonly used constants are the following:
SHGFI_ICON; //Get the icon
SHGFI_DISPLAYNAME; //Get the display name
SHGFI_TYPENAME; //Get the type name
SHGFI_ATTRIBUTES;//Get attributes
SHGFI_LARGEICON; //Get large icon
SHGFI_SMALLICON; //Get the small icon
SHGFI_PIDL; // pszPath is an identifier
The return value of the function SHGetFileInfo() also varies with the value of uFlags. By calling SHGetFileInfo(), you can get the icon handle of the file from the psfi parameter, but please note that when SHGFI_PIDL is not used in the uFlags parameter, SHGetFileInfo() cannot obtain information about virtual folders such as "My Computer". 2. Loading of icons
Use the TImageList component provided by Delphi to load the obtained icon by calling the function ImageList_AddIcon() in CommCtrl.pas, and ensure that its index number corresponds to the displayed text. The instructions are as follows:
function ImageList_AddIcon(ImageList: HIMAGELIST; //Load the ImageList handle of the icon
Icon: Hicon //Loaded icon handle): Integer; //Return the index number of the icon in the ImageList
The return value of ImageList_AddIcon() can be used when you need to specify the icon index number. 3. Graphical output of icons and text
For components such as combo boxes and list boxes that cannot directly display icons, because they are required to display icons and text at the same time, this can be achieved by setting their corresponding Style properties. Examples are as follows:
Combo box: ComboBox1.Style:=csOwnerDrawVariable. According to actual usage experience, it is best not to set it directly in the ObjectInspector form. Instead, the code should be added to the appropriate location in the program, otherwise irregular changes in the height of the drawing area may occur.
List box: ListBox1.Style:=lbOwnerDrawVariable
Status bar: StatusBar1.Panels[i].Style:= psOwnerDraw cannot use a simple status bar. i is the index number of a certain pane in the status bar where the icon is to be drawn. Graphic output can use the ImageList1.Draw() method of TImageList, and Text output can use the TextOut() method of TCanvas, which is inherited from the Canvas property of the component. Obviously components without Canvas properties cannot use this method to display icons.
For components that can directly display icons, directly specify the required icon properties such as Images and StateImages as the corresponding TimageList component name, and the icon can be displayed by specifying the index number of the icon. It should be noted that when using large icons, you must first call the CreateSize() method of TImageList to specify the size of the loadable icon, and call CreateSize() again after each call to the Clear method of TImageList.
Use the ImageList1.Clear method of TImageList to clear the loaded icons, which is often used when refreshing is required.