標籤:des style blog http io ar os sp for
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); }}
AllowsTransparency和WebBrowser相容性問題解決方案