AllowsTransparency and WebBrowser compatibility solution, allowstransparency
AllowsTransparency and System. windows. controls. for WebBrowser compatibility issues, I can refer to this article, so I don't need to talk about the reason. The most fundamental reason is that MS encapsulates the underlying WebBrowser of win32 into System. windows. controls. webBrowser
The solution is also very simple, but most of the online articles are lack of code and outdated code (various transformations; various code errors, such errors do not refer to versions and other reasons, but some simple punctuation marks; I don't believe that he is a code developer, rather than copying the code written by vs to an article. Do I still need to read this irresponsible article ?), So I organized it into a class for convenient calling.
Now, we are all very careful about fast development. If your article cannot provide substantial help to others, even if it is not a good article, if your article does not help, but is a waste of others' development time, there is no need to spam the article;
Because this article is relatively simple, we have added a trace of vomit. In order to prevent physical discomfort from nausea, Retching, and ignoring the above content, we will go back and forth.
1. xaml:
<Window x: Class = "AllowsTransparency and webbrowser compatibility. 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 "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); // or directly 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 _ placementTarg Et; Form _ form; WebBrowser _ wb = new WebBrowser (); public WebBrowser {get {return _ wb ;}} public FormsWebBrowser (FrameworkElement placementTarget) //, Window mainWindow) {_ placementTarget = placementTarget; Window owner = Window. getWindow (placementTarget); // Window owner = mainWindow; // input window Debug in page. 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 );}}