How to Use webbrowser in C #

Source: Internet
Author: User

1. The first problem is to shield the right-click menu of the browser,
Use the following code to allow the browser to use its own right-click menu:
TempBrowser. ContextMenuStrip = this. contextMenuStrip1;
TempBrowser. IsWebBrowserContextMenuEnabled = false;

However, unfortunately, the above Code does not work on some machines and started to think that it is an environment or rogue plug-in problem. It has been suffering for a long time and failed. the net upgrade to 4.0 solved this problem, probably because of the Microsoft webbrowser control.

2. Disable copy and screenshot shortcuts

Public bool PreFilterMessage (ref Message msg)
{
If (msg. Msg = WM_KEYDOWN | msg. Msg = WM_KEYUP)
{
StringBuilder sb = new StringBuilder (50 );
GetClassName (msg. HWnd, sb, 50 );

If (sb. ToString (). ToLower () = "internet assumer_server ")
{
If (Control. modifierKeys = Keys. control & (Keys) msg. WParam. toInt32 () & Keys. keyCode) = Keys. c) | (Keys) msg. WParam. toInt32 () & Keys. keyCode) = Keys. printScreen | (Keys) msg. WParam. toInt32 () & Keys. keyCode) = Keys. menu)
{
MessageBox. Show ("content copying is forbidden in the current system. To copy and paste content in the system, right-click the menu! "," Blocked ");
Clipboard. SetDataObject ("null ");
Return true;
}
}
}

Return false;
}

3. Solve webbrowser's problem of window. open being unable to open the page
The reason is that the url to open the page is usually through myBrowser. statusText, but window. open cannot obtain the real url. The solution is to specially process the window in the NewWindow event of webbrowser. open event to get the real url address
WebBrowser myBrowser = (WebBrowser) sender;
TabPage mypage = (TabPage) myBrowser. Parent;
String NewURL = "";
String html = myBrowser. Document. ActiveElement. OuterHtml;
String pattern = @ "<button .*? Onclick = .*\('(.*)'\).*";
MatchCollection matches = Regex. Matches (html, pattern, RegexOptions. IgnoreCase );
If (matches. Count = 1)
{
Match m = matches [0];
Group g = m. Groups [1];
If (g! = Null & g. Length> 0)
{
String address = myBrowser. Url. Scheme + ": //" + myBrowser. Url. Host + ":" + myBrowser. Url. Port + g. ToString ();
NewURL = address. Replace ("&","&");
}
}
NewURL = string. IsNullOrEmpty (NewURL )? MyBrowser. StatusText: NewURL;

Unfortunately, this method is only applicable to a page with only one window. open, if there are multiple windows. open, we can use the following method: In DocumentCompleted of webbrowser, if (mybrowser. documentText. indexOf ("window. open (")>-1), and then put all windows. replace open with window. location. href. However, after the system executes this process, the webbrowser url is changed to the original url.

4. Shield webbrowser from dragging webpage content to external word
Private void tempBrowser_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser tempBrowser = (WebBrowser) sender;
TempBrowser. Parent. Text = tempBrowser. DocumentTitle;
HtmlElement ele = tempBrowser. Document. CreateElement ("script"); // Add
Ele. SetAttribute ("type", "text/javascript ");
Ele. SetAttribute ("text", "document. body. ondragstart = function () {window. event. returnValue = false ;};");
TempBrowser. Document. Body. AppendChild (ele );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.