Discussion:
Create a System Tray Program
(too old to reply)
Kenneth Keeley
2006-01-11 05:08:25 UTC
Permalink
Hi,
I wish to create a program that runs on multiple pc's that are networked
that when launched it places a icon in the system tray and then displays the
main user window. I want the system tray icon part of the program to look
after the network chatting between the different computers. I then want the
user to be able to close the main program window but have the system tray
icon remain running and still chatting with the other computers. I would
then like the user to be able to re-open the main user window by either
double clicking on the system tray icon or by clicking on the normal program
shortcut as would have been done the first time the program was launched. I
want to have only one copy of the program running on a computer at any one
time. The user should have to select a special close/shutdown option from
the program menu or system tray icon menu to get the program to close fully
and remove the system tray icon. Clicking the X in the top corner of the
main window should only close the main window not the whole program. A
sample of this behavour would may be a virus scanner or the download manager
GetRight. I can already do the networking part along with creating the
system tray icon, I have also found a way of making sure only one copy of
the program is running. What I would like are some samples of how to make
the main program window display and close with closeing the system tray
icon, how to make it that when the second copy of the program is launched
that all it does is reopen the main window.

Thanks for any help.
Kenneth Keeley
William DePalo [MVP VC++]
2006-01-11 05:32:40 UTC
Permalink
Post by Kenneth Keeley
What I would like are some samples of how to make
the main program window display and close with closeing the system tray
icon
Most applications do this by hiding and showing the window. Given a window
handle it is a simple matter of calling ShowWindow() passing either of the
flags SW_SHOWNORMAL or SW_HIDE.
Post by Kenneth Keeley
how to make it that when the second copy of the program is launched
that all it does is reopen the main window.
You send a message to the application. Often developers create a unique
class name for their applications (perhaps with a tool like GUIDGEN). Then
you locate an instance of your window by passing FindWindow() the class
name. Then you display it with ShowWindow().

Regards,
Will
Kenneth Keeley
2006-01-11 22:00:35 UTC
Permalink
Thanks for the help, I still have a few questions.
Post by William DePalo [MVP VC++]
Most applications do this by hiding and showing the window. Given a window
handle it is a simple matter of calling ShowWindow() passing either of the
flags SW_SHOWNORMAL or SW_HIDE.
That sounds fine but what happens when te user clicks on the X at the top of
the program window. Wouldn't that still close the program, or is ther a way
to stop that.
Post by William DePalo [MVP VC++]
You send a message to the application. Often developers create a unique
class name for their applications (perhaps with a tool like GUIDGEN). Then
you locate an instance of your window by passing FindWindow() the class
name. Then you display it with ShowWindow().
This sounds good, could you show me a small sample of this type of code. I
assume the second copy of the program should then quit after it has found
and activated the first copy.

Thanks for the help.
Kenneth
Kellie Fitton
2006-01-11 22:14:40 UTC
Permalink
Hi,

If the endUser clicks the X button to close the window, the window
procedure should receive the
window message WM_CLOSE, then you can decide what to do next:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowmessages/wm_close.asp

Do you want sample code that shows how to use the following functions
?

FindWindow()
ShowWindow()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindow.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/showwindow.asp

Kellie.
Kenneth Keeley
2006-01-11 23:37:42 UTC
Permalink
Thanks for this information it has helped me a lot.
Post by Kellie Fitton
If the endUser clicks the X button to close the window, the window
procedure should receive the
Do I close the window, or is it just hidden? What commands do I have to use,
is it just the ShowWindow() command.
Post by Kellie Fitton
Do you want sample code that shows how to use the following functions
?
FindWindow()
ShowWindow()
I did me these functions but also how do other people create a unique class
name for their applications. All the samples that I have seen name there
class something like "MyProgramClass". Is this what is ment by a unique name
or do they mean that every time the program starts it creates a new class
name (ie "MyProgramClass1" then "MyProgramClass2" and so on). If they do it
by the first method how does FindWindow() get the handle of the other copy
of the program and not the handle to it's own window or should the testing
be done before I create the main window.

Thanks
Kenneth
Kellie Fitton
2006-01-12 05:31:38 UTC
Permalink
Hi,

1). When your window procedure receives the windows message
WM_CLOSE, you can either quit the application by calling
the function PostQuitMessage(), or you can hide the window
by calling the following function:

ShowWindow(hWindow, SW_HIDE);

2). When you create a window you must give it a class name that
is unique, the class name can be ANY name you choose and you
can register with the API's RegisterClass(), RegisterClassEx(),
the reason that the class name should be unique is because
when you try to enumerate multiple windows on the deskTop,
each window will have its own unique class name that you can
use to locate or identify any window. LikeWise, the window
name also knowing as the window's TITLE, should be unique for
the same reason stated above as the windows class name, however,
starting your application does NOT change the class name or the
windows title name, NOR create a new class name or a title name.

3). When you call the functions FindWindow() or FindWindowEx(),
those functions will retrieve the handle of the topLevel window
whose Class Name and Window Name MATCHES the specified strings,
these are the strings that you have created UNIQUELY when you
created the window and registered its class as mentioned above.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/messagesandmessagequeues/messagesandmessagequeuesreference/messagesandmessagequeuesfunctions/postquitmessage.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windowclasses/windowclassreference/windowclassfunctions/registerclass.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/createwindow.asp

Kellie.
Kenneth Keeley
2006-01-12 23:00:34 UTC
Permalink
Thanks for all your help. I have know managed to get my program to start
behaving the way I want it to. It now opens and creates a system tray icon.
If you click on the X it closes the window but not the system tray icon, If
you double click the system tray icon it restores the window, Also if you
start a second copy of the program it restores the window of the first copy
and then closes.

I still have to do some work on the context menus for the system tray icon
and a heap of other things that I am sure to find.

Kenneth Keeley.

Kellie Fitton
2006-01-11 17:06:14 UTC
Permalink
Hi,

You can use the following API to manage the system tray icon:

Shell_NotifyIcon()

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shell_notifyicon.asp

Hope these information helps,

Kellie.
Kenneth Keeley
2006-01-11 21:54:52 UTC
Permalink
Thanks for the link. I already new how to create the tray icon. my problem
is more about how to make all the parts work together.
Post by Kellie Fitton
Hi,
Shell_NotifyIcon()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shell_notifyicon.asp
Post by Kellie Fitton
Hope these information helps,
Kellie.
Continue reading on narkive:
Loading...