Display Windows icons in Delphi

xiaoxiao2021-03-06  14

The column published an article on "extraction icon's Delphi control" article, the method described can be used to extract icons contained in the file, but the files that do not contain icons are not displayed like Windows "Explorer". Out of its default icon. This article describes how to display almost all icons in the document of this icon in the ListView component of Delphi, and the "extraction icon's Delphi Control" can display almost all icons. A large feature of this method is to display a number of Windows icons without drawing any icons.

---- A large number of icons showed a major feature in Windows, colorful icons not only beautify Windows desktop, but also to intuitively, bring great convenience to users. It can be said to some extent, the status of the icon has exceeded the role of the file name.

---- However, when we use visual programming tools to program Windows programming, there is little convenient way to load and display icons. In general, we use the icon resource in our application to draw itself personally. Therefore, the icons used in our compilation are very limited. We know that there is a large number of known system icons in Windows, such as icons of the folder, the icon of the disk drive, etc. Friends who are familiar with Windows programming may know that these icons have their own specific numbers inside Windows, as long as they get these numbers, they can call and display the corresponding icons. By comparison, I chose the ListView component in Delphi to display the icon because this method is the simplest.

---- The following is an example, which is compiled in Delphi 4.0.

---- This demo can display various types of Windows system icons, including file icons, disk icons, folder icons, user-specific executable file icons (such as torch icons for Delphi, etc.). Interestingly, the icons we have shown have no one who needs to be hand drawn by our own.

---- Please create a form FORM1 in the Delphi environment and add a button Button1 and a list box ListView1 in Form1; then set the property for VIEWSTYle for Vsicon.

---- Source program list:

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils,

Classes, Graphics, Controls,

Forms, Dialogs, Comctrls,

Stdctrls, Shellapi, ExtCtrls,

Imglist;

Type

TFORM1 = Class (TFORM)

ListView1: TListView;

Button1: tbutton;

Procedure FormDestroy (Sender: TOBJECT);

Procedure formcreate (Sender: TOBJECT);

Procedure Button1Click (Sender: TOBJECT);

Private

{Private Declarations}

public

{Public declarations}

END;

Const

Test = 'c: /';

{TEST is test data, returned in this example is a drive icon. reader

You can choose other test data yourself as: test: = 'c: /autoexec.bat'

and many more. }

VAR

FORM1: TFORM1;

SHFILEINFO: TSHFILEINFO;

IMPLEMENTATION

{$ R * .dfm}

Function GetfileiconIndex

Filename: String): Integer;

{Get icon serial number}

VAR

EXT: STRING;

Begin

EXT: = filename; result: = shGetfileinfo (Pchar

(EXT), 0, SHFILEINFO,

Sizeof (SHFILEINFO), SHGFI_LARGEICON OR

SHGFI_SYSICIONDEX or SHGFI_TYPENAME);

Result: = shfileinfo.iicon;

{Return the acquired icon serial number}

END;

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

WITH LISTVIEW1 DO

Begin

SmallImages: = TimageList.createsize (32, 32);

Smallimages.ShareImages: = true;

SmallImages.Handle: = Shgetfileinfo ('*. *', 0,

SHFILEINFO, SIZEOF (SHFILEINFO),

SHGFI_LARGEICON OR

SHGFI_ICON OR SHGFI_SYSICIONDEX);

LargeImages: = TIMAGELIST.CREATESize (32, 32);

Largeimages.shareImages: = true;

LargeImages.Handle: = shGetfileinfo ('*. *', 0,

SHFILEINFO, SIZEOF (SHFILEINFO),

SHGFI_LARGEICON OR

SHGFI_ICON OR SHGFI_SYSICIONDEX);

END;

{Assign system resources to ListView1 components to display icon}

END;

Procedure TFORM1.FORMDESTROY

Sender: TOBJECT;

Begin

Listview1.smallimages.free;

ListView1.larges.free;

{Release the system resources occupied by ListView1

END;

Procedure TFORM1.BUTTON1CLICK

Sender: TOBJECT;

Begin

Listview1.Items.Item [0].

ImageIndex: = GetfileiconIndex (TEST);

{Draw icon for the first item in ListView1}

END;

End.

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

New Post(0)