导航:[首页]->[wingui]->[托盘菜单基本用法]
    class CMainDlg : public CDialogImpl<CMainDlg>
    {
    public:
        enum { IDD = IDD_MAINDLG };

        BEGIN_MSG_MAP(CMainDlg)
            MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
            MESSAGE_HANDLER(WM_USER + 1, OnUser)
            COMMAND_CODE_HANDLER(0,OnMenu)
            COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
        END_MSG_MAP()

        LRESULT OnMenu(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
        {
            // 如果是按钮消息
            if(!!hWndCtl){
                bHandled = FALSE;
                return TRUE;
            }
            switch(wID){
            case 100:
                return 0;
            case 101:
                return 1;
            case 102:
                return 2;
            }
            return 0;
        }

        // 消息响应
        LRESULT OnUser(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
        {
            // m_nid.uID     = 12345;
            UINT fuck_ = wParam;
            switch (lParam)
            {
            case WM_LBUTTONDBLCLK:
                {
                    // 左键双击
                    int i =0;
                    i++;
                    break;
                }
            case WM_RBUTTONUP:
                // Right mouse button upped
                {
                    // 右键
                    int i = 0;
                    i++;

                    CMenu menu;
                    menu.CreatePopupMenu();
                    menu.AppendMenu(MF_STRING,100,_T("菜单项1"));
                    menu.AppendMenu(MF_SEPARATOR);
                    menu.AppendMenu(MF_STRING,101,_T("菜单项2"));
                    // 特定位置插入
                    menu.InsertMenu(1,MF_BYPOSITION|MF_STRING,102,_T("菜单项3"));
                    CPoint pt_;
                    ::GetCursorPos(&pt_);
                    menu.TrackPopupMenu(TPM_LEFTALIGN, pt_.x,pt_.y,m_hWnd);
                    break;
                }
            }
            return 0;
        }
        LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
        {
            // center the dialog on the screen
            CenterWindow();

            // set icons
            HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
                IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
            SetIcon(hIcon, TRUE);
            HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
                IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
            SetIcon(hIconSmall, FALSE);

            // 创建托盘
            // http://msdn.microsoft.com/en-us/library/windows/desktop/bb762159(v=vs.85).aspx
            m_nid.cbSize  = (DWORD)sizeof(NOTIFYICONDATA);
            m_nid.hWnd    = this->m_hWnd;
            m_nid.uID     = 12345;
            m_nid.uFlags  = NIF_ICON | NIF_MESSAGE | NIF_TIP ;
            m_nid.uCallbackMessage = WM_USER + 1;             // 自定义的消息名称
            m_nid.hIcon   = hIconSmall;
            _tcscpy(m_nid.szTip,_T("服务器程序"));                // 信息提示条为"服务器程序"
            Shell_NotifyIcon(NIM_ADD, &m_nid);                // 在托盘区添加图标

            return TRUE;
        }

        LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
        {
            // 删除托盘
            Shell_NotifyIcon(NIM_DELETE, &m_nid);
            //EndDialog(wID);
            return 0;
        }

    private:
        NOTIFYICONDATA m_nid;
    };