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,欢迎更正和 ...
随机推荐
- bat定时检测系统服务是否开启
@echo offrem 定义循环间隔时间和监测的服务:set secs=90set srvname="Apache2a" echo.echo ================== ...
- Codeforces Round #273 (Div. 2) D. Red-Green Towers 背包dp
D. Red-Green Towers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Ant入门
一.Ant介绍 Ant是Java的生成工具,是Apache的核心项目:直接在apache官网下载即可: Ant类似于Unix中的Make工具,都是用来编译.生成: Ant是跨平台的,而Make不能: ...
- ubuntu安装vmplayer出现问题的解决方法
ubuntu安装vmplayer 出现问题的解决方法 1:ubuntu12.04安装vmware12出现cannot ope dev/vmmon及modprobe vmmon提示密钥无效的解决办法 笔 ...
- audiojs 音频插件使用教程
audiojs 音频插件使用教程 github地址 https://kolber.github.io/audiojs/ 依赖文件 <script src="https://cdn.bo ...
- .netframe work的操作系统支持版本
https://msdn.microsoft.com/zh-cn/library/8z6watww.aspx
- substr 方法
substr 方法 返回一个从指定位置开始,并具有指定长度的子字符串. 参数 start 必选.所需的子字符串的起始位置.字符串中第一个字符的索引为 0. length 可选项.返回的子字符串中包含的 ...
- BW ON HANA 业务模型关系与数据取数
在接到业务需求之后,我认为重要的是理清楚自己该做什么.来实现业务.由于不了解业务,还是走了很多弯路.本可以不用这么做,还是这么做了.自然你最傻瓜的按照用户的方式去实现是没有问题的. 会使后面的人难以维 ...
- github 第一次使用及出现的问题解决
1.前言: 我们使用git,自然是希望我们的项目可以方便的从本地上传到git的仓库中,从而实现项目版本控制和备份,但是,从GitHub的网站上传文件,只能上传25MB的数据,我想多数人的项目都不可能只 ...
- 分布式_理论_03_2PC
一.前言 五.参考资料 1.分布式理论(三)—— 一致性协议之 2PC 2.分布式理论(三) - 2PC协议