标签:
Duilib中的RichEdit控件在使用中发现,基本上对复制、粘贴、剪切等快捷方式都是支持的,不过唯一缺点是没有右键菜单,感觉不够好,于是就想着加上右键菜单。
右键菜单基本思路是,在RichEdit的消息处理函数中对鼠标的右键消息处理,发送一个自定义的Notify消息出来,主窗口中受到这个消息后弹出自己的右键菜单。
MessageHandler中修改原有代码
bool bWasHandled = true; if( (uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST) || uMsg == WM_SETCURSOR ) { if( !m_pTwh->IsCaptured() ) { switch (uMsg) { case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: case WM_RBUTTONDOWN: case WM_RBUTTONUP: { POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }; CControlUI* pHover = GetManager()->FindControl(pt); if(pHover != this) { bWasHandled = false; return 0; } if ( uMsg == WM_RBUTTONUP ) { GetManager()->SendNotify(this, DUI_MSGTYPE_RBTN_RICHEDIT); } } break; } }
这个Notify需要自己定义,消息一经发送出去了,初步处理那是客户端的事了。你需要自定义右键菜单就对这个消息处理。
在主窗口的Notify函数中,弹出右键菜单
void CMainWnd::Notify( TNotifyUI& msg ) { if ( _tcscmp(msg.sType, DUI_MSGTYPE_RBTN_RICHEDIT) == 0 ) { CRichEditUI* pRichEdit = static_cast<CRichEditUI*>(msg.pSender); if ( NULL == pRichEdit ) return ; CRichEditMenuWnd* pMenuWnd = new CRichEditMenuWnd(pRichEdit); pMenuWnd->Create(NULL, L"菜单", WS_POPUP, WS_EX_TOPMOST); POINT pt; GetCursorPos(&pt); pMenuWnd->MoveWindow(pt.x, pt.y, 104, 102, FALSE); pMenuWnd->ShowWindow(); return ; } WindowImplBase::Notify(msg); }
void CRichEditMenuWnd::OnClick( TNotifyUI& msg ) { if ( _tcscmp(msg.pSender->GetName(), _T("undo")) == 0 ) { m_pRichEdit->Undo(); } else if ( _tcscmp(msg.pSender->GetName(), _T("cut")) == 0 ) { m_pRichEdit->Cut(); } else if ( _tcscmp(msg.pSender->GetName(), _T("copy")) == 0 ) { m_pRichEdit->Copy(); } if ( _tcscmp(msg.pSender->GetName(), _T("paste")) == 0 ) { m_pRichEdit->Paste(); } Close(); }
Duilib中为RichEdit\Edit控件添加自定义右键菜单
标签:
原文地址:http://blog.csdn.net/mfcing/article/details/44706829