--> Check Valid Icon with Delphi | Delphi Tips Trick

For Newbie

Tuesday, 15 November 2011

Check Valid Icon with Delphi

| Tuesday, 15 November 2011


The trick is knowing whether the icon to load, valid (can read) or not.

Function declaration:
ValidIcon function (const Filename: String): Boolean;
var
   h: Hicon;
begin
   h: = ExtractIcon (Application.Handle, PChar (Filename), 0);
   if h <> 0 then Result: = True else Result: = False;
end;

Examples of implementation (using TImage, TOpenDialog):
procedure TForm1.Button1Click (Sender: TObject);
var
   F: String;
begin
   if OpenDialog1.Execute then
   begin
     F: = OpenDialog1.FileName;
     if ValidIcon (F) then Image1.Picture.Icon.LoadFromFile (F) else
       MessageBox (Handle, 'Icon is not Valid!', 'Error', MB_ICONWARNING);
   end;
end;

Add ShellAPI unit in uses clause.

Related Posts

No comments: