...

View Full Version : Icon on the tray icon



Aymen++
12-07-2002, 08:31 PM
I want to make an icon appears beside the clock, how can I do it with visual c++???

maes
12-07-2002, 11:13 PM
It is called a system tray icon.
Take a look at the Shell_notifyIcon() (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shell_notifyicon.asp) function.


#include <shellapi.h>

#define WM_SHELLNOTIFY WM_USER+5
#define ID_TRAY 0
#define ID_RESTORE 1000
#define ID_MINIMIZE 1001
#define ID_CLOSE 1002

//In your Window Procedure function
POINT pt;
static NOTIFYICONDATA NData;
HMENU hPopupMenu = CreatePopupMenu();
AppendMenu(hPopupMenu, MF_STRING, ID_RESTORE, "Restore");
AppendMenu(hPopupMenu, MF_STRING, ID_MINIMIZE, "Minimize");
AppendMenu(hPopupMenu, MF_STRING, ID_CLOSE, "Close Program")



case WM_CREATE: // Or case WM_INITDIALOG:
NData.cbSize = sizeof(NOTIFYICONDATA);
NData.hWnd = hwnd;
NData.uID = ID_TRAY;
NData.uFlags = NIF_ICON+NIF_MESSAGE+NIF_TIP;
NData.uCallbackMessage = WM_SHELLNOTIFY;
NData.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
lstrcpy(&NData.szTip[0], "Press right mouse button for menu or left to restore");
Shell_NotifyIcon(NIM_ADD, &NData);
return 0;



case WM_COMMAND:
if (lParam == 0)
{
switch(LOWORD (wParam))
{
case ID_RESTORE:
ShowWindow(hwnd, SW_RESTORE);
break;
case ID_MINIMIZE:
ShowWindow(hwnd, SW_MINIMIZE);
break;
case ID_CLOSE:
DestroyWindow(hwnd);
break;
}
}
...
return 0;



case WM_SHELLNOTIFY:
if (wParam == ID_TRAY)
{
if (lParam == WM_RBUTTONDOWN)
{
GetCursorPos(&pt);
TrackPopupMenu(hPopupMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, hwnd, NULL);
}
else if (lParam == WM_LBUTTONDOWN)
SendMessage(hwnd, WM_COMMAND, ID_RESTORE, 0);
}
else
DefWindowProc(hwnd, msg, wParam, lParam);
return true;


I hope this is what you're looking for

Aymen++
12-08-2002, 10:09 PM
Thank you for your concern :)
but i think there is many errors :D , for example:
case with out switch and hwnd undeclared identifier and hInst...etc
so, i hope from you to explain how to do it step by step because i'm beginner. :confused:
with many thanks... :o

maes
12-08-2002, 11:14 PM
Well, all the cases belong to the switch that handles your Window messages (the big switch statement in your main window procedure) I wrote it this way to make the post shorter, (I'm lazy :D)
the hInst is the handle to your instance
hwnd is the handle to the window
here's an example. I've removed, the winmain function because that is almost the same for every normal application
Also, don't forget to create an icon. and Call it IDI_ICON1


#include <windows.h>
#include <shellapi.h>
#include "resource.h" //for your icon


#define WM_SHELLNOTIFY WM_USER+5
#define ID_TRAY 0
#define ID_RESTORE 1000
#define ID_MINIMIZE 1001
#define ID_CLOSE 1002


LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine,int iCmdShow)
{
//everything from winmain goes here
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInst;
static NOTIFYICONDATA NData;
static HMENU hPopupMenu;
POINT pt;

switch (msg)
{
case WM_CREATE:
hInst=((LPCREATESTRUCT) lParam)->hInstance;
hPopupMenu = CreatePopupMenu()
AppendMenu(hPopupMenu, MF_STRING, ID_RESTORE, "Restore");
AppendMenu(hPopupMenu, MF_STRING, ID_MINIMIZE, "Minimize");
AppendMenu(hPopupMenu, MF_STRING, ID_CLOSE, "Close Program");

NData.cbSize = sizeof(NOTIFYICONDATA);
NData.hWnd = hwnd;
NData.uID = ID_TRAY;
NData.uFlags = NIF_ICON+NIF_MESSAGE+NIF_TIP;
NData.uCallbackMessage = WM_SHELLNOTIFY;
NData.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
lstrcpy(&NData.szTip[0], "Right click for menu, left click to restore...");
Shell_NotifyIcon(NIM_ADD, &NData);
return 0;


case WM_COMMAND:
if (lParam == 0)
{
switch(LOWORD (wParam))
{
case ID_RESTORE:
ShowWindow(hwnd, SW_RESTORE);
break;

case ID_MINIMIZE:
ShowWindow(hwnd, SW_MINIMIZE);
break;

case ID_CLOSE:
DestroyWindow(hwnd);
break;
}
}
return 0;
case WM_SHELLNOTIFY:
if (wParam == ID_TRAY)
{
if (lParam == WM_RBUTTONDOWN)
{
GetCursorPos(&pt);
TrackPopupMenu(hPopupMenu, TPM_RIGHTALIGN, pt.x, pt.y, 0, hwnd, NULL);
}
else if (lParam == WM_LBUTTONDOWN)
{
SendMessage(hwnd, WM_COMMAND, ID_RESTORE, 0);
}
}
else DefWindowProc(hwnd, msg, wParam, lParam);
return 0;

case WM_CLOSE:
DestroyWindow(hwnd);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}

I hope it will work now. If not, let me know

Aymen++
12-09-2002, 07:29 PM
Thank you very much my friend...
but i forgot to tell you that i have a dialog designed with mfc application (dialog based), i want to make it visible with an icon appear in the system tray, but nothing in the task bar appear for it... and i don't know how to convert the code that you wrote it to a dialog application designed by mfc.:confused:
with many thnks :o

Aymen++
12-11-2002, 10:15 AM
Where r u maes??????????
pleas don't forget me:mad:

maes
12-11-2002, 06:31 PM
nah, I haven't forgot about you.
But I've been bussy.
My teacher asked me to give a 2 hour presentation (for the whole class) about mysql and php. not that difficult you say. Well it is if you only started with html 6 months ago.

Anyway, everything went ok I think.
So now I have time for your problem again.
But (here is allways a "but")
unfortunatly (sp?) I don't know anything about mfc :(
I wanted to learn it, but I never found the time.

So I don't think I can help you with mfc

Aymen++
12-11-2002, 06:44 PM
Ok,
i have two questions now:
1- can you write a program (in vc++) that display a widow and draw a simple line (or any thing like circle... etc) on it?
2- how can i draw on the desktop?:confused:

maes
12-11-2002, 07:31 PM
MFC or Win32 API?
it is probably mfc, so I can't tell you how to do it.
But here's the aswer for WIN32 API

again, the case belongs to the big switch( msg) statement as befor


HDC hdc;
PAINTSTRUCT ps;
//...

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
Ellipse(hdc,20,20,50,50);
EndPaint(hwnd,&ps);
return 0;

that will draw a cirle

>>2- how can i draw on the desktop?
you don't.
I think there is a way to do it, but it is bad programming.
when you draw something to the screen, make sure it is allways in your own client area. But that doesn't mean that you can't have it look like you're drawing on the desktop.
for instance, you can remove the top caption, no border. and you can make your client area transparant. (or set a color key or something)
I believe that is the way your nice annoying paperclip (MS office assitant) works. but I haven't done anything like it, so I have no code for you :(

Aymen++
12-11-2002, 07:50 PM
Thank you very much... ;)
that's what i need (drawing win32)...

Aymen++
12-14-2002, 12:35 PM
But there is a problem,
when i write click the icon on the system tray, the mouse cursor changed to clock (not arrow), that is only when i move on the menu that contains these items (Restore, Minimize, Close program); :(

maes
12-14-2002, 01:24 PM
Never happened to me.
Normaly when you wan to change the cursor, you have to explicit program it that way. it shouldn't happen by itself

Aymen++
12-14-2002, 03:25 PM
but also when i click on another place the menu didn't disapear!!

Aymen++
12-14-2002, 04:03 PM
However, i want to make my origion for drawing (0, 0) at the center of the window not at the up left corner :confused:

maes
12-14-2002, 04:49 PM
Then you'll have to start working with offsets. Maybe there is an other way, if there is, I haven't found it:



RECT r;
int offsetx, offsety;

case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&r);
offsetx=r.right/2;
offsety=r.bottom/2;
Ellipse(hdc, offsetx+20,offsety+20,offsetx+50,offsety+50);
EndPaint(hwnd,&ps);
return 0;

this will draw a circle with radius 15 and center at (20,20)
with (0,0) as the center of the client area.
A little more overhead, but it gets the job done.

>>but also when i click on another place the menu didn't disapear!!
You're right about that :eek: . I never noticed it.

Aymen++
12-14-2002, 08:30 PM
Ok this is the last question (may be :) )...
I want to make my window transparent...:(

Aymen++
12-14-2002, 09:16 PM
i found it :D
you can make it transparent by:


SetWindowLong (hwnd, GWL_EXSTYLE, GetWindowLong (hwnd, GWL_EXSTYLE ) | WS_EX_LAYERED ) ;

typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD);

PSLWA pSetLayeredWindowAttributes;

HMODULE hDLL = LoadLibrary ("user32");

pSetLayeredWindowAttributes = (PSLWA) GetProcAddress(hDLL,"SetLayeredWindowAttributes");

if (pSetLayeredWindowAttributes != NULL) {

pSetLayeredWindowAttributes (hwnd, RGB(255, 255, 255), 20, LWA_COLORKEY);

SetWindowPos(hwnd,HWND_TOP, 570, 425, 0, 0, SWP_NOSIZE);

}

now i don't want to see a bar for my window on the task bar, i want to see only it's icon on the system tray....:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum