Windows 介面:右鍵菜單的實現

來源:互聯網
上載者:User

1,首先在資源欄的 menu 項建立好要使用的菜單;

2,對於一般的視窗和控制項,可以使用 ClassWizard 建立其訊息處理函數 OnContextMenu() ; 

3,特定地區的右鍵菜單也可以建立滑鼠右鍵訊息響應;

4,在訊息響應函數中快顯功能表:

void CScanFileList::OnContextMenu(CWnd* /*pWnd*/, CPoint point)
{
 // TODO: Add your message handler code here
 //按右鍵顯示菜單
 CMenu Menu;
 Menu.LoadMenu(IDR_MAINFRAME);
 CMenu *pPopUpMenu = Menu.GetSubMenu(1);

 //show menu
 pPopUpMenu->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, AfxGetMainWnd());
}

5,如果是響應右鍵訊息,那麼快顯功能表的位置參數就要使用 API 來獲得了,沒錯,就是擷取滑鼠當前位置:

 case WM_RBUTTONDOWN:
  POINT pt;
  if (!GetCursorPos(&pt))
  {
   // error
   CString strErr;
   strErr.Format(_T("Cannot pop mouse right button memu, GetCursorPos failed !/r/n/r/nError: %s")
    , GetErrorMsgAndCode()    // 本函數參見我的另一文:http://tb.blog.csdn.net/TrackBack.aspx?PostId=2071783
    );
   AfxMessageBox(strErr);
   return;
  }
  CMenu rbMenu;
  rbMenu.LoadMenu(IDR_MENU_RButton);
  CMenu *pPopUpMenu = rbMenu.GetSubMenu(0);
  pPopUpMenu->TrackPopupMenu(TPM_LEFTALIGN, pt.x, pt.y, this);
  break;

6,That's All,就是這麼簡單。(ps:有空就給點回複吧!:)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.