AllowsTransparency和WebBrowser兼容性问题解决方案
AllowsTransparency和System.Windows.Controls.WebBrowser兼容性问题,能看这篇文章,所以原因也不用多说;最根本的就是因为MS对win32底层的WebBrowser简易封装成System.Windows.Controls.WebBrowser
解决方案也很简单,但是网上的文章大部分都代码缺少和代码陈旧(各种转;各种代码错误,这种错误不是指版本等原因,而是一些简单标点符号;我不相信他是手扣代码,而不是把vs写好的代码复制到文章里,这种不负责任的人的文章还需要看么?),于是我就整理成了一个类方便调用
现在都讲究的快速开发,如果你的文章不能对别人起到实质性的帮助 就算不上一篇好文章,如果你的文章不但没起到帮助反而是浪费别人的开发时间 那么就没必要把垃圾发出来;
因本文章较为简单,故加入了微量吐槽,为防止身体不适者恶心干呕,忽视以上内容,下来文归正转
1、xaml:
<Window x:Class="AllowsTransparency和webbrowser兼容问题.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AllowsTransparency和webbrowser兼容问题"
Title="MainWindow" Height="350" Width="525" AllowsTransparency="True" WindowStyle="None">
<Grid x:Name="grid"></Grid>
</Window>
2、cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
FormsWebBrowser web = new FormsWebBrowser(this.grid); //或者直接this
web.WebBrowser.Navigate(new Uri("http://www.baidu.com"));
}
}
3、FormWebBrowser类调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Diagnostics;
using System.ComponentModel;
using System.Windows.Interop;
using System.Windows.Threading;
using System.Windows.Media;
using System.Runtime.InteropServices; namespace Tools
{
public class FormsWebBrowser
{
Window _owner;
FrameworkElement _placementTarget;
Form _form;
WebBrowser _wb = new WebBrowser(); public WebBrowser WebBrowser { get { return _wb; } } public FormsWebBrowser(FrameworkElement placementTarget) //, Window mainWindow)
{
_placementTarget = placementTarget;
Window owner = Window.GetWindow(placementTarget);
//Window owner = mainWindow; //page中传入window
Debug.Assert(owner != null);
_owner = owner; _form = new Form();
_form.Opacity = owner.Opacity;
_form.ShowInTaskbar = false;
_form.FormBorderStyle = FormBorderStyle.None;
_wb.Dock = DockStyle.Fill;
_form.Controls.Add(_wb); owner.LocationChanged += delegate { OnSizeLocationChanged(); };
_placementTarget.SizeChanged += delegate { OnSizeLocationChanged(); }; if (owner.IsVisible)
InitialShow();
else
owner.SourceInitialized += delegate
{
InitialShow();
}; DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(UIElement.OpacityProperty, typeof(Window));
dpd.AddValueChanged(owner, delegate { _form.Opacity = _owner.Opacity; }); _form.FormClosing += delegate { _owner.Close(); };
} void InitialShow()
{
NativeWindow owner = new NativeWindow();
owner.AssignHandle(((HwndSource)HwndSource.FromVisual(_owner)).Handle);
_form.Show(owner);
owner.ReleaseHandle();
} DispatcherOperation _repositionCallback; void OnSizeLocationChanged()
{
if (_repositionCallback == null)
_repositionCallback = _owner.Dispatcher.BeginInvoke(new Action(Reposition), DispatcherPriority.Input);
} void Reposition()
{
_repositionCallback = null; Point offset = _placementTarget.TranslatePoint(new Point(), _owner);
Point size = new Point(_placementTarget.ActualWidth, _placementTarget.ActualHeight);
HwndSource hwndSource = (HwndSource)HwndSource.FromVisual(_owner);
CompositionTarget ct = hwndSource.CompositionTarget;
offset = ct.TransformToDevice.Transform(offset);
size = ct.TransformToDevice.Transform(size); Win32.POINT screenLocation = new Win32.POINT(offset);
Win32.ClientToScreen(hwndSource.Handle, ref screenLocation);
Win32.POINT screenSize = new Win32.POINT(size); Win32.MoveWindow(_form.Handle, screenLocation.X, screenLocation.Y, screenSize.X, screenSize.Y, true);
}
} static class Win32
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y; public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
public POINT(Point pt)
{
X = Convert.ToInt32(pt.X);
Y = Convert.ToInt32(pt.Y);
}
}; [DllImport("user32.dll")]
internal static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint); [DllImport("user32.dll")]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); }
}
源码:http://files.cnblogs.com/NotAnEmpty/wpf_WebBorwser.rar
AllowsTransparency和WebBrowser兼容性问题解决方案的更多相关文章
- Xilinx的ISE14.7和PlanAhead与win10系统的兼容性问题解决方案
Xilinx的ISE14.7和PlanAhead与win10系统的兼容性问题解决方案 2018年07月03日 18:27:57 feq123 阅读数:4495 今天在新电脑的win10系统上安装I ...
- JS组件系列——Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案
前言:最近项目里面需要用到表格的冻结列功能,所谓“冻结列”,就是某些情况下表格的列比较多,需要固定前面的几列,后面的列滚动.遗憾的是,bootstrap table里自带的fixed column功能 ...
- JS中用execCommand("SaveAs")保存页面兼容性问题解决方案
开发环境:ASP.NET MVC,其他环境仅供参考. 问题描述:在开发中遇到这样的需求,保存页面,通常使用JavaScript的saveAs进行保存,各浏览器对saveAs支持,见下表. 代码一:初始 ...
- CSS Flexible 布局兼容性以及解决方案
1.兼容性 https://caniuse.com/#feat=flexbox IE11以及以下不支持. 2.解决方案 flexibility库 https://github.com/jonathan ...
- IE浏览器兼容性问题解决方案
一.CSS常见问题 1.H5标签兼容性 解决方案:<script src="http://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.j ...
- extjs 兼容性问题解决方案
首先明确一点,extjs是没有所谓的兼容性的问题的.那为什么总是出现不同浏览器兼容性的问题呢?而且很多人把这作为了extjs一个缺点. 解决方法,看看写的代码是不是多了 英文逗号 , 或 中文的逗号 ...
- Selenium2学习-024-WebUI自动化实战实例-022-网站不同分辨率下页面样式展示兼容性问题解决方案 -- 设置浏览器显示区域大小(无人值守,节约测试成本的福音,BOSS 最爱)
在 Web UI 自动化测试的过程中,通常会测试页面在不同分辨率下的显示效果,即在不同大小的显示器上全屏浏览器后的页面展示,此种测试方法需要购置不同大小的显示器,或者频繁的设置屏幕分辨率,不仅浪费了大 ...
- IE兼容性问题解决方案1--ajax请求不发送到后台
相信很多小伙伴会遇到这种问题,用ajax做异步请求的时候,在IE浏览器下,并没有发送出去.但是相关程序确实执行了.为什么呢? 原来这是IE缓存方式的原因,所以呢,用下边的解决方案吧. 1.在请求的UR ...
- (转)没有IE就没有伤害!浏览器兼容性问题解决方案汇总
普及:浏览器的兼容性问题,往往是个别浏览器(没错,就是那个与众不同的浏览器)对于一些标准的定义不一致导致的.俗话说:没有IE就没有伤害. 贴士:内容都是自己总结的,不免会出现错误或者bug,欢迎更正和 ...
随机推荐
- 初识Spring security-添加security
请先查看 初识Spring security-无Security的SpringMVC 在pom.xml文件中添加包 <!-- Spring Security --> <depende ...
- MySQL数据库的安装以及配置过程
首先,下载 MySQL 数据库的安装包:mysql-5.7.17.msi.(百度一下自己下吧) 然后点击这里,啊哈哈哈哈哈哈哈. ----------------------------------- ...
- ubuntu12.04 错误:phonon: No such file or directory; phonon模块的安装
http://blog.csdn.net/xsl1990/article/details/9009919 错误:phonon: No such file or directory 1)sudo ap ...
- javaScript中的DOM补充
一.DOM树 二.DOM节点 DOM 是这样规定的: 整个文档是一个文档节点 每个 HTML 标签是一个元素节点 包含在 HTML 元素中的文本是文本节点 每一个 HTM ...
- 国内的Git比GitHub快
GitHub的速度简直受不了! 被微软收购之后就堕落了! 用Gitee也挺好的,学习用吧!
- nova libvirt event
nova中利用libvirt 事件来更新vm的DB中的power状态 https://wiki.openstack.org/wiki/ComputeDriverEvents Nova compute ...
- 经典分水岭算法的 C++ 实现
这个程序是研一下学期的计算机视觉课程大作业,完成于 2013/06/16,是对 Soille 和 Vincent(1991)提出的模拟浸没的分水岭算法的实现,详见下面的报告. 源码托管在 Github ...
- 解决:cmd中运行monkeyrunner monkey_recorder.py报错: Can't open specified script file
看lynnLi的博客monkeyrunner之录制与回放(七),遇到了一个问题,我在cmd中输入monkeyrunner monkey_recorder.py,却报错了: 当时第一个感觉时,先到\sd ...
- RabbitMQ学习系列三:.net 环境下 C#代码订阅 RabbitMQ 消息并处理
上一篇已经讲了Rabbitmq如何在Windows平台安装 不懂请移步: RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列 一.理论 .net环境下,C#代码订阅 ...
- 分布式_理论_04_ 3PC
一.前言 五.参考资料 1.分布式理论(四)—— 一致性协议之 3PC 2.分布式理论(四) - 3PC协议 3.