Solved

Repair missing tray icon.

Posted on 2006-07-07
11
489 Views
Last Modified: 2010-04-05
Hi.

My application stays minimized in the system tray.
Sometimes explorer crash and when it reloads it always forget to create the icons of my application.

How can i add a check to see if my icons is missing ?

Thanks in advance.
0
Comment
Question by:CodedK
11 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17057752
Are you sure you application is running after Explorer reloads? Did you checked it in Task Manager?
0
 
LVL 16

Author Comment

by:CodedK
ID: 17057820
Hi Ivanov_G .
Yes it runs.
And the big problem is that without those icons (2) there is no functionality !
0
 
LVL 17

Expert Comment

by:geobul
ID: 17057858
Hi,

Have you tried moving the mouse button over the tray? Is it possible the two icons to be among the hidden ones? (if there is < button).

Are you using a component (third-party) or API functions directly for adding the app in the tray? If you have coded that could you show the code?

Regards, Geo
0
 
LVL 28

Assisted Solution

by:ciuly
ciuly earned 50 total points
ID: 17057878
that "bug" happens to be an explorer bug. as many others.

one way to go is to see when explorer is restarted and then recreate the icons. (hooks will be on this one, or a slow timer, say 1 check in a 1-3 or more seconds, though this approach will keep the cpu busy)
another way is to again check in a timer that the icons have valid handles (though this might be true). I never tried, but you could check to see exactly what becomes invalid after explorer restart.

best way to go is probably as described in this blog:
http://blogs.msdn.com/triballibrarian/archive/2004/06/25/166328.aspx
0
 
LVL 16

Author Comment

by:CodedK
ID: 17058098
Hi Geobul... Yes Api functions ...
Here is a small portion of the code:

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
uses ..... ShellApi {For Tray notification definitions }

const
  WM_ICONTRAY=WM_USER+1;

  public
    { Public declarations }
    F1Tray:TNotifyIconData;
    F1Icon:TIcon;
    F2Tray:TNotifyIconData;
    F2Icon:TIcon;
....


procedure TForm1.FormCreate(Sender: TObject);
Begin

//Enable tray icon 1
With F1Tray do
 begin
  cbSize:=SizeOf(F1Tray);
  Wnd:=Handle;
  uID:=0;
  uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
  uCallbackMessage:=WM_ICONTRAY;
  F1Icon:=TIcon.Create;
  Image.GetIcon(0,F1Icon);
  hIcon:=F1Icon.Handle;
  StrPCopy(szTip,'This is the first icon.');
 end;

Shell_NotifyIcon(NIM_ADD,@F1Tray);


//Enable tray icon 2
With F2Tray do
 begin
  cbSize:=SizeOf(F2Tray);
  Wnd:=Handle;
  uID:=1;
  uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
  uCallbackMessage:=WM_ICONTRAY;
  F2Icon:=TIcon.Create;
  Image.GetIcon(0,F2Icon);
  hIcon:=F2Icon.Handle;
  StrPCopy(szTip,'This is the second icon');
 end;

Shell_NotifyIcon(NIM_ADD,@F2Tray);

end;

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Hi Ciuly... I've seen this happen many times with other programs.
But Explorer doesnt like my app at all... It always destroy the icons when it crash.
Interesting link.

I'll create a timer that will do something ONLY IF my app capture the message of TaskbarCreated...
This way i want use cpu power....

How can i do that ?
The article says the TaskbarCreated string and then broadcasts this message to all top-level windows.
Mine is minimized....

Thanks for any help :)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Accepted Solution

by:
geobul earned 200 total points
ID: 17058158
Hi again,

The following is a solution from another expert I haven't tried myself but it's worth:

type
 TForm1 = class(TForm)
   procedure FormCreate(Sender: TObject);
 private
   { Private-Deklarationen }
 protected
   FMsgTaskbarCreated : dword;
   procedure WndProc(var Message: TMessage); override;
 public
   { Public-Deklarationen }
 end;

var
 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 FMsgTaskbarCreated := RegisterWindowMessage('TaskbarCreated');
end;

procedure TForm1.WndProc(var Message: TMessage);
begin
 inherited;
 if Message.Msg = FMsgTaskbarCreated then
   Caption := Caption + '+';
end;

end.

Regards, Geo
0
 
LVL 16

Author Comment

by:CodedK
ID: 17058246
Thanks Geo it works in a way....

When i terminate explorer (with my app running) and then reload explorer
I get "System error 1400 - Invalid Window Handle"

I use the proc like that :

procedure TForm1.WndProc(var Message: TMessage);
begin
 inherited;
 if Message.Msg = FMsgTaskbarCreated then
 begin
   Shell_NotifyIcon(NIM_ADD,@F1Tray);
   Shell_NotifyIcon(NIM_ADD,@F2Tray);
 end;
end;
--------------------------------------------------

ONLY F2 Icon loads to the tray, this restores half the functionality of my app...
Why F1 doesnt load ?
0
 
LVL 16

Author Comment

by:CodedK
ID: 17058307
Strange... very strange

I've done the following :

procedure TForm1.WndProc(var Message: TMessage);
begin
 inherited;
 if Message.Msg = FMsgTaskbarCreated then
 begin
   //Enable tray icon 1
   With F1Tray do
    begin
     cbSize:=SizeOf(F1Tray);
     Wnd:=Handle;
     uID:=0;
     uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
     uCallbackMessage:=WM_ICONTRAY;
     F1Icon:=TIcon.Create;
     Image.GetIcon(0,F1Icon);
     hIcon:=F1Icon.Handle;
     StrPCopy(szTip,'This is the first icon.');
    end;
     
Shell_NotifyIcon(NIM_ADD,@F1Tray);
     
  //Enable tray icon 2
  With F2Tray do
   begin
    cbSize:=SizeOf(F2Tray);
    Wnd:=Handle;
    uID:=1;
    uFlags:=NIF_MESSAGE+NIF_ICON+NIF_TIP;
    uCallbackMessage:=WM_ICONTRAY;
    F2Icon:=TIcon.Create;
    Image.GetIcon(0,F2Icon);
    hIcon:=F2Icon.Handle;
    StrPCopy(szTip,'This is the second icon');
  end;

Shell_NotifyIcon(NIM_ADD,@F2Tray);
 end;

end;


Now i dont get the "Invalid Window Handle" error !
But it only loads only the first paragraph !!!!!!!

If i put the icon1 paragraph first then it only loads paragraph1 else the second...
0
 
LVL 16

Author Comment

by:CodedK
ID: 17058320
It seems like when ever it likes it gives the error... I got again the "Invalid handle" error.
0
 
LVL 16

Author Comment

by:CodedK
ID: 17064063
Geo the procedure you gave works.
I should destroy first the icons.
But the message "System error 1400. Invalid Handle" appears every time.
Any ideas ?
0
 
LVL 16

Author Comment

by:CodedK
ID: 17067824
Thanks Geo and Ciuly :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Firewall issues 6 85
How to fill array with TArray.Create? 14 68
Delphi Form ownership 4 50
LAN or WAN ? 11 59
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now