For Newbie

Tuesday, 15 November 2011

Check Valid Icon with Delphi



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.

Share on Facebook
Share on Twitter
Share on Google+

Related : Check Valid Icon with Delphi