简体   繁体   中英

Disable zooming in C# WebBrowser control

I have C# desktop app that has a WebBrowser control that loads a third-party webpage. Is there anything I can do to prevent the user from zooming the webpage with Ctrl+Mousewheel or Ctrl+[0]/[-]/[+] ?

Add this to to your documentCompleted event:

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;

Then add these two functions:

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;
    }
}

and

static bool docEvents_onmousewheel(mshtml.IHTMLEventObj pEvtObj)
{
    if (pEvtObj.ctrlKey)
    {
        pEvtObj.cancelBubble = true;
        pEvtObj.returnValue = false;
        return false;
    }
    return true;
}

For Body_KeyDown you will need to search up the KeyPressedCode for the 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM