瀏覽器延伸系列————透明瀏覽器視窗的實現

來源:互聯網
上載者:User

首先先看一下:

    本實現是基於WPF,VS版本2008 SP1。

    先說一下在Winform中的實現方法:很簡單通過設定表單的opacity來實現,或者還可以設定TransparentKey來實現某種顏色透明。但是在WPF中則如何?呢?

通過設定表單的opacity,那麼得到結果就是webbrowser整體消失了。因為這裡面涉及到WPF中“空域”的問題,相關的文章如下:

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

    由此看來通過直接設定透明度的方法是不行了,那麼回到原來的問題,“將瀏覽器表單背景成透明”,其實這裡的透明只是一個視覺上的感覺,就是瀏覽器中網頁的背景和整個表單的背景想融合就可以。看到這裡,各位看官可能已經想到了,將瀏覽器中頁面的背景繪製成被瀏覽器控制項所覆蓋出的背景就可以了。確實,我的實現也是依照這種思路走的。

    這裡主要用到了兩個技術:

l Mshtml操作網頁中元素,通過給body標籤添加行為來實現背景的繪製。

 

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;

 

下面是繪製部分的代碼

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 RenderTargetBitmap類用來給應用程式:

 

 

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;
        }

在的時候這裡使用了一個技巧就是,先將控制項隱藏,然後,最後恢複控制項的顯示。 

最後說一下本實現的一些缺陷:

  1.  如果將應用程式的背景設定為透明,則瀏覽器的背景將呈現白色,因為本實現使用的是應用程式的背景來進行,如果應用程式背景被透明,則得到的也是一張透明的圖片,繪製到頁面上後並不能達到透明的效果。如果想在這種情況下實現透明,可以考慮對案頭背景進行。
  2. 如果網頁過大出現捲軸,那麼網頁中未呈現的部分並不能透明,因為只能作用於已經顯示的部分。所以本實現用於顯示本地控制好大小的html頁面有比較好的效果。

具體項目下載如下:

/Files/chinese-zmm/TransportWebBrowserDemo.rar 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.