Using the dynamic icon in Delphi

Source: Internet
Author: User
Tags integer

In writing an application, a combo box (ComboBox), list box (ListBox), and other common parts are typically used not only to display text but also to display its text-related icons. In a typical Windows application, the display of these icons varies with the display text that is listed, such as when all the files in the current directory are listed in the combo box, and the icon associated with the file name is displayed to the left of the combo box, which is called the dynamic icon. The steps for using dynamic icons in Delphi are as follows:

One, the acquisition of the icon

To use a dynamic icon, the first thing to solve is how to get the display text and its associated icon handle. The icon is determined by the system registry by file association, and there may be two display results on the desktop for the same file (or subdirectory, or folder) in Windows programming, which is the DOS filename and display name. If our application does not need to have an effect like Windows Explorer, you can use the FindFirst () and FindNext () two functions and the findclose () procedure to get the DOS filename. Otherwise, we should use WINDOWSAPI to get the display name. You can obtain its icon handle Hicon by using the Shgetfileinfo () function in Shellapi.pas when you obtain the filename, as described below:

function Shgetfileinfo (Pszpath:pansichar; Dwfileattributes:dword;var psfi:tshfileinfo; cbfileinfo,uflags:uint): DWORDL;

Pszpath parameter: The specified file name. When the value of the uflags does not contain shgfi_pidl, it can be specified directly, otherwise the pszpath should be obtained by calculation and cannot be specified directly.

dwFileAttributes Parameters: File properties, only when the value of Uflags contains shgfi_usefileattributes is valid, generally do not use this parameter;

PSFI parameter: Returns the obtained file information, which is a record type with the following fields:

Hicon:hicon; Icon handle for file

Iicon:integer; The system index number of the icon

Dwattributes:dword; Property value for 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, and the following constants are commonly used:

Shgfi_icon; Get icon

Shgfi_displayname; Get Display Name

Shgfi_typename; Get type name

shgfi_attributes;//Get Properties

Shgfi_largeicon; Get Large Icons

Shgfi_smallicon; Get Small Icons

shgfi_pidl;//Pszpath is an identifier

The return value of the function shgetfileinfo () also varies depending on the value of the uflags.      by calling Shgetfileinfo (), you can get the icon handle of the file by the PSFI parameter, but note that shgetfileinfo () cannot obtain information about virtual folders such as My computer when SHGFI_PIDL is not used in the Uflags parameter. Second, the loading of the icon

Using the Timagelist component provided by Delphi, load the resulting icon by invoking the function Imagelist_addicon () in Commctrl. Pas, and ensure that its index number corresponds to the display text. The description is as follows:

function Imagelist_addicon (imagelist:himagelist;//load icon ImageList handle

Icon:hicon//Loaded icon handle): Integer; Returns the index number of the icon in ImageList

Use the return value of Imagelist_addicon () when you need to indicate an icon index number.

Graphic output of icons and text

For components that can't display icons directly, such as combo boxes, list boxes, and so on, because you want to display the icon and display the text at the same time, you can do so by setting its corresponding style property, as shown in the following example:

Combo box: It is best not to set it directly in the Objectinspector form, depending on the actual experience, but to add the code to the appropriate location in the program, otherwise the height of the drawing area may appear irregular

List box:

Status bar: Statusbar1.panels[i]. Style:= Psownerdraw cannot use the simple status bar, I is the index number of a pane in the status bar to draw an icon, the graphics output can use the Timagelist Imagelist1.draw () method, and the text output can use Tcanvas TextOut () method, which is inherited by the component's Canvas property, and it is obvious that a component without a canvas property cannot use this method to display an icon.

For components that can display icons directly, specify the icon properties that are required for their images, stateimages, and so on, and then display the icons by specifying the index number of the icon for the corresponding Timagelist component name. Note that when you use a large icon, you must first call the Timagelist Createsize () method to specify the dimensions of the loadable icon and recall createsize () each time the Timagelist clear method is invoked.

Use the Timagelist Imagelist1.clear method to clear the loaded icons, which are often used when refreshing is required.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.