自定义CEdit右键菜单,并响应右键菜单命令
2010-06-02 16:41
295 查看
首先创建一个类,这里命名为CEditBox,其派生自CEdit。然后在资源中创建一个Menu,我这里命名其ID为IDR_VOL_VALUE。
在CEditBox的OnContextMenu中添加自定义右键菜单:
我在CTestView中使用了CEditBox类,CTestView派生自CFormView,右键菜单命令也是在CTestView中进行响应。这里有个问题,由于我在CTestView中使用了不止一个CEditBox控件,我们在命令消息中要知道是在哪个CEditBox上面点击了右键。我采取如下措施:
1、在CTestView中定义了一个CEditBox指针
m_pCurEdit用来保存我们的命令响应函数是针对的哪个控件。
2、在在CEditBox的OnContextMenu中,TrackPopupMenu之前得到其父窗口的指针pView,然后把this指针赋给pView->m_pCurEdit。代码如下:
3、消息响应函数在CEditBox的父窗口即CTestView中实现,代码如下:
在CEditBox的OnContextMenu中添加自定义右键菜单:
void CEditBox::OnContextMenu(CWnd* pWnd, CPoint point) { if (point.x == -1 && point.y == -1) //SHIFT+F10 { //keystroke invocation CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu menu; VERIFY(menu.LoadMenu(IDR_VOL_VALUE)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); CTestView *pView = (CTestView*)(GetParent()); pView->m_pCurEdit = this; pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pView); }
我在CTestView中使用了CEditBox类,CTestView派生自CFormView,右键菜单命令也是在CTestView中进行响应。这里有个问题,由于我在CTestView中使用了不止一个CEditBox控件,我们在命令消息中要知道是在哪个CEditBox上面点击了右键。我采取如下措施:
1、在CTestView中定义了一个CEditBox指针
CEditBox *m_pCurEdit;
m_pCurEdit用来保存我们的命令响应函数是针对的哪个控件。
2、在在CEditBox的OnContextMenu中,TrackPopupMenu之前得到其父窗口的指针pView,然后把this指针赋给pView->m_pCurEdit。代码如下:
CTestView *pView = (CTestView*)(GetParent()); pView->m_pCurEdit = this;
3、消息响应函数在CEditBox的父窗口即CTestView中实现,代码如下:
//电压等幅值 void CTestView::OnVolEqual() { // TODO: Add your command handler code here CString str; if(m_pCurEdit == &m_valueUa) { m_valueUa.GetWindowText(str); m_valueUb.SetWindowText(str); m_valueUc.SetWindowText(str); m_valueUz.SetWindowText(str); } if(m_pCurEdit == &m_valueUb) { m_valueUb.GetWindowText(str); m_valueUa.SetWindowText(str); m_valueUc.SetWindowText(str); m_valueUz.SetWindowText(str); } if(m_pCurEdit == &m_valueUc) { m_valueUc.GetWindowText(str); m_valueUa.SetWindowText(str); m_valueUb.SetWindowText(str); m_valueUz.SetWindowText(str); } if(m_pCurEdit == &m_valueUz) { m_valueUz.GetWindowText(str); m_valueUa.SetWindowText(str); m_valueUb.SetWindowText(str); m_valueUc.SetWindowText(str); } m_pCurEdit = NULL; } //额定相电压 void CTestView::OnVolRating() { // TODO: Add your command handler code here if(m_pCurEdit == NULL) return; m_pCurEdit->SetWindowText("100.0"); } //电压零 void CTestView::OnVolZero() { // TODO: Add your command handler code here if(m_pCurEdit == NULL) return; m_pCurEdit->SetWindowText("0.0"); }
相关文章推荐
- MFC学习笔记.day03-菜单,图形标记菜单创建,菜单栏禁用,命令响应,右键弹出菜单,添加菜单
- 在浏览器右键添加自定义菜单
- 右键菜单的响应问题
- javascript自定义右键菜单并屏蔽默认右键菜单【原创】
- 关于如何为FormView中的Richedit/Edit添加右键菜单,并响应CTRL+C等快捷键
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- jQuery自定义右键菜单
- jxbrowser 实现自定义右键菜单 jxbrowser破解版请联系作者
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- QT右键菜单、右键下拉菜单、各控件响应右键菜单方法简介 【百度空间】
- Flex4 自定义右键菜单
- 对任意元素,自定义右键菜单
- 怎么修改cedit控件的右键菜单
- TreeView的自定义右键菜单
- 给Windows右键菜单添加类似Linux的"tail -f"命令
- js自定义鼠标右键菜单
- JS实现自定义右键菜单
- 在右键菜单中添加“用记事本打开”命令
- 在浏览器中添加自定义右键菜单c#
- Adobe SVG Viewer 6.0 中自定义右键菜单