浏览器的定制与扩展
2006-07-22 22:56:33 来源:WEB开发网enum CONTEXT_MENU_MODE // 上下文菜单
通过CLhpHtmlView的函数SetContextMenuMode来设置右键菜单的类型。如果设定的右键弹出菜单是“自定义菜单”类型, 我们只要在CLhpHtmlView的派生类中重载OnShowCustomContextMenu虚函数即可,如下代码 CDemoView是CLhpHtmlView的派生类
{
NoContextMenu, // 无菜单
DefaultMenu, // 默认菜单
TextSelectionOnly, // 仅文本选择菜单
CustomMenu // 自定义菜单
};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;
}
更多精彩
赞助商链接