Focus problem during a right click in the windows system tray
Hello!
I've written a small application that creates a system tray icon using the Shell_NotifyIcon from the w32api with Mingw. When the user does a right click on the icon, a popup menu shows up. However, there is a problem when the application is used the first the first time: On the first right click of the icon, not only the menu of my application shows up, but also the popup menu of the windows taskbar (just as if I right clicked on the taskbar itself). When I first click on the taskbar or any other icon, even my first click on my own application is right. I guess there is some problem with the focus, but I couldn't find it and also NIM_SETFOCUS didn't help. Furthermore it seems that this problem only shows up on Windows XP, not 2000. Any hints?
Thanks!
Faizel
# 3 Re: Focus problem during a right click in the windows system tray
In response to which message and using which function is the popup menu shown?
I have this function that catches the message where WM_SYSTEMTRAYICON is the uCallbackMessage of the system tray.
LRESULT CALLBACK WndProcMain (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_SYSTEMTRAYICON:
{
return(SystemTrayIcon.TrayIconMsgProc(wParam, lParam));
}
case WM_DESTROY:
{
PostQuitMessage (0);
return (0);
}
}
return (DefWindowProc (hwnd, message, wParam, lParam));
}
So there we call the function TrayIconMsgProc that then shows up the popup menu.
LRESULT CSystemTrayIcon::TrayIconMsgProc(WPARAM wParam, LPARAM lParam)
{
//check if the message is for this tray icon
if(wParam != m_pIconData->uID)
return(0L);
//if the right mouse button was clicked, we show the popup menu at the current mouse pointer location
if(lParam == WM_RBUTTONDOWN)
{
if(m_hPopupMenu == NULL)
return 0L;
POINT CursorPos;
//we need to set a foreground window so that the menu is closed after the
//window looses the focus
SetForegroundWindow(m_pIconData->hWnd);
if(GetCursorPos(&CursorPos))
TrackPopupMenu(m_hPopupMenu, TPM_RIGHTBUTTON, CursorPos.x, CursorPos.y, 0, m_pIconData->hWnd, NULL);
}
return(0L);
}
Maybe there is some mistake in the function showing up the popup menu, but I'm quite stuck.
Thanks in advance!
PS: No, I'm not using anything like Google toolbar or Google Desktop.