Browser extension series-implementation of transparent browser windows

Source: Internet
Author: User
Tags transparent image

First, let's take a look:

This implementation is based on WPF and VS version 2008 SP1.

First, the implementation method in Winform is implemented by setting the opacity of the form, or by setting the TransparentKey to realize a certain color transparency. But how does one implement it in WPF?

By setting the opacity of the form, the result is that the overall webbrowser disappears. This involves the issue of "airspace" in WPF. The related articles are as follows:

Http://blogs.msdn.com/changov/archive/2009/01/19/webbrowser-control-on-transparent-wpf-window.aspx

From this point of view, the method of directly setting the transparency is not good, so back to the original problem, "making the browser form background transparent", in fact, the transparency here is just a visual feeling, that is, the background of the webpage in the browser and the background of the entire form can be integrated. As you can see, you may have thought of it. Just draw the background of the page in the browser into the background covered by the browser control. Indeed, my implementation follows this idea.

Two technologies are used here:

L Mshtml is used to operate on elements in a webpage. By adding behavior to the body tag, the background is drawn.

 

Code
[ComVisible (true), Guid ("0015EC28-C85F-49a8-9B1A-DC91E6345274 "),
ClassInterface (ClassInterfaceType. AutoDispatch)]
Public class MyGadgetBodyBehavior: IElementBehavior, IHTMLPainter
{
Public delegate void SizeChangedEventHandler (SizeChangedEventArgs e );
Public event SizeChangedEventHandler onSizeChangedEvent;
Private AppScreenSnapHelper snapHelper;

 

The following is the code for drawing part.

Code
Public void Draw (RECT rcBounds, RECT rcUpdates, int lDrawFlags, IntPtr hdc, IntPtr pvDrawObject)
{
Graphics g = Graphics. FromHdc (hdc );
Bitmap buffer = new Bitmap (width, height );
Graphics gBuffer = Graphics. FromImage (buffer );

AppScreenSnapHelper. Image image = snapHelper. GetScreenSnap ();
GBuffer. DrawImage (image. Bitmap, 0, 0 );
Image. Dispose ();

String imageSrc = (IHTMLElement2) body). currentStyle. backgroundImage;
If (! String. IsNullOrEmpty (imageSrc ))
{
Match match = Regex. Match (imageSrc, @ "url \ (" "file :///(? <Path> .*)""\)");
If (match. Success)
{
ImageSrc = match. Groups ["path"]. Value;
Using (Bitmap bitmap = new Bitmap (imageSrc ))
{
Object obj = (IHTMLElement2) body). currentStyle. marginLeft;
GBuffer. DrawImage (bitmap, new Rectangle (0, 0, width, height ));
}
}
}
G. DrawImage (buffer, rcUpdates. left, rcUpdates. top,
New Rectangle (rcUpdates. left-rcBounds. left,
RcUpdates. top-rcBounds. top, rcUpdates. right-rcUpdates. left,
RcUpdates. bottom-rcUpdates. top), GraphicsUnit. Pixel );
Buffer. Dispose ();
GBuffer. Dispose ();
G. Dispose ();

}

 

L The RenderTargetBitmap class is used for applications:

 

 

Code
Internal Image GetScreenSnap (bool isForceRefresh)
{
If (CheckPositionAndSize ()&&! IsForceRefresh)
{
Return screenImage;
}

Control. Visibility = Visibility. Hidden;
RenderTargetBitmap bitmap = new RenderTargetBitmap (int) parentWindow. Width,
(Int) parentWindow. Width, 96, 96, PixelFormats. Pbgra32 );
Bitmap. Render (parentWindow );
BitmapSource bitmapSource = bitmap. CloneCurrentValue ();
Bitmap newBitmap = ConvertSourceImageToBitmap (bitmapSource );
NewBitmap = ClipBitmap (newBitmap, new System. Drawing. Rectangle (int) oldPoint. X, (int) oldPoint. Y,
(Int) control. Width = 0? 1: (int) control. Width), (int) control. Height) = 0? 1: (int) control. Height ));

Control. Visibility = Visibility. Visible;
ScreenImage = new Image (newBitmap, imagePtr );
Return screenImage;
}

Here we use a technique to hide the control first, and then restore the display of the control.

Finally, let's talk about some shortcomings of this implementation:

  1. If the background of the application is set to transparent, the background of the browser is displayed in white, because the background of the application is used in this implementation. If the background of the application is transparent, the result is a transparent image, which cannot be transparent after being drawn to the page. If you want to achieve transparency in this case, you can consider the desktop background.
  2. If a scroll bar appears when the page is too large, the unrendered part of the page cannot be transparent, because it can only act on the displayed part. Therefore, this implementation is used to display html pages with locally controlled sizes.

Download the project as follows:

/Files/chinese-zmm/TransportWebBrowserDemo.rar

 

Related Article

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.