List Control 选中一项,然后点鼠标右键,弹出一个菜单怎样实现?谢谢~~~

解决方案 »

  1.   

    呵呵listctrl好象没有右键抬起的消息以前我也是郁闷了一段时间不知道怎么实现啊。
      

  2.   

    不知你的list control是指CListBox,还是CListCtrl?
    下面的方法是将其当成前者,因为对于后者而言很easy!
    给你一种解决办法,你可以推而广之!
    ----------------------------
    以formview为例
    void CWord_dbView::OnInitialUpdate()
    {
    m_pSet = &GetDocument()->m_word_dbSet;
    CRecordView::OnInitialUpdate();
    GetParentFrame()->RecalcLayout();
    ResizeParentToFit();

    //m_ListCtrl是list box控件对应控件变量
    //used for improving the performence!
    this->m_ListCtrl.InitStorage(256,16);

    //***********************************
    //修改list box 的窗口风格,使得其可以向父窗口发送WM_PARENTNOTIFY消息
    DWORD dwStyle;
    dwStyle=::GetWindowLong(this->m_ListCtrl.m_hWnd,GWL_EXSTYLE);
    dwStyle&=((~0)&~WS_EX_NOPARENTNOTIFY);
    ::SetWindowLong(this->m_ListCtrl.m_hWnd,GWL_EXSTYLE,dwStyle);
    //************************************}
    如果是对话框的话,将//******//之间的代码放在InitDialog()中就行了在父窗口中添加WM_PARENTNOTIFY
    void CWord_dbView::OnParentNotify(UINT message, LPARAM lParam) 
    {
    CRecordView::OnParentNotify(message, lParam);

    // TODO: Add your message handler code here
    if(message==WM_RBUTTONDOWN)
    {
    int iCurSel=this->m_ListCtrl.GetCurSel();
    if(iCurSel!=LB_ERR )//there is a selection item in listbox
    {
    CString str;
    this->m_ListCtrl.GetText(iCurSel,str);
    int x=LOWORD(lParam);
    int y=HIWORD(lParam);
    CPoint pt(x,y);
    ClientToScreen(&pt);
    CMenu menu;
    menu.LoadMenu(IDR_MAINFRAME);
    menu.GetSubMenu(0)->AppendMenu(MF_SEPARATOR);
    menu.GetSubMenu(0)->AppendMenu(MF_STRING,10001,str);
    menu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTBUTTON,pt.x,pt.y,this);
    menu.Detach();
    } }
    }----------------------------
    当然了你也可以派生一个新的CUserListBox类,
    在里面响应WM_RBUTTONDOWN,或者WM_CONTEXTMENU来完成要求
    -----------------------------
    Just run & enjoy [email protected]
      

  3.   

    对于listctrl可以直接相应消息NM_RCLICK就ok了
    ------------------------------
    void CWordListView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    // TODO: Add your control notification handler code here /*typedef struct tagNMITEMACTIVATE{
        NMHDR   hdr;
        int     iItem;
        int     iSubItem;
        UINT    uNewState;
        UINT    uOldState;
        UINT    uChanged;
        POINT   ptAction;
        LPARAM  lParam;
        UINT    uKeyFlags;
    } NMITEMACTIVATE, FAR *LPNMITEMACTIVATE */
    NMITEMACTIVATE *pNotifier=(NMITEMACTIVATE*)pNMHDR;
    char chstr[MAX_PATH];
    CListCtrl &theCtrl=this->GetListCtrl();
    theCtrl.GetItemText(pNotifier->iItem,pNotifier->iSubItem,chstr,MAX_PATH);
    CString str=chstr;
    if(!str.IsEmpty() )//there is a selection item in listctrl
    {
    CPoint pt(pNotifier->ptAction.x,pNotifier->ptAction.y);
    ClientToScreen(&pt);
    CMenu menu;
    menu.LoadMenu(IDR_MAINFRAME);
    menu.GetSubMenu(0)->AppendMenu(MF_SEPARATOR);
    menu.GetSubMenu(0)->AppendMenu(MF_STRING,10001,str);
    menu.GetSubMenu(0)->TrackPopupMenu(
    TPM_RIGHTBUTTON,pt.x,pt.y,this);
    menu.Detach();
    } *pResult = 0;
    }
      

  4.   

    我看还是psusong(人类失去指针,世界将会怎样?) 的方法不错,就是响应的消息不对,应该是右键抬起消息才对。
      

  5.   

    在CListCtrl中添加右键菜单:void CYourListCtrl::OnRButtonDown(...)
    {
    ...
       CMenu menu;
       VERIFY(menu.LoadMenu(IDR_YOUR_MENU));
       CMenu* pPopup = menu.GetSubMenu(0);
       ASSERT(pPopup != NULL);   ClientToScreen(&point);
       pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,point.x,point.y,AfxGetMainWnd());...
    }
      

  6.   

    IDR_YOUR_MENU是菜单资源,你要在资源中添加。
      

  7.   

    to elabs(洋洋) 
    我看还是psusong(人类失去指针,世界将会怎样?) 的方法不错,就是响应的消息不对,应该是右键抬起消息才对。
    --------------------------------
    ???
    老兄!有没有搞错!
    你相应右健抬起消息干什么?有什么用?
    NM_RCLICK就是右建安下的消息,直接用就ok !to 搂住
    你自己添加菜单资源呀!
    resource view--->right click the "menu folder",select "insert menu",
    and then design you own menu!
    last use the id of your menu to replace the id:IDR_MAINFRAME which I have said above