0

我正在创建 mfc 应用程序的任务栏图标,并在我编写的 MyView.cpp 文件中

    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;
    }

在 My.cpp 中

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

应用程序启动,图标出现在任务栏上,但鼠标悬停时消失。我做错了什么吗?谢谢

4

1 回答 1

3

AfxGetApp()->m_pMainWnd指向主框架窗口,而不是视图。我怀疑框架窗口正在接收WMU_NOTIFY_TASKBAR_ICON消息并且没有处理它,因此 Windows 删除了该图标。

您可以在框架窗口类中处理消息,也可以将句柄传递给视图,如下所示:

void CMyView::AddTaskbarIcon()
{
     ...
     nid.hWnd = GetSafeHwnd();
     ...
}
于 2013-04-03T20:18:32.657 回答