delphi dynamic access to the file type icon

delphi dynamic access to the file type icon .txt what I do not expect, just hope you are not as good as a woman a. Really miss the child, ah, hot day when I can, too light arm like a man! In the preparation of the application, the combo box (ComboBox), the list box (ListBox), and other common components, usually used to display not only text, but also display its text-related icons. In a typical Windows application, change the display text display these icons go out with lists of changes, such as when it lists all the files in the current directory combo box, in the box on the left displays a combination of the file name icon with it, this is called dynamic icons. To use dynamic icons in Delphi as follows:

 

 

  First, get the icon
  you want to use dynamic icons, we must first solve is how to get a handle text and icon associated with it. The icon associated with the file through the registry by the system decides, and the same file (or sub-directory, or folder) may have two results that appear on the desktop in Windows programming, this is the DOS file name and display name (Display Name) . If our application does not need to have Windows Explorer like that kind of effect, you can use the FindFirst () and FindNext () function and two FindClose () procedure to get the DOS file name, otherwise we will be using the show to get WindowsAPI name. While obtaining the file name can be obtained by using in the SHGetFileInfo ShellAPI.pas () function which icon handle HICON, as follows:
  function the SHGetFileInfo (pszPath: PAnsiChar; dwFileAttributes: DWORD; var psfi: TSHFileInfo; cbFileInfo, the uFlags: UINT) : DWORDl;
  pszPath parameters: specify the file name. When the value is not included in uFlags SHGFI_PIDL, can be specified directly; pszPath otherwise be obtained by calculation, can not be specified directly;
   dwFileAttributes parameters: the file attributes, is effective only when the value contained uFlags SHGFI_USEFILEATTRIBUTES, generally do not have this parameter;
  psfi parameters: returns the obtained file information, a record type has the following fields:
  the hIcon: HICON; // file icon handle
  iIcon: Integer; // index icon system
  dwAttributes: DWORD; // file attribute value
  szDisplayName: array [0..MAX_PATH-1] of AnsiChar; // file name display
  szTypeName: array [0..79] of AnsiChar ; // file type name
  cbFileInfo parameters: bit value of psfi;
  the uFlags parameters: indicated We need to return the file information identifier, commonly used are the following constants:
  SHGFI_ICON; // get icon
  SHGFI_DISPLAYNAME; // get the display name
  SHGFI_TYPENAME; // get the type name
  SHGFI_ATTRIBUTES; // obtain properties
  SHGFI_LARGEICON; // get large icons
  SHGFI_SMALLICON; / / small icon is obtained
  SHGFI_PIDL; // pszPath identifier is a
  function of the SHGetFileInfo () return value with the value change uFlags also varies. You can get a handle to the icon file by psfi parameters by calling SHGetFileInfo (), but be careful not to use SHGFI_PIDL in uFlags argument when, SHGetFileInfo () can not obtain information on the "My Computer" and other like virtual folder.

 

  Second, load the icon
  using TImageList components provided by Delphi, by calling the function ImageList_AddIcon CommCtrl .pas in () to load the icon get, and to ensure that its index number corresponding to the text and display. As follows:
  function ImageList_AddIcon (ImageList: HIMAGELIST; // load ImageList handle icon
  Icon: Hicon icon handle // load): Integer; // return icon in the ImageList index
  when index need to specify the icons can be used ImageList_AddIcon ()The return value.

 

  Third, graphics and text Output icons
  for combo boxes, list boxes and other components can not be directly displayed icons, since displayed icons requirements while also simultaneously display text object can be achieved by setting its corresponding Style property, for example:
  a combination of block: ComboBox1.Style: = csOwnerDrawVariable based on actual experience is best not disposed directly ObjectInspector form, but rather the program code is added at an appropriate position, or may occur irregularities in the height of the drawing area
  list box: ListBox1.Style: = lbOwnerDrawVariable
  status bar: StatusBar1.Panels [i] .Style: = psOwnerDraw simple status bar can not be used, i is the status bar to be drawn a pane index icon, graphical output may be used ImageList1.Draw TImageList () method , and text output can be used TCanvas of TextOut () method, which inherited from the Canvas properties of the component, the component Canvas apparently did not attribute can not use this method to display the icon.
  For components can directly display the icon, which directly specifies Images, StateImages other desired attribute icon corresponding TimageList component name, the icon may be displayed and the index number specified by the icon. Note that: When using large icons, you must first call the CreateSize TImageList () method to specify the size of the icons can be loaded, and should recall CreateSize after each TImageList Clear method invocation ().
  TImageList use of ImageList1.Clear way to clear icon loaded, often used when needed to refresh.

 


This method is only shown to take the file extension icons, the file does not need to fully present completely !!!!

TForm1.FormCreate Procedure (Sender: TObject);
var
the FileInfo: TSHFileInfo;
the begin
// Set made small icon handle
S_ImageListHandle: = the SHGetFileInfo ( 'C: \', 0, the FileInfo,
the SizeOf (the FileInfo), or SHGFI_SYSICONINDEX SHGFI_SMALLICON);
// the handle of the small icon set refers to the list
SendMessage (Listview1.Handle, LVM_SETIMAGELIST, LVSIL_SMALL, S_ImageListHandle);
End;

When used:
the ListItem: = ListView1.Items.Add;
FillChar (the FileInfo, the SizeOf (the FileInfo), # 0);
IF = Ty '<the DIR>' // take the then directory icon
SHGetFileInfo (Pchar (extractfilepath (application.ExeName) ), 0, the FileInfo, the sizeof (the FileInfo),
SHGFI_SYSICONINDEX or SHGFI_SMALLICON)
the else // file icon takes
the SHGetFileInfo (Pchar (N), 0, the FileInfo, the sizeof (the FileInfo),
SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES or SHGFI_SMALLICON);
ListItem.ImageIndex: = the FileInfo .iIcon;
there are some variables my local variables, you look mean on OK

Guess you like

Origin www.cnblogs.com/blogpro/p/11453534.html