我的C#桌面应用程序具有加载第三方网页的WebBrowser
控件。我可以做些什么来阻止用户使用Ctrl+Mousewheel
或Ctrl+[0]/[-]/[+]
缩放网页?
答案 0 :(得分:2)
将此添加到您的documentCompleted
活动:
webBrowser1.Document.Body.KeyDown -= Body_KeyDown;
webBrowser1.Document.Body.KeyDown += Body_KeyDown;
var docEvents = (mshtml.HTMLDocumentEvents2_Event)webBrowser1.Document.DomDocument;
docEvents.onmousewheel -= docEvents_onmousewheel;
docEvents.onmousewheel += docEvents_onmousewheel;
然后添加这两个函数:
static void Body_KeyDown(object sender, HtmlElementEventArgs e)
{
if ((e.KeyPressedCode == 109 && e.CtrlKeyPressed) ||
(e.KeyPressedCode == 107 && e.CtrlKeyPressed) ||
(e.CtrlKeyPressed && e.KeyPressedCode == 187) ||
(e.CtrlKeyPressed && e.KeyPressedCode == 189))
{
e.ReturnValue = false;
}
}
和
static bool docEvents_onmousewheel(mshtml.IHTMLEventObj pEvtObj)
{
if (pEvtObj.ctrlKey)
{
pEvtObj.cancelBubble = true;
pEvtObj.returnValue = false;
return false;
}
return true;
}
对于Body_KeyDown
,您需要向KeyPressedCode
搜索0
。