扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
如何使自定义的控制站点来替换默认的控制站点呢?在MFC7.0中只需重载CHtmlView的虚函数CreateControlSite即可:
BOOL CLhpHtmlView::CreateControlSite(COleControlContainer * pContainer,COleControlSite ** ppSite, UINT /*nID*/, REFCLSID /*clsid*/){ *ppSite = new CDocHostSite(pContainer, this);
HRESULT CDocHostSite::XDocHostUIHandler::ShowContextMenu(DWORD dwID, POINT * ppt, IUnknown * pcmdtReserved, IDispatch * pdispReserved) { METHOD_PROLOGUE(CDocHostSite, DocHostUIHandler); return pThis->m_pView->OnShowContextMenu( dwID, ppt, pcmdtReserved,pdispReserved ); } HRESULT CLhpHtmlView::OnShowContextMenu(DWORD dwID, LPPOINT ppt, LPUNKNOWN pcmdtReserved, LPDISPATCH pdispReserved) { HRESULT result = S_FALSE; switch(m_ContextMenuMode) { case NoContextMenu: // 无菜单 result=S_OK; break; case DefaultMenu: // 默认菜单 break; case TextSelectionOnly: // 仅文本选择菜单 if(!(dwID == CONTEXT_MENU_TEXTSELECT || dwID == CONTEXT_MENU_CONTROL)) result=S_OK; break; case CustomMenu: // 自定义菜单 if(dwID!=CONTEXT_MENU_TEXTSELECT) result=OnShowCustomContextMenu(ppt,pcmdtReserved,pdispReserved); break; } return result; }在CLhpHtmlView中定义的枚举类型CONTEXT_MENU_MODE举出了定制右键弹出菜单的四种类型
enum CONTEXT_MENU_MODE // 上下文菜单 { NoContextMenu, // 无菜单 DefaultMenu, // 默认菜单 TextSelectionOnly, // 仅文本选择菜单 CustomMenu // 自定义菜单 };通过CLhpHtmlView的函数SetContextMenuMode来设置右键菜单的类型。如果设定的右键弹出菜单是“自定义菜单”类型, 我们只要在CLhpHtmlView的派生类中重载OnShowCustomContextMenu虚函数即可,如下代码 CDemoView是CLhpHtmlView的派生类
HRESULT CDemoView::OnShowCustomContextMenu(LPPOINT ppt, LPUNKNOWN pcmdtReserved,LPDISPATCH pdispReserved) { if ((ppt==NULL)||(pcmdtReserved==NULL)||(pcmdtReserved==NULL)) return S_OK; HRESULT hr=0; IOleWindow *oleWnd=NULL; hr=pcmdtReserved->QueryInterface(IID_IOleWindow, (void**)&oleWnd); if((hr != S_OK)||(oleWnd == NULL)) return S_OK; HWND hwnd=NULL; hr=oleWnd->GetWindow(&hwnd); if((hr!=S_OK)||(hwnd==NULL)) { oleWnd->Release(); return S_OK; } IHTMLElementPtr pElem=NULL; hr = pdispReserved->QueryInterface(IID_IHTMLElement, (void**)&pElem); if(hr != S_OK) { oleWnd->Release(); return S_OK; } IHTMLElementPtr pParentElem=NULL; _bstr_t tagID; BOOL go=TRUE; pElem->get_id(&tagID.GetBSTR()); while(go && tagID.length()==0) { hr=pElem->get_parentElement(&pParentElem); if(hr==S_OK && pParentElem!=NULL) { pElem->Release(); pElem=pParentElem; pElem->get_id(&tagID.GetBSTR()); } else go=FALSE; }; if(tagID.length()==0) tagID="no id"; CMenu Menu,SubMenu; Menu.CreatePopupMenu(); CString strTagID = ToStr(tagID); if(strTagID == "red") Menu.AppendMenu(MF_BYPOSITION, ID_RED, "您点击的是红色"); else if(strTagID == "green") Menu.AppendMenu(MF_BYPOSITION, ID_GREEN, "您点击的是绿色"); else if(strTagID == "blue") Menu.AppendMenu(MF_BYPOSITION, ID_BLUE, "您点击的是蓝色"); else Menu.AppendMenu(MF_BYPOSITION, ID_NONE, "你点了也白点,请在指定的地方点击"); int MenuID=Menu.TrackPopupMenu(TPM_RETURNCMD|TPM_LEFTALIGN|TPM_RIGHTBUTTON,ppt->x, ppt->y, this); switch(MenuID) { case ID_RED: MessageBox("红色"); break; case ID_GREEN: MessageBox("红色"); break; case ID_BLUE: MessageBox("红色"); break; case ID_NONE: MessageBox("haha"); break; } oleWnd->Release(); pElem->Release(); return S_OK; }
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者