2
votes

It seems like it's impossible to disable printing in a webBrowser control.

Here's what I've tried:

  • Setting WebBrowserShortcutsEnabled to FALSE

    • This still shows a print dialog when pressing ctrl+p. The behavior seems strange, but from my testing, you can trigger the dialog while the webbrowser control still has focus. If you click on another control so the webbrowser loses focus, and click on the webbrowser again, you are unable to trigger the print dialog.
  • Overriding Ctrl+P in the Form's KeyDown event

    • This doesn't seem to do anything at all.
  • Using Webbrowser's PreviewKeyDown

    • This doesn't seem to be intended to intercept keys, merely preview them as you can't cancel out keypresses with it.

It would be optimal if ALL keyboard shortcuts are disabled.

There must be some way to achieve this? Thanks.

3

3 Answers

1
votes

working example..

$(document).ready(function(e){
    $('body').keydown(function(event) {
       // alert('this');
        if (event.which == 80 && event.ctrlKey) {
            return false;
             //alert('me');   
        }
    });
});

http://jsfiddle.net/rhPxU/

1
votes

You can try disable printing through CSS.

<style type="text/css" media="print">
 .noprint { display:none }
 </style>


<body>
 <div> This can print</div> 
<div class="noprint"> This can't print</div> 
<div> This can print</div> 
</body> 
1
votes

The cleanest way is probably to use the TranslateAccelerator method of IDocHostUIHandler. On the other hand it is quite a bunch of code and the need of dealing with HtmlInterop classes.

the code then can look as follows:

int IDocHostUIHandler.TranslateAccelerator(ref tagMSG msg, ref Guid group, uint nCmdID)
{
  KeyEventArgs e = new KeyEventArgs((Keys)((int)msg.wParam & 0xff) | Control.ModifierKeys);
  if (e.Modifiers == Keys.Control && e.KeyCode == Keys.P )
    return 0;
  return 1;
}

The DocHostUIHandler can be attached to the document in the DocumentCompleted event: