Solved

load icon from resource file

Posted on 2001-06-26
5
1,151 Views
Last Modified: 2010-04-06
this is the code i use and i have an icon named 'icon_2' in my resource file but still it won't work! help!

AppIcon := TIcon.Create;
AppIcon.Handle := LoadIcon( HInstance ,'ICON_2');
TrayIcon1.Icon := AppIcon;
AppIcon.Free;

i want to change my program icon in the system tray when i click on it! help!
0
Comment
5 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 6230224
Try it with a default icon:

AppIcon := TIcon.Create;
AppIcon.Handle := LoadIcon( HInstance ,'IDI_APPLICATION');
TrayIcon1.Icon := AppIcon;
AppIcon.Free;

or shorter if possible

TrayIcon1.Icon.Handle := LoadIcon( HInstance ,'IDI_APPLICATION');


If these examples work then your icon is not properly stored in the exe...
0
 
LVL 17

Expert Comment

by:geobul
ID: 6231580
Hi,

Application.Icon.Handle := LoadIcon(hInstance, 'ICON_2');

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 6231601
I didn't see that it's about the tray.

you must have a similar declaration in the main form:
  IconNotifyData : TNotifyIconData;

then :

  Application.Icon.Handle := LoadIcon(hInstance, 'ICON_2');
  IconNotifyData.hIcon := Application.Icon.Handle;
  Shell_NotifyIcon(NIM_MODIFY, @IconNotifyData);

Regards, Geo
0
 

Expert Comment

by:manata
ID: 6232516
Hi

How about simplifying it


var
  Icons : TIcon;
begin
  Icons := TIcons.Create(Self);
  ImageList1.ResourceLoad(rtIcon, 'IconResource' ,clNone);
  ImageList1.GetIcon(0, Icons);
end;
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6232855
Hi,

Test if the icon is loaded.
Just add this code and a "BitBtn" to your form and
set "OnClick".
If it does not work it can be that "ICON_2" is not the
right resource name.


Function IconToBitmap(sIcon : TIcon) : TBitmap;
Begin
 Result          := TBitmap.Create;
 Result.Width    := sIcon.Width;
 Result.Height   := sIcon.Height;
 Result.Canvas.Draw(0,0, sIcon);

 //Sets the real width/height of the Icon
 Result.Width    := sIcon.Width;
 Result.Height   := sIcon.Height;
End;


procedure TForm1.BitBtn1Click(Sender: TObject);
Var
 AppIcon :  TIcon;
begin
 AppIcon        := TIcon.Create;
 AppIcon.Handle := LoadIcon(HInstance ,'ICON_2');
 BitBtn1.Glyph := IconToBitmap(AppIcon);
 AppIcon.Free;
end;

GL
Bug
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question