码迷,mamicode.com
首页 > Windows程序 > 详细

WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案

时间:2018-07-12 19:53:57      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:details   forms   lan   c++   需要   过程   from   分层   layer   

原文:WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案

?? ? ?? ?首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如 ?WPF中不规则窗体与WebBrowser控件的兼容问题解决办法该网友的解决办法也是别出心裁的,为什么这样说呢,你下载了他的程序认真读一下就便知道,他的webBrowser控件的是单独放在一个Form中,让这个Form与WPF中的一个Bord控件进行关联,进行同步移动,但是在移动的时候会出现闪烁,并且还会出现运动的白点,用户体验肯定不好。
?? ? ?OK,绕了一大圈,还是言归正传吧,为什么会出现该问题呢,是什么原因导致在WPF中设置了透明窗体之后,嵌入WinForm中的控件会显示不了呢。一开始我以为是没有正常加载,还要我有UISPY,通过这个软件,我捕获了一下当前运行的程序,发现我在WPF中内嵌的WinForm控件已经加载上了,只是没有看到而已罢了。很郁闷啊。
?? ? 悲催的程序,头疼啊,是什么原因导致的呢,网上查资料,找到了http://msdn.microsoft.com/zh-cn/library/aa970688.aspx?,让我了解了不少知识。由于项目要用到透明窗体还要制作圆角窗体,说以本来打算不改变WPF中对window的设置,即不改变WindowStyle=“None” 和AllowTransparent = “True”这些设置,想在在WindowsFormsHost上做一些设置,发现这条路走不通。浪费了不少时间。
?? ? 此路不通只有换思路了,那么把AllowTransparent =“false” ,然后就可以显示,呵呵……当然还要修改啊,WPF的窗体多难看啊,外边有一个边框。怎么搞去啊,怎样办,这也是一个问题啊。想用WPF的特性,悲剧了,好像没有相关的方法啊。
?? ? ?OK,路还是有的,程序员就是来解决办法的,怎么办,只能调用Windows的API,把最外层的那层边框被去掉了。那么需要什么呢,思路是有了,对吧,那么就行动吧,google 和百度一通,发现还真有不少例子,c++的例子最全面,可以参考一下。那么就整理了一下需要这些函数:
?? ??SetWindowLong ? 设置值window的样式
?? ??GetWindowLong ? 获取window的样式
?? ? SetWindowRgn ? ? 设置window的工作区
?? ??CreateRoundRectRgn ?创建带有圆角的区域
?? ??SetLayeredWindowAttributes ?设置层次窗体,进行透明度的设置
直接百度,百科有对他们的详细解释,不过给出的是C++的解释,那么需要你对C++的东西进行转化成C#的东西,有关C#如何调用C++的DLL文件,百度和google中有你想要的答案,我就补多少了,不过要注意类型的转化和字符
集的转化。
下面我把我转化好的函数给大家贴上来,以飨读者。

    public class NativeMethods
    {
        /// <summary>
        /// 带有外边框和标题的windows的样式
        /// </summary>
        public const long WS_CAPTION = 0X00C0000L;

        // public const long WS_BORDER = 0X0080000L;

        /// <summary>
        /// window 扩展样式 分层显示
        /// </summary>
        public const long WS_EX_LAYERED = 0x00080000L;

        /// <summary>
        /// 带有alpha的样式
        /// </summary>
        public const long LWA_ALPHA = 0x00000002L;

        /// <summary>
        /// 颜色设置
        /// </summary>
        public const long LWA_COLORKEY = 0x00000001L;

        /// <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, long newStyle);

        /// <summary>
        /// 获取窗体指定的样式.
        /// </summary>
        /// <param name="handle">操作窗体的句柄</param>
        /// <param name="style">要进行返回的样式</param>
        /// <returns>当前window的样式</returns>
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern long 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, ulong colorKey, byte alpha, long flags);
    }
?? ? ?下面的问题就是如何进行操作了,首先在进行嵌入WinForm控件的WPF窗体中添加一个Load事件,在事件中添加如下代码:
           // 获取窗体句柄
            IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;

            // 获得窗体的 样式
            long oldstyle = NativeMethods.GetWindowLong(hwnd, NativeMethods.GWL_STYLE);

            // 更改窗体的样式为无边框窗体
            NativeMethods.SetWindowLong(hwnd, NativeMethods.GWL_STYLE, oldstyle & ~NativeMethods.WS_CAPTION);

            // SetWindowLong(hwnd, GWL_EXSTYLE, oldstyle & ~WS_EX_LAYERED);
            // 1 | 2 << 8 | 3 << 16  r=1,g=2,b=3 详见winuse.h文件
            // 设置窗体为透明窗体
            NativeMethods.SetLayeredWindowAttributes(hwnd, 1 | 2 << 8 | 3 << 16, 0, NativeMethods.LWA_ALPHA);

            // 创建圆角窗体  12 这个值可以根据自身项目进行设置
            NativeMethods.SetWindowRgn(hwnd, NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);

??还有就是窗体大小改变之后还要重画圆角窗体,否则出现很不理想的显示效果,添加如下事件代码,解决窗体大小改变的时候,重画窗体的圆角区域:
        /// <summary>
        /// Handles the SizeChanged event of the DesktopShell control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.SizeChangedEventArgs"/> instance containing the event data.</param>
        private void DesktopShell_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // 获取窗体句柄
            IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;

            // 创建圆角窗体
            NativeMethods.SetWindowRgn(hwnd,NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);
        }

? ?

PS:有网友在操作的过程中,发现任务栏中不显示右击的系统菜单的问题。

其实这个问题我也遇到过,不过已经修复了该问题。下面贴上如何修复该问题的代码。具体原因,最后分析。

在NativieMethods类中添加如下代码:

        /// <summary>
        /// 系统菜单
        /// </summary>
        public const long WS_SYSMENU = 0x00080000L;
另外我新建了一个类进行进行改变窗体的大家,改类名为:ChangeWindowSize,

该类代码为:

    /// <summary>
    /// 拖动窗体四角,可以改变窗体的大小
    /// </summary>
    public class ChangeWindowSize
    {
        /// <summary>
        /// 边框宽度
        /// </summary>
        private readonly int Thickness = 4;

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

        /// <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)
        {
            switch (msg)
            {
                case WMNCHITTEST:
                    this.mousePoint.X = longParam.ToInt32() & 0xFFFF;
                    this.mousePoint.Y = longParam.ToInt32() >> 16;

                    // 窗口位置  // 窗口左上角
                    if (this.mousePoint.Y - this.window.Top <= this.angelWidth
                       && this.mousePoint.X - this.window.Left <= this.angelWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPLEFT);
                    }
                    else if (this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.angelWidth
                       && this.mousePoint.X - this.window.Left <= this.angelWidth) // 窗口左下角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMLEFT);
                    }
                    else if (this.mousePoint.Y - this.window.Top <= this.angelWidth
                       && this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.angelWidth) // 窗口右上角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPRIGHT);
                    }
                    else if (this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.angelWidth
                       && this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.angelWidth) // 窗口右下角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
                    }
                    else if (this.mousePoint.X - this.window.Left <= this.Thickness) // 窗口左侧
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTLEFT);
                    }
                    else if (this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.Thickness) // 窗口右侧
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTRIGHT);
                    }
                    else if (this.mousePoint.Y - this.window.Top <= this.Thickness) // 窗口上方
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOP);
                    }
                    else if (this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.Thickness) // 窗口下方
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOM);
                    }
                    else // 窗口移动
                    {
                        // handled = true;
                        return new IntPtr((int)HitTest.HTCAPTION);
                    }
            }

            return IntPtr.Zero;
        }
    }
修改ChangeWindowSize类,

添加成员变量:

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

修改回调函数:
        /// <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)
        {
            // 获得窗体的 样式
            long oldstyle = MedSys.PresentationCore.AdjustWindow.NativeMethods.GetWindowLong(hwnd, MedSys.PresentationCore.AdjustWindow.NativeMethods.GWL_STYLE);
            switch (msg)
            {
                case WMNCHITTEST:
                    this.mousePoint.X = longParam.ToInt32() & 0xFFFF;
                    this.mousePoint.Y = longParam.ToInt32() >> 16;

                    // 更改窗体的样式为无边框窗体
                    MedSys.PresentationCore.AdjustWindow.NativeMethods.SetWindowLong(hwnd, MedSys.PresentationCore.AdjustWindow.NativeMethods.GWL_STYLE, oldstyle & ~MedSys.PresentationCore.AdjustWindow.NativeMethods.WS_CAPTION);

                    // 窗口位置  // 窗口左上角
                    if (this.mousePoint.Y - this.window.Top <= this.angelWidth
                       && this.mousePoint.X - this.window.Left <= this.angelWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPLEFT);
                    }
                    else if (this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.angelWidth
                       && this.mousePoint.X - this.window.Left <= this.angelWidth) // 窗口左下角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMLEFT);
                    }
                    else if (this.mousePoint.Y - this.window.Top <= this.angelWidth
                       && this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.angelWidth) // 窗口右上角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPRIGHT);
                    }
                    else if (this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.angelWidth
                       && this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.angelWidth) // 窗口右下角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
                    }
                    else if (this.mousePoint.X - this.window.Left <= this.Thickness) // 窗口左侧
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTLEFT);
                    }
                    else if (this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.Thickness) // 窗口右侧
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTRIGHT);
                    }
                    else if (this.mousePoint.Y - this.window.Top <= this.Thickness) // 窗口上方
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOP);
                    }
                    else if (this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.Thickness) // 窗口下方
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOM);
                    }
                    else // 窗口移动
                    {
                        // handled = true;
                        // 更改窗体的样式为无边框窗体
                        return new IntPtr((int)HitTest.HTCAPTION);
                    }

                case WMWINDOWPOSCHANGING:

                    // 在将要改变的时候,是样式添加系统菜单
                    MedSys.PresentationCore.AdjustWindow.NativeMethods.SetWindowLong(hwnd, MedSys.PresentationCore.AdjustWindow.NativeMethods.GWL_STYLE, oldstyle & ~MedSys.PresentationCore.AdjustWindow.NativeMethods.WS_CAPTION | MedSys.PresentationCore.AdjustWindow.NativeMethods.WS_SYSMENU);
                    break;
            }

            return IntPtr.Zero;
        }
最后对主窗体重载如下函数:

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            ChangeWindowSize changeWindowSize = new ChangeWindowSize(this);
            changeWindowSize.RegisterHook();
        }
最大化最小化区域的修改,对主窗体添加如下事件:

        /// <summary>
        /// Resets the max size initialize.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void ResetMaxSizeInitialize(object sender, EventArgs e)
        {
            this.MaxWidth = SystemParameters.WorkArea.Width;
            this.MaxHeight = SystemParameters.WorkArea.Height;
        }
具体原因为:

? ? 移动窗体的消息和响应系统菜单消息之间产生冲突.

我上述解决的方案策略为:

? ?我采用迂回策略,当开始进行移动的时候,去除系统菜单的样式,这样就不能响应系统菜单的消息。移动完成时,添加系统菜单的样式,这样再去点击任务栏中系统的图标便可以响应系统菜单的消息。


关于在Framework4.0无法工作的问题修复:

//-----------------------------------------------------------------------
// <copyright file="ChangeWindowSize.cs" company="Vadeware">
//     Copyright (c) Vadeware Enterprises. All rights reserved.
//     窗体
// </copyright>
//-----------------------------------------------------------------------
namespace MedSys.PresentationCore.AdjustWindow
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Interop;

    /// <summary>
    /// 拖动窗体四角,可以改变窗体的大小
    /// </summary>
    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 = MedSys.PresentationCore.AdjustWindow.NativeMethods.GetWindowLong(hwnd, MedSys.PresentationCore.AdjustWindow.NativeMethods.GWL_STYLE);
            switch (msg)
            {
                case WMNCHITTEST:
                    this.mousePoint.X = longParam.ToInt32() & 0xFFFF;
                    this.mousePoint.Y = longParam.ToInt32() >> 16;

                    // 更改窗体的样式为无边框窗体
                    MedSys.PresentationCore.AdjustWindow.NativeMethods.SetWindowLong(hwnd, MedSys.PresentationCore.AdjustWindow.NativeMethods.GWL_STYLE, oldstyle & ~MedSys.PresentationCore.AdjustWindow.NativeMethods.WS_CAPTION);

                    // 窗口位置  // 窗口左上角
                    if (this.mousePoint.Y - this.window.Top <= this.angelWidth
                       && this.mousePoint.X - this.window.Left <= this.angelWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPLEFT);
                    }
                    else if (this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.angelWidth
                       && this.mousePoint.X - this.window.Left <= this.angelWidth) // 窗口左下角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMLEFT);
                    }
                    else if (this.mousePoint.Y - this.window.Top <= this.angelWidth
                       && this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.angelWidth) // 窗口右上角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPRIGHT);
                    }
                    else if (this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.angelWidth
                       && this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.angelWidth) // 窗口右下角
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
                    }
                    else if (this.mousePoint.X - this.window.Left <= this.Thickness) // 窗口左侧
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTLEFT);
                    }
                    else if (this.window.ActualWidth + this.window.Left - this.mousePoint.X <= this.Thickness) // 窗口右侧
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTRIGHT);
                    }
                    else if (this.mousePoint.Y - this.window.Top <= this.Thickness) // 窗口上方
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOP);
                    }
                    else if (this.window.ActualHeight + this.window.Top - this.mousePoint.Y <= this.Thickness) // 窗口下方
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOM);
                    }
                    else // 窗口移动
                    {
                        // handled = true;
                        // 更改窗体的样式为无边框窗体
                        return new IntPtr((int)HitTest.HTCAPTION);
                    }

                case WMWINDOWPOSCHANGING:

                    // 在将要改变的时候,是样式添加系统菜单
                    MedSys.PresentationCore.AdjustWindow.NativeMethods.SetWindowLong(hwnd, MedSys.PresentationCore.AdjustWindow.NativeMethods.GWL_STYLE, oldstyle & ~MedSys.PresentationCore.AdjustWindow.NativeMethods.WS_CAPTION | MedSys.PresentationCore.AdjustWindow.NativeMethods.WS_SYSMENU);
                    break;
            }

            return IntPtr.Zero;
        }
    }
}

//-----------------------------------------------------------------------
// <copyright file="NativeMethods.cs" company="Vadeware">
//     Copyright (c) Vadeware Enterprises. All rights reserved.
//    windows 函数的借口接口函数
// </copyright>
namespace MedSys.PresentationCore.AdjustWindow
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

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


____2012年08月05日11点05分修改__添加Vs2008的Demo_____________________________________

应网友的要求,现将DMEO奉上。

该Demo为Framework3.5的,在Vs2008下执行没有问题。

Demo下载连接:http://download.csdn.net/detail/wziyx513225244/4477441

感谢网友:huyunsuo?对我支持


____2012年09月21日17点36分修改__添加Vs2010的Demo_____________________________________

?应网友的要求,现将DMEO奉上。

该Demo为Framework4.0的,在Vs2010下执行没有问题。

链接(收您1分,请您体谅和尊重原创):http://download.csdn.net/detail/wziyx513225244/4585821

感谢网友给出的解决方法和对此问题的关注。首先感谢mikejodden?给出的修改建议,以及?llhccyc?对该问题质疑。

同样感谢其他人的关注。谢谢你们的关注。我将做的更好。

_________________________________ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?___
?至此问题就全部解决了,如果有朋友有疑问,可以留言,能帮助到你,我很荣幸。

? ?请尊重作者的劳动果实,支持转载,请注明博客的出处。谢谢。

WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案

标签:details   forms   lan   c++   需要   过程   from   分层   layer   

原文地址:https://www.cnblogs.com/lonelyxmas/p/9301033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!