Thread: right click on an icon in systray

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    35

    right click on an icon in systray

    Hi,

    Can we right click on an icon in systray by a shortcut key? (not own application, anyone in tray)
    Want to learn? Then try to teach...

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Do want to programmatically simulate a right click in the systray?

    What exactly are you trying to do?

    gg

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    35
    Yes, as you guessed. Sorry, couldnt explained well. I want to access to popup menu on a shortcut.
    Want to learn? Then try to teach...

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Just happened to have this example around...

    Code:
    #include <windows.h> 
    
    
    LRESULT CALLBACK MainProc (HWND, UINT, WPARAM, LPARAM); 
    
    static TCHAR szClass[ ]   = TEXT( "SystemTrayApp" ); 
    static TCHAR szCaption[ ] = TEXT( "System Tray - Dante Shamest" ) ; 
    
    static const int TRAYICON_ID    = 2410 ; 
    static const int WM_TRAYICONMSG = WM_USER + 2 ; 
    
    int WINAPI WinMain (HINSTANCE hThisInstance, 
                        HINSTANCE hPrevInstance, 
                        LPSTR lpszArgument, 
                        int nFunsterStil) 
    
    { 
        HWND hwnd; 
        MSG messages; 
        WNDCLASSEX wincl; 
    
        wincl.hInstance     = hThisInstance; 
        wincl.lpszClassName = szClass; 
        wincl.lpfnWndProc   = MainProc; 
        wincl.style         = CS_DBLCLKS; 
        wincl.cbSize        = sizeof (WNDCLASSEX); 
        wincl.hIcon         = LoadIcon (NULL, IDI_APPLICATION); 
        wincl.hIconSm       = LoadIcon (NULL, IDI_APPLICATION); 
        wincl.hCursor       = LoadCursor (NULL, IDC_ARROW); 
        wincl.lpszMenuName  = NULL; 
        wincl.cbClsExtra    = 0; 
        wincl.cbWndExtra    = 0; 
        wincl.hbrBackground = GetSysColorBrush( COLOR_3DFACE ) ; 
    
        if (!RegisterClassEx (&wincl)) 
            return 0; 
    
        hwnd = CreateWindowEx ( 
               0, 
               szClass, 
               szCaption, 
               WS_OVERLAPPEDWINDOW|WS_VISIBLE, 
               CW_USEDEFAULT, 
               CW_USEDEFAULT, 
               544, 
               375, 
               HWND_DESKTOP, 
               NULL, 
               hThisInstance, 
               NULL 
               ); 
    
        while (GetMessage (&messages, NULL, 0, 0)>0) 
        { 
            TranslateMessage(&messages); 
            DispatchMessage(&messages); 
        } 
    
        return messages.wParam; 
    } 
    
    
    LRESULT CALLBACK MainProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
    { 
      static HMENU hIconMenu = CreatePopupMenu() ; 
    
      switch( msg ) 
      { 
        case WM_CREATE: 
          { 
            NOTIFYICONDATA notifyIconData ; 
            notifyIconData.cbSize           = sizeof( NOTIFYICONDATA ) ; 
            notifyIconData.hWnd             = hwnd ; 
            notifyIconData.uID              = TRAYICON_ID ; 
            notifyIconData.uFlags           = NIF_MESSAGE | NIF_ICON ; 
            notifyIconData.hIcon            = LoadIcon( 0, IDI_APPLICATION ) ; 
            notifyIconData.uCallbackMessage = WM_TRAYICONMSG ; 
            Shell_NotifyIcon(NIM_ADD, &notifyIconData); 
    
            AppendMenu( hIconMenu, MF_STRING, 0, TEXT( "MenuItem 1!" ) ) ; 
            AppendMenu( hIconMenu, MF_STRING, 0, TEXT( "MenuItem 2!" ) ) ; 
            AppendMenu( hIconMenu, MF_STRING, 0, TEXT( "MenuItem 3!" ) ) ; 
    
            return 0 ; 
          } 
    
        case WM_TRAYICONMSG: 
          { 
            if ( lParam == WM_RBUTTONDOWN ) 
            { 
              SetForegroundWindow(hwnd) ; 
              POINT p ; 
              GetCursorPos( &p ) ; 
              TrackPopupMenu( hIconMenu, TPM_LEFTALIGN, p.x, p.y, 0, hwnd, 0 ) ; 
            } 
          
            return 0 ; 
          } 
    
        case WM_DESTROY: 
          { 
            NOTIFYICONDATA notifyIconData ; 
            notifyIconData.cbSize           = sizeof( NOTIFYICONDATA ) ; 
            notifyIconData.hWnd             = hwnd ; 
            notifyIconData.uID              = TRAYICON_ID ; 
            Shell_NotifyIcon(NIM_DELETE, &notifyIconData); 
          
            PostQuitMessage(0) ; 
            return 0 ; 
          } 
    
        default: 
          return DefWindowProc( hwnd, msg, wParam, lParam ) ; 
      } 
    }

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    35
    Hi Dante Shamest,

    Thank you. You are too kind to give me an example. I will look at your code. Just now, seemed will work for just my own programs' tray icon. As an early comment, was need to access to any program icon in tray. But a very good start for me. I will try.

    Best Regards
    Want to learn? Then try to teach...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting coordinates of tray icon click
    By bennyandthejets in forum Windows Programming
    Replies: 8
    Last Post: 04-21-2004, 06:33 AM
  2. right click windows html icon errors
    By Benzakhar in forum Tech Board
    Replies: 10
    Last Post: 01-06-2004, 06:43 PM
  3. Replies: 2
    Last Post: 08-09-2003, 04:44 AM
  4. Systray Icon removeable
    By Onkel BeBu in forum Windows Programming
    Replies: 2
    Last Post: 08-07-2003, 04:48 AM
  5. Console App & Systray Icon
    By GaPe in forum Windows Programming
    Replies: 3
    Last Post: 06-14-2003, 05:08 AM