Use dynamic icons in Delphi

zhaozj2021-02-16  48

In the application's writing, the Combination box (ComboBox), a list box (Listbox), the common components, usually not only to display text, but also display it with text-related icons. In a general Windows application, the display of these icons will vary with the changes of the listed display text, for example, when all files in the current directory are listed in the combo box, the file name is displayed on the left side of the combo box. The icon is the so-called dynamic icon. The steps to use the dynamic icon in Delphi are as follows:

First, the icon is obtained to use the dynamic icon, first of all to solve how to get the text and the icon handle associated with it. This icon is determined by the system registry via the file association, and the same file (or child directory, or folder) in the Windows programming can also have two display results, this is the DOS file name and display name (Display name) . If our application does not need to have the effect like a Windows Resource Browser, you can use FindFirst () and FindNext () two functions and the FindClose () process to get the DOS file name, otherwise we should use WindowsAPI to get the display. name. At the same time by obtaining the file name, the icon handle can be obtained by using the SHGETFILEINFO () function in shellapi.PAS, which is as follows: Function ShgetFileInfo (PSZPath: Pansichar; DWFileAttributes: DWord; var Psfi: Tshfileinfo; CBFILEINFO, UFLAGS: UINT) : Dwordl; pszpath parameter: The specified file name. When you do not include SHGFI_PIDL in the value of UFLAGS; otherwise PSZPath wants to be specified by calculation; DWFileAttributes parameters: File properties, only when UFLAGS is included, it is valid when it contains SHGFI_USEFileAttribute, generally no use; PSFI Parameters: Returns the obtained file information, is a record type, with the following field: hicon: hicon; // file icon handle IICON: Integer; // icon system index number dwattributes: dword; // The properties value of the file szdisplayName: Array [0..max_path-1] of ansichar; // file display name sztypename: array [0..79] of ansichar; // file type name name name name cbfileinfo parameter: PSFI's bit value; uflags parameter: Indicate need to return The file information identifier, commonly used constant: SHGFI_ICON; // Get the icon SHGFI_DISPLAYNAME; // Get the display name SHGFI_TYPENAME; // Get the type name SHGFI_ATTRIBUTES; // Get the attribute SHGFI_LARGEICON; // Get big icon SHGFI_SMallicon; // Small icon SHGFI_PIDL; // pszpath is an identifier function SHGETFILEINFO () return value varies depending on the value of UFLAGS. By calling SHGETFILEINFO (), you can get the icon handle of the file by the PSFI parameter, but pay attention to the information such as the virtual folder such as "My Computer" when using SHGFI_PIDL in the uflags parameter.

Second, the icon loads the TimageList component provided by Delphi, load the obtained icon by calling the function imagelist_addicon () in CommCtrl .PAS, and guarantees its index number to correspond to the display text. The following: function imagelist_addicon (imagelist: himagelist; // load icon): hicon // loaded icon handle): Integer; // Return icon in ImageList's index number You can use imagelist_addicon when you need to indicate the icon index. ()The return value. Third, the icon and text drawing output For the combo box, the list box, etc. cannot directly display the icon, since the display icon is required to display the text, and can be achieved by setting its corresponding style attribute, as follows: Combination Box: ComboBoX1.Style: = CSOWNERDRAWVARIABLE best not set directly in the ObjectInspector form, but should add the code in the proper position of the program, otherwise the drawing area height irregular change list box: listbox1.style: = LBOWNERDRAWVARIABLE Status Bar: statusbar1.panels [i] .Style: = PSOWNERDRAW Can't use a simple status bar, i is the status bar to draw an icon in the status bar, the graphic output can be used using timelist's imagelist1.draw () method And the text output can use Tcanvas's TextOut () method, which is inherited by the CANVAS property of the component, and it is clear that the component without the Canvas property cannot use this method to display icons. For components that can directly display the icon, specify the icon attributes required for IMAGES, StateImages, etc., which can be used as the corresponding TimageList component name, and the icon can be displayed by the index number of the specified icon. It should be noted that when using a large icon, you must first call the TimageList's Createsize () method specify the size of the load icon and re-call Createsize () after each Clear method that calls TimageList. Use the timagelist1.clear method to clear the loaded icon, often used when needed.

转载请注明原文地址:https://www.9cbs.com/read-23097.html

New Post(0)