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兼容性问题解决方案的更多相关文章

  1. Xilinx的ISE14.7和PlanAhead与win10系统的兼容性问题解决方案

    Xilinx的ISE14.7和PlanAhead与win10系统的兼容性问题解决方案 2018年07月03日 18:27:57 feq123 阅读数:4495   今天在新电脑的win10系统上安装I ...

  2. JS组件系列——Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

    前言:最近项目里面需要用到表格的冻结列功能,所谓“冻结列”,就是某些情况下表格的列比较多,需要固定前面的几列,后面的列滚动.遗憾的是,bootstrap table里自带的fixed column功能 ...

  3. JS中用execCommand("SaveAs")保存页面兼容性问题解决方案

    开发环境:ASP.NET MVC,其他环境仅供参考. 问题描述:在开发中遇到这样的需求,保存页面,通常使用JavaScript的saveAs进行保存,各浏览器对saveAs支持,见下表. 代码一:初始 ...

  4. CSS Flexible 布局兼容性以及解决方案

    1.兼容性 https://caniuse.com/#feat=flexbox IE11以及以下不支持. 2.解决方案 flexibility库 https://github.com/jonathan ...

  5. IE浏览器兼容性问题解决方案

    一.CSS常见问题 1.H5标签兼容性 解决方案:<script src="http://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.j ...

  6. extjs 兼容性问题解决方案

    首先明确一点,extjs是没有所谓的兼容性的问题的.那为什么总是出现不同浏览器兼容性的问题呢?而且很多人把这作为了extjs一个缺点. 解决方法,看看写的代码是不是多了  英文逗号 , 或 中文的逗号 ...

  7. Selenium2学习-024-WebUI自动化实战实例-022-网站不同分辨率下页面样式展示兼容性问题解决方案 -- 设置浏览器显示区域大小(无人值守,节约测试成本的福音,BOSS 最爱)

    在 Web UI 自动化测试的过程中,通常会测试页面在不同分辨率下的显示效果,即在不同大小的显示器上全屏浏览器后的页面展示,此种测试方法需要购置不同大小的显示器,或者频繁的设置屏幕分辨率,不仅浪费了大 ...

  8. IE兼容性问题解决方案1--ajax请求不发送到后台

    相信很多小伙伴会遇到这种问题,用ajax做异步请求的时候,在IE浏览器下,并没有发送出去.但是相关程序确实执行了.为什么呢? 原来这是IE缓存方式的原因,所以呢,用下边的解决方案吧. 1.在请求的UR ...

  9. (转)没有IE就没有伤害!浏览器兼容性问题解决方案汇总

    普及:浏览器的兼容性问题,往往是个别浏览器(没错,就是那个与众不同的浏览器)对于一些标准的定义不一致导致的.俗话说:没有IE就没有伤害. 贴士:内容都是自己总结的,不免会出现错误或者bug,欢迎更正和 ...

随机推荐

  1. XXL-Job分布式任务调度

    分布式情况下定时任务会出现哪些问题? 分布式集群的情况下,怎么保证定时任务不被重复执行 分布式定时任务解决方案 ①使用zookeeper实现分布式锁 缺点(需要创建临时节点.和事件通知不易于扩展) ② ...

  2. 【codevs1907】方格取数3(最大流最小割定理)

    网址:http://codevs.cn/problem/1907/ 题意:在一个矩阵里选不相邻的若干个数,使这些数的和最大. 我们可以把它看成一个最小割,答案就是矩阵中的所有数-最小割.先把矩阵按国际 ...

  3. 基于主主复制的mysql双机热备+keepalived实现高可用性

  4. 利用Xshell从windows上传文件到linux

    1.首先,打开你的xshell客户端. 2.我用的是ubuntu 所以用 apt-get install lrzsz 命令来安装这个上传软件. 安装成功以后,可以使用rz上传,sz下载. 然后等待上传 ...

  5. Qt 自定义PushButton

    http://blog.csdn.net/zddblog/article/details/11116191 功能:鼠标弹起并在按键区域内时,按键响应.并实现normal.hover.pressed效果 ...

  6. Spring初学之使用外部配置文件dataSource

    一.在Spring的基础上还要另外导入c3p0包和mysql的驱动包. 二.配置文件, jdbc.propertices:这里只做了一些简单配置 user=root password=123 driv ...

  7. AVFoundation之录音及播放

    录音 在开始录音前,要把会话方式设置成AVAudioSessionCategoryPlayAndRecord //设置为播放和录音状态,以便可以在录制完之后播放录音 AVAudioSession *s ...

  8. idea更换git地址操作

    更换地址: git remote set-url origin XXXXXXXXXXXXXXX 查看远程地址: git remote -v

  9. filter()和sort()这两个方法一块学习,案例中。

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 搞定thrift双向消息

    thrift作为脱胎于facebook的rpc框架,各方面都非常优秀.清晰的分层设计,多语言的支持,以及不输protocolbuffer的效率(compact下优于protocolbuffer),都让 ...