Nabijon
Nabijon

Reputation: 841

MFC C++ Tray Application Issue

I'm creating taskbar icon of a mfc application and in MyView.cpp file I've written

    static const UINT WMU_NOTIFY_TASKBAR_ICON = ::RegisterWindowMessage(_T("NOTIFY_TASKBAR_ICON"));

    IMPLEMENT_DYNCREATE(CMyView, CView)

    BEGIN_MESSAGE_MAP(CMyView, CView)
         // Standard printing commands
         ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
         ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
         ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
         // added messages by the developer
         ON_REGISTERED_MESSAGE(WMU_NOTIFY_TASKBAR_ICON, OnNotifyTaskbarIcon)
    END_MESSAGE_MAP()
    //...

    void CMyView::AddTaskbarIcon()
    {
         DWORD dwMessage = NIM_ADD;
         NOTIFYICONDATA nid;

         nid.cbSize = sizeof(NOTIFYICONDATA);
         nid.hWnd = HWND(AfxGetApp()->m_pMainWnd);
         nid.uID = 0;
         nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
         nid.uCallbackMessage = WMU_NOTIFY_TASKBAR_ICON;
         nid.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_SS_ICON));
         nid.dwInfoFlags = NIIF_INFO;
         ::Shell_NotifyIconW(dwMessage, &nid);
    }

    LRESULT CMyView::OnNotifyTaskbarIcon( WPARAM wParam, LPARAM lParam )
    {
        UINT uID = (UINT)wParam;
        UINT uMouseMsg = (UINT)lParam;

        switch(uMouseMsg)
        {
        case WM_LBUTTONDOWN:
            break;

        case WM_LBUTTONDBLCLK:
            //if(IsIconic())
            {
                break;
            }

        case WM_RBUTTONDOWN:
            {
                // must be implemented:
                // app popup menu will be showed
                CMenu* pMenu = GetMenu();
                if( pMenu )
                {
                    CMenu *pSubMenu = NULL;
                    pSubMenu = pMenu->GetSubMenu( 0 );
                    {
                        SetForegroundWindow(); // *** little patch here ***             
                        POINT pointCursor;             
                        ::GetCursorPos( &pointCursor );            
                        pSubMenu->TrackPopupMenu( TPM_RIGHTALIGN, 
                            pointCursor.x, pointCursor.y, 
                            this );
                    }
                }
            }
            break;

        case WM_RBUTTONDBLCLK:
            break;

        case WM_MOUSEMOVE:
            break;
        }

        return 0L;
    }

and in My.cpp

    BOOL CMyApp::InitInstance()
    {
            //...
            myViewPtr->AddTaskbarIcon();
            //...
    }

the app launches, the icon appears on the taskbar but it disappears on mouse hovering. Have I done something wrong? Thanx

Upvotes: 0

Views: 688

Answers (1)

mjk99
mjk99

Reputation: 1330

AfxGetApp()->m_pMainWnd points to the main frame window, not to the view. I suspect that the frame window is receiving the WMU_NOTIFY_TASKBAR_ICON message and not handling it so Windows removes the icon.

You could either handle the message in the frame window class, or pass the handle to the view instead, like this:

void CMyView::AddTaskbarIcon()
{
     ...
     nid.hWnd = GetSafeHwnd();
     ...
}

Upvotes: 3

Related Questions