user3680524
user3680524

Reputation:

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]/[-]/[+]?

Upvotes: 0

Views: 2058

Answers (1)

echo2121
echo2121

Reputation: 21

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.

Upvotes: 2

Related Questions