wpf中解決無邊框時,webBroswer沒法顯示

在wfp開發中,若是設置AllowsTransparency="True"和WindowStyle="None"後,瀏覽器沒法正常顯示,查找網上資料後,發現能夠經過註冊鉤子,引用win32dll解決問題,正好園子中一個朋友也遇到,就記錄下來,代碼以下:web

在MainWindow.cs中添加劇載:windows

 /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            webBrowser.Navigate("http://www.baidu.com");
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            ChangeWindowSize changeWindowSize = new ChangeWindowSize(this);
            changeWindowSize.RegisterHook();
        }
    }

 

MainWindow.xaml中添加 瀏覽器

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"  WindowStyle="None" >
    <Grid>
        <WebBrowser x:Name="webBrowser"></WebBrowser>
    </Grid>
</Window>

 

而後調用鉤子程序:ide

public class ChangeWindowSize
    {
        /// <summary>
        /// 邊框寬度
        /// </summary>
        private readonly int Thickness = 4;

        /// <summary>
        /// 改變大小的通知消息
        /// </summary>
        private const int WMNCHITTEST = 0x0084;

        /// <summary>
        /// 窗口的大小和位置將要被改變時的消息
        /// </summary>
        private const int WMWINDOWPOSCHANGING = 0x0046;

        /// <summary>
        /// 拐角寬度
        /// </summary>
        private readonly int angelWidth = 12;

        /// <summary>
        /// 要改變窗體大小的對象
        /// </summary>
        private Window window = null;

        /// <summary>
        /// 鼠標座標
        /// </summary>
        private Point mousePoint = new Point();

        /// <summary>
        /// 構造函數,初始化目標窗體對象
        /// </summary>
        /// <param name="window">目標窗體</param>
        public ChangeWindowSize(Window window)
        {
            this.window = window;
        }

        /// <summary>
        /// 進行註冊鉤子
        /// </summary>
        public void RegisterHook()
        {
            HwndSource hwndSource = PresentationSource.FromVisual(this.window) as HwndSource;
            if (hwndSource != null)
            {
                hwndSource.AddHook(new HwndSourceHook(this.WndProc));
            }
        }

        /// <summary>
        /// 窗體回調程序
        /// </summary>
        /// <param name="hwnd">窗體句柄</param>
        /// <param name="msg">消息</param>
        /// <param name="wideParam">附加參數1</param>
        /// <param name="longParam">附加參數2</param>
        /// <param name="handled">是否處理</param>
        /// <returns>返回句柄</returns>
        public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wideParam, IntPtr longParam, ref bool handled)
        {
            // 得到窗體的 樣式
            int oldstyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE);
            switch (msg)
            {
                case WMNCHITTEST:
                    this.mousePoint.X = longParam.ToInt32() & 0xFFFF;
                    this.mousePoint.Y = longParam.ToInt32() >> 16;
                    // 更改窗體的樣式爲無邊框窗體
                    NativeMethods.SetWindowLong(hwnd, NativeMethods.GWL_STYLE, oldstyle & ~NativeMethods.WS_CAPTION);
                    return new IntPtr((int)HitTest.HTCAPTION);
                //}

                case WMWINDOWPOSCHANGING:

                    // 在將要改變的時候,是樣式添加系統菜單
                    NativeMethods.SetWindowLong(hwnd, NativeMethods.GWL_STYLE, oldstyle & ~NativeMethods.WS_CAPTION | NativeMethods.WS_SYSMENU);
                    break;
            }

            return IntPtr.Zero;
        }
    }

    /// <summary>
    /// 主窗體內部類
    /// </summary>
    public class NativeMethods
    {
        /// <summary>
        /// 帶有外邊框和標題的windows的樣式
        /// </summary>
        public const int WS_CAPTION = 0X00C0000;

        /// <summary>
        /// 系統菜單
        /// </summary>
        public const int WS_SYSMENU = 0x00080000;

        /// <summary>
        /// window 擴展樣式 分層顯示
        /// </summary>
        public const int WS_EX_LAYERED = 0x00080000;

        /// <summary>
        /// 帶有alpha的樣式
        /// </summary>
        public const int LWA_ALPHA = 0x00000002;

        /// <summary>
        /// 顏色設置
        /// </summary>
        public const int LWA_COLORKEY = 0x00000001;

        /// <summary>
        /// window的基本樣式
        /// </summary>
        public const int GWL_STYLE = -16;

        /// <summary>
        /// window的擴展樣式
        /// </summary>
        public const int GWL_EXSTYLE = -20;

        /// <summary>
        /// 設置窗體的樣式
        /// </summary>
        /// <param name="handle">操做窗體的句柄</param>
        /// <param name="oldStyle">進行設置窗體的樣式類型.</param>
        /// <param name="newStyle">新樣式</param>
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern void SetWindowLong(IntPtr handle, int oldStyle, int newStyle);

        /// <summary>
        /// 獲取窗體指定的樣式.
        /// </summary>
        /// <param name="handle">操做窗體的句柄</param>
        /// <param name="style">要進行返回的樣式</param>
        /// <returns>當前window的樣式</returns>
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern int GetWindowLong(IntPtr handle, int style);

        /// <summary>
        /// 設置窗體的工做區域.
        /// </summary>
        /// <param name="handle">操做窗體的句柄.</param>
        /// <param name="handleRegion">操做窗體區域的句柄.</param>
        /// <param name="regraw">if set to <c>true</c> [regraw].</param>
        /// <returns>返回值</returns>
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw);

        /// <summary>
        /// 建立帶有圓角的區域.
        /// </summary>
        /// <param name="x1">左上角座標的X值.</param>
        /// <param name="y1">左上角座標的Y值.</param>
        /// <param name="x2">右下角座標的X值.</param>
        /// <param name="y2">右下角座標的Y值.</param>
        /// <param name="width">圓角橢圓的 width.</param>
        /// <param name="height">圓角橢圓的 height.</param>
        /// <returns>hRgn的句柄</returns>
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height);

        /// <summary>
        /// Sets the layered window attributes.
        /// </summary>
        /// <param name="handle">要進行操做的窗口句柄</param>
        /// <param name="colorKey">RGB的值</param>
        /// <param name="alpha">Alpha的值,透明度</param>
        /// <param name="flags">附帶參數</param>
        /// <returns>true or false</returns>
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern bool SetLayeredWindowAttributes(IntPtr handle, uint colorKey, byte alpha, int flags);
    }

    /// <summary>
    /// 枚舉測試命中
    /// </summary>
    public enum HitTest : int
    {
        /// <summary>
        /// 錯誤
        /// </summary>
        HTERROR = -2,

        /// <summary>
        /// 透明
        /// </summary>
        HTTRANSPARENT = -1,

        /// <summary>
        /// 任意位置
        /// </summary>
        HTNOWHERE = 0,

        /// <summary>
        /// 客戶端
        /// </summary>
        HTCLIENT = 1,

        /// <summary>
        /// 標題
        /// </summary>
        HTCAPTION = 2,

        /// <summary>
        /// 系統菜單
        /// </summary>
        HTSYSMENU = 3,

        /// <summary>
        /// GroupBOx
        /// </summary>
        HTGROWBOX = 4,

        /// <summary>
        /// GroupBox的大小
        /// </summary>
        HTSIZE = HTGROWBOX,

        /// <summary>
        /// 菜單
        /// </summary>
        HTMENU = 5,

        /// <summary>
        /// 水平滾動條
        /// </summary>
        HTHSCROLL = 6,

        /// <summary>
        /// 垂直滾動條
        /// </summary>
        HTVSCROLL = 7,

        /// <summary>
        /// 最小化按鈕
        /// </summary>
        HTMINBUTTON = 8,

        /// <summary>
        /// 最大化按鈕
        /// </summary>
        HTMAXBUTTON = 9,

        /// <summary>
        /// 窗體左邊
        /// </summary>
        HTLEFT = 10,

        /// <summary>
        /// 窗體右邊
        /// </summary>
        HTRIGHT = 11,

        /// <summary>
        /// 窗體頂部
        /// </summary>
        HTTOP = 12,

        /// <summary>
        /// 窗體左上角
        /// </summary>
        HTTOPLEFT = 13,

        /// <summary>
        /// 窗體右上角
        /// </summary>
        HTTOPRIGHT = 14,

        /// <summary>
        /// 窗體底部
        /// </summary>
        HTBOTTOM = 15,

        /// <summary>
        /// 窗體左下角
        /// </summary>
        HTBOTTOMLEFT = 16,

        /// <summary>
        /// 窗體右下角
        /// </summary>
        HTBOTTOMRIGHT = 17,

        /// <summary>
        /// 窗體邊框
        /// </summary>
        HTBORDER = 18,

        /// <summary>
        /// 窗體縮小
        /// </summary>
        HTREDUCE = HTMINBUTTON,

        /// <summary>
        /// 窗體填出
        /// </summary>
        HTZOOM = HTMAXBUTTON,

        /// <summary>
        /// 開始改變大小
        /// </summary>
        HTSIZEFIRST = HTLEFT,

        /// <summary>
        /// 結束改變大小
        /// </summary>
        HTSIZELAST = HTBOTTOMRIGHT,

        /// <summary>
        /// 對象
        /// </summary>
        HTOBJECT = 19,

        /// <summary>
        /// 關閉
        /// </summary>
        HTCLOSE = 20,

        /// <summary>
        /// 幫助
        /// </summary>
        HTHELP = 21,
    }

 效果以下:函數

 

源碼地址:http://files.cnblogs.com/betterchaner/wpfWebBroswer.rar測試

相關文章
相關標籤/搜索