禁用C#WebBrowser控件的缩放

时间:2014-06-08 11:36:49

标签: c# winforms browser controls webbrowser-control

我的C#桌面应用程序具有加载第三方网页的WebBrowser控件。我可以做些什么来阻止用户使用Ctrl+MousewheelCtrl+[0]/[-]/[+]缩放网页?

1 个答案:

答案 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