AllowsTransparency and WebBrowser compatibility issue solutions

Source: Internet
Author: User

AllowsTransparency and System.Windows.Controls.WebBrowser compatibility issues, can read this article, so the reason is not much to say, the most fundamental is because the MS to Win32 bottom WebBrowser simple package into System.wind oWS. Controls.webbrowser

The solution is also very simple, but the online most of the articles are missing and the code is obsolete (various kinds of code errors, the error does not refer to the version and other reasons, but some simple punctuation; I don't believe he's a hand code, rather than a copy of the VS-written code into the article, this irresponsible person's article still need to see? ), so I sorted it into a class for easy invocation

Now all pay attention to the rapid development, if your article can not be a substantial help to others, even if not a good article, if your article not only did not help but waste other people's development time then there is no need to send the rubbish;

Because this article is relatively simple, so added a small amount of spit groove, in order to prevent the body discomfort nausea and vomiting, ignoring the above content, down the text to the positive turn

1. xaml:

<window x:class= "allowstransparency and WebBrowser compatibility issues. MainWindow" xmlns= "        Http://schemas.microsoft.com/winfx /2006/xaml/presentation "        xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "        xmlns:local=" Clr-namespace:allowstransparency and WebBrowser compatibility issues "        title=" MainWindow "height=" "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); or direct this            web. Webbrowser.navigate (New Uri ("http://www.baidu.com"));        }    }

3. Formwebbrowser Class Call:

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 = PLA            Cementtarget;             Window owner = Window.getwindow (Placementtarget); Window owner = MainWindow;            The page is passed in 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 = _ow Ner.        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; The 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 and WebBrowser compatibility issue solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.