?
Solved

Change Icon in System Tray

Posted on 1999-10-28
4
Medium Priority
?
280 Views
Last Modified: 2010-04-04
Hi All,

I asked a question a couple of days ago to add my applicaiton icon to the system tray and remove it from the task bar.  That works great and thank you.  The second part to this is that I need to change the icon in system tray while the applciation is running to indicate the application status.  How do I change the Icon in the system tray while the application is running?  I have a RES file that is storing all of my Icons.

I used:
   Application.Icon.Handle := LoadIcon(ResDLLMod, pchar('AWMAILGO'));

But that only seems to change the application icon.  The icon in the system tray wasn't affected.

Thanks in advance,
Chad
0
Comment
Question by:dhertzfe
4 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 2165609
hi dhertzfe,

well just not what you want, but i guess it shows how could it be done.

a sample from my paq
shows the mouse-cursor pos in the tray by usage of three Icons, which are created and updated on the fly

unit tray_ico_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, shellapi;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const MyIconIDs : array[0..2] of Integer = (1000,1001,1002);

var
  IconInfo : TIconInfo;
  MyIcons : array[0..2] of TIcon;
  NotifyIconData : TNotifyIconData;

procedure TForm1.FormCreate(Sender: TObject);
var i : Integer;
begin
  for i := 0 to 2 do  //Push three Icons in the Tray
  begin
    MyIcons[i] := TIcon.Create;
    NotifyIconData.cbSize := SizeOf(NotifyIconData);
    NotifyIconData.hIcon := MyIcons[i].Handle;
    NotifyIconData.szTip := 'Shows the Mouseposition';
    NotifyIconData.uCallbackMessage := 0;
    NotifyIconData.uFlags := NIF_ICON or NIF_TIP;
    NotifyIconData.uID := MyIconIDs[i];
    NotifyIconData.Wnd := Application.Handle;
    if Shell_NotifyIcon(NIM_ADD, @NotifyIconData) then;
  end;
  Timer1.Interval := 100;  //Enable Timer
  Timer1.Enabled := True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var i : Integer;
begin
  for i := 0 to 2 do   //Remove the Icons from the Tray
  begin
    NotifyIconData.cbSize := SizeOf(NotifyIconData);
    NotifyIconData.hIcon := MyIcons[i].Handle;
    NotifyIconData.szTip := 'Shows the Mouseposition';
    NotifyIconData.uCallbackMessage := 0;
    NotifyIconData.uFlags := NIF_ICON or NIF_TIP;
    NotifyIconData.uID := MyIconIDs[i];
    NotifyIconData.Wnd := Application.Handle;
    if Shell_NotifyIcon(NIM_DELETE, @NotifyIconData) then
      MyIcons[i].Free;
  end;
end;


//The timer event
procedure TForm1.Timer1Timer(Sender: TObject);
var
  MyBmp : TBitMap;
  P : TPoint;
  i,x,y : Integer;
  s : String;
  MB : Array[0..2] of TBitmap;
begin
  Timer1.enabled := False;  //prevent that the event comes twice
  MyBmp := TBitMap.Create;  //Prepare Source Bitmap
  MyBmp.Height := MyIcons[1].Height;
  MyBmp.Width := MyIcons[1].Width * 3;
  MyBmp.Canvas.Brush.Color := clBlue;  //comes black on yellow in the tray
  MyBmp.Canvas.FillRect(Rect(0,0,MyBmp.width,MyBmp.Height));
  MyBmp.Canvas.Font.Size := 20;
  for i := 0 to 2 do  //prepare secondary Bitmaps
  begin
    MB[I] := TBitMap.Create;
    MB[I].Width := MyIcons[i].Width;
    MB[i].Height := MyIcons[i].height;
  end;
  GetCursorPos(P);  //Get current Mousepos
  s := Inttostr(p.x)+':'+Inttostr(P.y); //prepare String
  y := (MyBmp.Height div 2) - (MyBmp.Canvas.TextHeight(S) div 2);
  x := (MyBmp.Width div 2) - (MyBmp.Canvas.TextWidth(S) div 2);
  MyBmp.Canvas.TextOut(x,y,s);
    Image1.Picture.Bitmap.Assign(MyBmp);  //Controlling on the form, can deleted
  for i := 0 to 2 do  //Create Icons
  begin
    MB[I].Canvas.CopyRect(Rect(0,0,MB[I].Width,MB[I].Height),
                          MyBmp.Canvas,
                          Rect(MB[i].Width * i,0,MB[i].Width * (i+1),MB[I].Height));
    MyIcons[i].ReleaseHandle; //Free Resource
    IconInfo.fIcon := True;
    IconInfo.xHotspot := 0;
    IconInfo.yHotspot := 0;
    IconInfo.hbmColor := MB[i].Handle;
    IconInfo.hbmMask := MB[i].MaskHandle;  //Doesn't work yet
    MyIcons[I].Handle := CreateIconIndirect(IconInfo);

    //changes to the tray
    NotifyIconData.cbSize := SizeOf(NotifyIconData);
    NotifyIconData.hIcon := MyIcons[i].Handle;
    NotifyIconData.szTip := 'Shows the Mouseposition';
    NotifyIconData.uCallbackMessage := 0;
    NotifyIconData.uFlags := NIF_ICON or NIF_TIP;
    NotifyIconData.uID := MyIconIDs[i];
    NotifyIconData.Wnd := Application.Handle;

    Shell_NotifyIcon(NIM_Modify, @NotifyIconData);

    MB[i].Free
  end;
  MyBmp.Free;
  Timer1.enabled := True;  //prepare for next event
end;

end.

meikl ;-)
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2165624
Chad,

changing the icon is easy. You already have the code to add a tray icon. Now change NIM_ADD to NIM_MODIFY. In the TNotifyIconData.uFlags pass NIF_ICON as you only want to change the icon and that's all.

Ciao, Mike
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2165858
Or are you using a TrayIcon component? Which one?

Regards, Madshi.
0
 

Author Comment

by:dhertzfe
ID: 2166143
Duh....I guess I didn't pay attention to what I was reading about Shell_NotifyIcon.  I missed NIM_Modify. I was trying to delete the Icon and then re-install and that didn't work either.

You gave a perfect example Meikl and was first.

Thanks to you too Mike!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

600 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