•在许多应用软件中,当用户单击鼠标右键时,会弹出一个快捷菜单,用户可以通过快捷菜单方便地进行各种操作,如何右键弹出菜单功能呢?此实例实现了这一功能,效果如图
5.上位机VC MFC在控件上右键弹出菜单
•弹出式菜单功能的实现还是比较简单的,我们只要处理WM_CONTEXTMENU消息就可以了,在其消息处理函数中,调用菜单的TRACKPOPUP(),便可在指定的位置弹出菜单;
•WM_CONTEXTMENU消息由用户鼠标右键放开后触发;
•TRACKPOPUP()是菜单类Cmenu处理弹出菜单的函数,这两函数是我们实现右键弹出菜单功能的关键; 实现功能•1.新建一个对话框程序
•2.添加List控件及对应成员变量;
•3.在对话框类的添加WM_CONTEXTMENU消息处理函数,实现弹出菜单的功能 •voidCGkbc8Dlg::OnContextMenu(CWnd* pWnd, CPoint point) •{
• // TODO: Add your message handler code here
• CMenu m_popmenu;
• m_popmenu.LoadMenu(IDR_POPMENU);
• CMenu* m_submenu = m_popmenu.GetSubMenu(0);
•
• m_submenu->TrackPopupMenu(TPM_LEFTBUTTON|TPM_LEFTALIGN ,point.x,point.y,this);
•
• m_popmenu.DestroyMenu();
•4.另外列表控件内的内容从数据库中读取由函数LoadBookInfo实现
•5.我们结合VC6.0来进行具体的操作,实现当前的功能 void CGkbc8Dlg:oadBookInfo(BOOL IsDesc) sql ="Select * from tb_bookinfo ORDER BY PRICE ASC"; sql ="Select * from tb_bookinfo ORDER BY PRICE desc"; m_pRecord =m_pCon->Execute((_bstr_t)sql,NULL,adCmdText); while (!m_pRecord->ADOEOF && !m_pRecord->BOF) m_list.InsertItem(100,"");
fieldvalue =(TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("bookname")->Value; m_list.SetItemText(i,0,fieldvalue);
fieldvalue =(TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("barcode")->Value; m_list.SetItemText(i,1,fieldvalue);
fieldvalue=(TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("author")->Value; m_list.SetItemText(i,2,fieldvalue);
fieldvalue =(TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("bookconcern")->Value; m_list.SetItemText(i,3,fieldvalue);
fieldvalue=(TCHAR*)(_bstr_t)m_pRecord->GetFields()->GetItem("price")->Value; m_list.SetItemText(i,4,fieldvalue);
|