WPF中AllowsTransparency和WebBrowser兼容性問題


最近項目中需要在WPF中加入WebBrowser控件,發現與AllowsTransparency="True" WindowStyle="None"有沖突,在網上搜索了一下,找到的解決方案跟我的項目有沖突,只好自己解決。

產生原因:引自http://www.cnblogs.com/SkyD/archive/2009/12/16/1625216.html一段回復

原因在於 WebBrowser 控件是GDI負責呈現的。而WPF是用dx繪制的,但AllowsTransparency="True" WindowStyle="None" 時原先由gdi繪制的窗體就沒有了。完全用dx繪制。 WebBrowser 也就顯示不出來,但能響應事件。

暫時想到最直接簡單的解決辦法就是做一個無邊框不透明的窗體,在這個窗體上放置WebBrowser控件,將這個窗體置於需要WebBroswer的窗體之上(Owner),為了方便使用,封裝成控件,包含兩部分:

1、包含WebBrowser的窗體

2、自定義用戶控件

 

1、窗體的xaml很簡單,Web.xaml

<Window x:Class="ZControls.WebBrowserEx.Web"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="grid"></Grid>
</Window>

b code,Web.xaml.cs

namespace ZControls.WebBrowserEx
{
    /// <summary>
    /// Web.xaml 的交互邏輯
    /// </summary>
    public partial class Web : Window
    {
        public System.Windows.Forms.WebBrowser WebBrowser { get { return web_browser; } }
        private System.Windows.Forms.Integration.WindowsFormsHost forms_host;
        private System.Windows.Forms.WebBrowser web_browser;
        public Web()
        {
            InitializeComponent();
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            this.ShowInTaskbar = false;
            forms_host = new System.Windows.Forms.Integration.WindowsFormsHost();
            web_browser = new System.Windows.Forms.WebBrowser();
            forms_host.Child = web_browser;
            this.grid.Children.Add(forms_host);
        }
    }
}

 

2、用戶控件,WebBrowserExControl.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
namespace ZControls.WebBrowserEx
{
    public class WebBrowserExControl : Canvas
    {
        public System.Windows.Forms.WebBrowser WebBrowser
        {
            get
            {
                return web == null ? null : web.WebBrowser;
            }
        }
        public bool IsShow { get { return show; } }
        private bool show = false;
        private Web web = null;
        private HwndSourceHook hook;
        private HwndSource hwndSource;
        private Window window;
        private const int WM_MOVE = 0x0003;
        private const int WM_MOVING = 0x0216;
        private const int WM_SIZE = 0x0005;
        private const int WM_SIZING = 0x0214;
        private const int WM_ENTERSIZEMOVE = 0x231;
        private const int WM_EXITSIZEMOVE = 0x232;
        private const int WM_PAINT = 0x000F;
        private const int WM_GETMINMAXINFO = 0x0024;//此消息發送給窗口當它將要改變大小或位置
        private const int WM_WINDOWPOSCHANGING = 0x0046;//發送此消息給那個窗口的大小和位置將要被改變時,來調用setwindowpos函數或其它窗口管理函數
        private const int WM_NCCALCSIZE = 0x0083;//當某個窗口的客戶區域必須被核算時發送此消息
        private const int WM_WINDOWPOSCHANGED = 0x0047;//發送此消息給那個窗口的大小和位置已經被改變時,來調用setwindowpos函數或其它窗口管理函數
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch (msg)
            {
                //case WM_MOVE:
                //case WM_MOVING:
                //case WM_SIZE:
                //case WM_SIZING:
                //case WM_ENTERSIZEMOVE:
                //case WM_EXITSIZEMOVE:
                //case WM_GETMINMAXINFO:
                //case WM_WINDOWPOSCHANGING:
                //case WM_NCCALCSIZE:
                //case WM_WINDOWPOSCHANGED:
                case 49849: //case 0xc2b9: 這是一個很神奇的消息,可能是WPF專用               
                case WM_PAINT:
                    render_web();
                    break;
            }
            //Console.WriteLine(msg);
            return IntPtr.Zero;
        }
        public WebBrowserExControl()
        {
            //this.Text = "WebBrowserExControl";
        }
        ~WebBrowserExControl()
        {
            Hide();
        }
        public void Show()
        {
            if (!show)
            {
                web = new Web();
                window = Window.GetWindow(this);
                hwndSource = PresentationSource.FromVisual(this) as HwndSource;
                if (hwndSource != null)
                {
                    hook = new HwndSourceHook(WndProc);
                    hwndSource.AddHook(hook);
                }
                web.Owner = window;
                web.Show();
                show = true;
                render_web();
            }
        }
        public void Hide()
        {
            if (hwndSource != null)
            {
                hwndSource.RemoveHook(hook);
            }
            if (web != null)
            {
                //try
                //{
                web.WebBrowser.Dispose();
                web.Close();
                //}
                //catch { }
                web = null;
            }
            show = false;
        }
        private void render_web()
        {
            if (show)
            {
                Point point = this.TransformToAncestor(window).Transform(new Point(0, 0));
                //point = this.PointToScreen(point);
                web.Left = window.Left + point.X;// -LeftEx;
                web.Top = window.Top + point.Y;// -TopEx;
                //point = this.PointToScreen(point);
                //web.Left = point.X;// -LeftEx;
                //web.Top = point.Y;// -TopEx;
                web.Width = this.ActualWidth;
                web.Height = this.ActualHeight;
            }
        }
    }
}

 

最后,直接在需要的地方引用這個控件

<WebBrowserEx:WebBrowserExControl Background="Aquamarine" x:Name="my_web"/>

b code

private void Button_Click(object sender, RoutedEventArgs e)
 {
            var url = txt_url.Text;
            my_web.Show();
            my_web.WebBrowser.Navigate(url);
 }

在hook窗體的過程中,需要找一個窗體move與size改變的消息,嘗試了半個多小時,找到消息 49849 (0xc2b9),能比較好的滿足需求,猜想這個可能是WPF定義的消息。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM