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. LCN协调者服务集群

    官方文档: https://github.com/codingapi/tx-lcn/wiki/TxManager%E9%9B%86%E7%BE%A4%E8%AF%B4%E6%98%8E 核心原理 通过 ...

  2. 【P2629】好消息,坏消息(前缀和+单调队列优化DP)

    一激动一颓就会忘了总结... 前面的大黄题就不总结了. 这个题我只想说一声艹,一开始的思路就是正确的,然后计算的时候有了一个瑕疵,不过很快也就改过来了.然后却一直连样例都过不了.仔仔细细看了老半天,经 ...

  3. 【bzoj5055】膜法师(离散化+树状数组)

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=5055 这道题……不得不说,从标题到题面都能看出一股浓浓的膜法气息……苟…… 题意就是统计顺序 ...

  4. linux基础(10)-导航菜单

    导航菜单实战 例:编写一个shell脚本,包含多个菜单,其中需要一个退出选项:可单选也可多选:根据序号选择后,显示所选菜单名称. #!/bin/bash ####################### ...

  5. 防域名DNS劫持 从保护帐号安全做起

    什么攻击能造成区域性的网络瘫痪?没错,DNS劫持.这个堪称核武器的攻击方式,一旦爆炸,后果不堪设想.2014年1月21日,全国大范围出现DNS故障,下午,中国顶级域名根服务器出现故障,大部分网站受影响 ...

  6. adb 解说

    ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 先说安装方法, 电脑上需要安装客户端. 客户端包含在sdk里. 设备上不需要安装, 只需要在手机 ...

  7. 区间dp的感悟

    学区间dp似乎也很久了...对区间dp的通用模型都了解了一些 但是做题还是很坑 上了一点难度的题基本想不出什么思路.. 目前的做题方式就是看题 想一会发现自己不会做 看题解 好巧妙啊 理解后写一发.. ...

  8. Ubuntu linux背景指南:在开始之前需要知道哪些东西

    1.摘要 Ubuntu是一个新的GNU/Linux衍生操作系统,其目标是更多地以用户为本以及桌面应用. 因此,Ubuntu的目的是消除安装的困难,在很大程度上靠自动配置和自动探测硬件解决 问题,无须用 ...

  9. ComboBoxStyle和ToggleButton

    <Style x:Key="ComboBoxStyle" TargetType="{x:Type ComboBox}">   <Setter ...

  10. uva11922splay

    题意:一个值1到n的数组,一种(多次)操作把l到r的区间反转,然后放到数组尾部 题解:裸的splay,用区间合并和区间分割,反转用lazy标记+pushdown就好了 #include<bits ...