AllowsTransparency和WebBrowser相容性問題解決方案

來源:互聯網
上載者:User

標籤: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相容性問題解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.