在C#中 webbrowser的使用心得
时间:2023-12-10 12:57:00
1.首先是屏蔽浏览器右键菜单的问题,
用以下代码可以让浏览器用自己的右键菜单:
tempBrowser.ContextMenuStrip = this.contextMenuStrip1;
tempBrowser.IsWebBrowserContextMenuEnabled = false;
但是很不幸,上面的代码在有的机器上不起作用,开始以为是环境或者流氓插件的问题,折磨了很久无果,后来把.net升级到4.0竟然解决了这个问题,估计就是微软webbrowser控件的问题
2.屏蔽拷贝快捷键和截屏快捷键
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 explorer_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("当前系统禁止内容复制,如需系统内部复制粘贴,请使用右键菜单的功能!", "屏蔽");
Clipboard.SetDataObject("null");
return true;
}
}
}
return false;
}
3.解决webbrowser遇到window.open无法打开页面的问题
是因为打开页面的url一般是通过myBrowser.StatusText来获取的,但是window.open是无法获取真正的url的,解决办法就是在webbrowser的NewWindow事件 * 殊处理window.open的事件,获取真正的url地址
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;
很遗憾这种方法只能适合一页中只有一个window.open的情况,如果有多个window.open,我们可以用下面的方法:在webbrowser的DocumentCompleted中,if (mybrowser.DocumentText.IndexOf("window.open(") > -1),然后把所有window.open替换为window.location.href。但是后来发现系统执行过这个过程之后,webbrowser的url会变成原来的url,不知道有没有朋友能知道为什么。
4.屏蔽webbrowser拖拽网页内容到外部word等的问题
private void tempBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser tempBrowser = (WebBrowser)sender;
tempBrowser.Parent.Text = tempBrowser.DocumentTitle;
HtmlElement ele = tempBrowser.Document.CreateElement("script");//新增
ele.SetAttribute("type", "text/javascript");
ele.SetAttribute("text", "document.body.ondragstart=function(){window.event.returnValue=false;};");
tempBrowser.Document.Body.AppendChild(ele);
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java解析json数组方式
![](https://img.aspxhome.com/file/2023/8/58188_0s.png)
feign实现传递参数的三种方式小结
![](https://img.aspxhome.com/file/2023/0/62740_0s.jpg)
SpringBoot jackson提供对LocalDate的支持方式
深入探索Java常量池
![](https://img.aspxhome.com/file/2023/0/66470_0s.jpg)
Android全屏设置的方法总结
Java web项目中的强制登录功能实现代码
C# WinForm制作登录界面的实现步骤
![](https://img.aspxhome.com/file/2023/7/68607_0s.png)
C#中this指针的用法示例
Spring Native项目实战(体验79毫秒启动springboot应用)
![](https://img.aspxhome.com/file/2023/1/78361_0s.png)
Android Activity的启动过程源码解析
![](https://img.aspxhome.com/file/2023/4/138514_0s.png)
一篇文章带你入门Java之编程规范
![](https://img.aspxhome.com/file/2023/7/132087_0s.png)
C#中按引用传递与按值传递的区别,以及ref与out关键字的用法详解
springboot配置加密的正确姿势分享
![](https://img.aspxhome.com/file/2023/7/77157_0s.jpg)
Java中利用gson解析Json实例教程
![](https://img.aspxhome.com/file/2023/1/70511_0s.png)
C#中static静态变量的用法实例
Flutter 自定义Drawer 滑出位置的大小实例代码详解
![](https://img.aspxhome.com/file/2023/8/138478_0s.jpg)
两个surfaceView实现切换效果
springboot整合JSR303参数校验与全局异常处理的方法
![](https://img.aspxhome.com/file/2023/5/61565_0s.png)
基于Flutter实现多边形和多角星组件
![](https://img.aspxhome.com/file/2023/7/84747_0s.png)
深入了解Java虚拟机栈以及内存模型
![](https://img.aspxhome.com/file/2023/0/112420_0s.jpg)