Hi,
Can we right click on an icon in systray by a shortcut key? (not own application, anyone in tray)
Printable View
Hi,
Can we right click on an icon in systray by a shortcut key? (not own application, anyone in tray)
Do want to programmatically simulate a right click in the systray?
What exactly are you trying to do?
gg
Yes, as you guessed. Sorry, couldnt explained well. I want to access to popup menu on a shortcut.
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, ¬ifyIconData);
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, ¬ifyIconData);
PostQuitMessage(0) ;
return 0 ;
}
default:
return DefWindowProc( hwnd, msg, wParam, lParam ) ;
}
}
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