Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Icon on the tray icon

    I want to make an icon appears beside the clock, how can I do it with visual c++???

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is called a system tray icon.
    Take a look at the Shell_notifyIcon() function.
    Code:
    #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
    Last edited by maes; 12-07-2002 at 10:17 PM.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your concern
    but i think there is many errors , 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.
    with many thanks...
    Last edited by Aymen++; 12-08-2002 at 09:16 PM.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 )
    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
    Code:
    #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

  • #5
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    with many thnks

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    Where r u maes??????????
    pleas don't forget me

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by maes; 12-11-2002 at 05:33 PM.

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    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

  • #10
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much...
    that's what i need (drawing win32)...

  • #11
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);

  • #12
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but also when i click on another place the menu didn't disapear!!

  • #14
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    However, i want to make my origion for drawing (0, 0) at the center of the window not at the up left corner

  • #15
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then you'll have to start working with offsets. Maybe there is an other way, if there is, I haven't found it:

    Code:
    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 . I never noticed it.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •