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


本文实例讲述了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);
}

至此问题就全部解决了,希望本文所述对大家的WPF程序设计有所帮助。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3