Problem Overview:
1. Add WebBrowser in XAML (whether it's a control in WPF or a control in WinForm)
2. Set window background= "Transparent" allowstransparency= "True"
Result: Content in Webbroser will not be visible.
Problem Analysis:
I was a WPF rookie, not at all. WPF is a well-known issue. The reason is simple:
1. The so-called WebBrowser control in WPF is in the WPF encapsulation of Ms WebBrowser, and its essence and WebBrowser in WinForm are simply to add some simple DependencyProperty, Enables it to be configured in XAML, and its use looks consistent with other WPF controls. Its kernel is still based on Win32.
2. Understand the essence of the WebBrowser control in Wpf/winform, we enter the topic, it is well known that WPF is respected for its epoch-making graphics rendering technology, do not do this. We understand that WPF uses vector technology to eliminate aliasing during page scaling. Therefore, WPF is implemented in resolution-independent. But Win32 's rendering techniques are not based on vector technology. Therefore, the intrinsic rendering of WebBrowser is fundamentally different from that of WPF.
3. When we set window background= "Transparent" allowstransparency= "True" in order to implement the glass window WebBrowser cannot complete the rendering of WPF, As a result, the WebBrowser content is not visible as described above.
Conclusion:
Non-WPF elements, such as Win32-based controls, cannot be rendered in WPF, otherwise WIN32-based controls render failures, such as scaling failures, color rendering failures, and other vector-related property settings. For the purposes of this chapter, to mix Win32-based controls and WPF controls, and to set
Window background= "Transparent" allowstransparency= "True" for multi-agent support, the Win32 element should be placed in the Win32 window for separate rendering purposes.
Solution:
1. Do not use WebBrowser directly in XAML, as a simple element instead, such as a canvas Grid border and so on container. For locating
2. Add the WebBrowser, the child of the form, and package it yourself to implement custom functions such as zooming.
3. In window, when initializing, instantiate the WebBrowser that passes through the form package and navigate to the container described in step one.
Sample Code:
Xaml:
<grid name= "Iecontainer" grid.column= "0" width= "1006" height= "577"/>
WebBrowser Package:
Static Class Win32
{
[StructLayout (LayoutKind.Sequential)]
public struct Point
{
.......
[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);
};
Class WEBBROWSEROVERLAYWF
{
Window _owner;
FrameworkElement _placementtarget;
Form _form; The top-level window holding the WebBrowser control
WebBrowser _WB = new WebBrowser ();
.......................
Public WEBBROWSEROVERLAYWF (FrameworkElement placementtarget)
{
_placementtarget = Placementtarget;
Window owner = Window.getwindow (Placementtarget);
_owner = owner;
_form = new form ();
_form. ShowInTaskbar = false;
_form. FormBorderStyle = Formborderstyle.none;
_WB. Dock = DockStyle.Fill;
_form. Controls.Add (_WB);
_WB. scrollbarsenabled = false;
Owner. SizeChanged + = delegate {onsizelocationchanged ();};
Owner. LocationChanged + = delegate {onsizelocationchanged ();};
_placementtarget.sizechanged + = delegate {onsizelocationchanged ();};
if (owner. IsVisible)
Initialshow ();
Else
Owner. Sourceinitialized + = Delegate{initialshow (););
..........
}
void Initialshow ()
{
NativeWindow owner = new NativeWindow ();
Owner. Assignhandle (
((HwndSource) hwndsource.fromvisual (_owner)). Handle);
_form. Show (owner);
Owner. ReleaseHandle ();
}
...............
}
Window Loaded:
WEBBROWSEROVERLAYWF WBO = new WEBBROWSEROVERLAYWF (Iecontainer);
Expand:
The previous example demonstrates how to encapsulate a Win32-based control into WPF, with the ability to scale, and reposition (space restrictions, please contact me if you need specific code). In fact, we have inadvertently involved the encapsulation and rewriting of controls. We will continue to share more of the WPF control encapsulation and rewriting technologies in the future.
WPF WebBrowser resolution of invisible issues [go]