首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如 。该网友的解决办法也是别出心裁的,为什么这样说呢,你下载了他的程序认真读一下就便知道,他的webBrowser控件的是单独放在一个Form中,让这个Form与WPF中的一个Bord控件进行关联,进行同步移动,但是在移动的时候会出现闪烁,并且还会出现运动的白点,用户体验肯定不好。 OK,绕了一大圈,还是言归正传吧,为什么会出现该问题呢,是什么原因导致在WPF中设置了透明窗体之后,嵌入WinForm中的控件会显示不了呢。一开始我以为是没有正常加载,还要我有UISPY,通过这个软件,我捕获了一下当前运行的程序,发现我在WPF中内嵌的WinForm控件已经加载上了,只是没有看到而已罢了。很郁闷啊。 悲催的程序,头疼啊,是什么原因导致的呢,网上查资料,找到了 ,让我了解了不少知识。由于项目要用到透明窗体还要制作圆角窗体,说以本来打算不改变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 { ///下面的问题就是如何进行操作了,首先在进行嵌入WinForm控件的WPF窗体中添加一个Load事件,在事件中添加如下代码:/// 带有外边框和标题的windows的样式 /// public const long WS_CAPTION = 0X00C0000L; // public const long WS_BORDER = 0X0080000L; ////// window 扩展样式 分层显示 /// public const long WS_EX_LAYERED = 0x00080000L; ////// 带有alpha的样式 /// public const long LWA_ALPHA = 0x00000002L; ////// 颜色设置 /// public const long LWA_COLORKEY = 0x00000001L; ////// window的基本样式 /// public const int GWL_STYLE = -16; ////// window的扩展样式 /// public const int GWL_EXSTYLE = -20; ////// 设置窗体的样式 /// /// 操作窗体的句柄 /// 进行设置窗体的样式类型. /// 新样式 [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern void SetWindowLong(IntPtr handle, int oldStyle, long newStyle); ////// 获取窗体指定的样式. /// /// 操作窗体的句柄 /// 要进行返回的样式 ///当前window的样式 [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern long GetWindowLong(IntPtr handle, int style); ////// 设置窗体的工作区域. /// /// 操作窗体的句柄. /// 操作窗体区域的句柄. /// if set totrue [regraw]. ///返回值 [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw); ////// 创建带有圆角的区域. /// /// 左上角坐标的X值. /// 左上角坐标的Y值. /// 右下角坐标的X值. /// 右下角坐标的Y值. /// 圆角椭圆的 width. /// 圆角椭圆的 height. ///hRgn的句柄 [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height); ////// Sets the layered window attributes. /// /// 要进行操作的窗口句柄 /// RGB的值 /// Alpha的值,透明度 /// 附带参数 ///true or false [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern bool SetLayeredWindowAttributes(IntPtr handle, ulong colorKey, byte alpha, long flags); }
// 获取窗体句柄 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);还有就是窗体大小改变之后还要重画圆角窗体,否则出现很不理想的显示效果,添加如下事件代码,解决窗体大小改变的时候,重画窗体的圆角区域:
////// Handles the SizeChanged event of the DesktopShell control. /// /// The source of the event. /// Theinstance containing the event data. 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类中添加如下代码:
///另外我新建了一个类进行进行改变窗体的大家,改类名为:ChangeWindowSize,/// 系统菜单 /// public const long WS_SYSMENU = 0x00080000L;
该类代码为:
///修改ChangeWindowSize类,/// 拖动窗体四角,可以改变窗体的大小 /// public class ChangeWindowSize { ////// 边框宽度 /// private readonly int Thickness = 4; ////// 改变大小的通知消息 /// private const int WMNCHITTEST = 0x0084; ////// 拐角宽度 /// private readonly int angelWidth = 12; ////// 要改变窗体大小的对象 /// private Window window = null; ////// 鼠标坐标 /// private Point mousePoint = new Point(); ////// 构造函数,初始化目标窗体对象 /// /// 目标窗体 public ChangeWindowSize(Window window) { this.window = window; } ////// 进行注册钩子 /// public void RegisterHook() { HwndSource hwndSource = PresentationSource.FromVisual(this.window) as HwndSource; if (hwndSource != null) { hwndSource.AddHook(new HwndSourceHook(this.WndProc)); } } ////// 窗体回调程序 /// /// 窗体句柄 /// 消息 /// 附加参数1 /// 附加参数2 /// 是否处理 ///返回句柄 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; } }
添加成员变量:
///修改回调函数:/// 窗口的大小和位置将要被改变时的消息 /// private const int WMWINDOWPOSCHANGING = 0x0046;
///最后对主窗体重载如下函数:/// 窗体回调程序 /// /// 窗体句柄 /// 消息 /// 附加参数1 /// 附加参数2 /// 是否处理 ///返回句柄 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(); }最大化最小化区域的修改,对主窗体添加如下事件:
///具体原因为:/// Resets the max size initialize. /// /// The sender. /// Theinstance containing the event data. private void ResetMaxSizeInitialize(object sender, EventArgs e) { this.MaxWidth = SystemParameters.WorkArea.Width; this.MaxHeight = SystemParameters.WorkArea.Height; }
移动窗体的消息和响应系统菜单消息之间产生冲突.
我上述解决的方案策略为:
我采用迂回策略,当开始进行移动的时候,去除系统菜单的样式,这样就不能响应系统菜单的消息。移动完成时,添加系统菜单的样式,这样再去点击任务栏中系统的图标便可以响应系统菜单的消息。
关于在Framework4.0无法工作的问题修复:
//-----------------------------------------------------------------------//// Copyright (c) Vadeware Enterprises. All rights reserved.// 窗体// //-----------------------------------------------------------------------namespace MedSys.PresentationCore.AdjustWindow{ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Interop; ////// 拖动窗体四角,可以改变窗体的大小 /// public class ChangeWindowSize { ////// 边框宽度 /// private readonly int Thickness = 4; ////// 改变大小的通知消息 /// private const int WMNCHITTEST = 0x0084; ////// 窗口的大小和位置将要被改变时的消息 /// private const int WMWINDOWPOSCHANGING = 0x0046; ////// 拐角宽度 /// private readonly int angelWidth = 12; ////// 要改变窗体大小的对象 /// private Window window = null; ////// 鼠标坐标 /// private Point mousePoint = new Point(); ////// 构造函数,初始化目标窗体对象 /// /// 目标窗体 public ChangeWindowSize(Window window) { this.window = window; } ////// 进行注册钩子 /// public void RegisterHook() { HwndSource hwndSource = PresentationSource.FromVisual(this.window) as HwndSource; if (hwndSource != null) { hwndSource.AddHook(new HwndSourceHook(this.WndProc)); } } ////// 窗体回调程序 /// /// 窗体句柄 /// 消息 /// 附加参数1 /// 附加参数2 /// 是否处理 ///返回句柄 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 (c) Vadeware Enterprises. All rights reserved.// windows 函数的借口接口函数// namespace MedSys.PresentationCore.AdjustWindow{ using System; using System.Collections.Generic; using System.Linq; using System.Text; ////// 主窗体内部类 /// public class NativeMethods { ////// 带有外边框和标题的windows的样式 /// public const int WS_CAPTION = 0X00C0000; ////// 系统菜单 /// public const int WS_SYSMENU = 0x00080000; ////// window 扩展样式 分层显示 /// public const int WS_EX_LAYERED = 0x00080000; ////// 带有alpha的样式 /// public const int LWA_ALPHA = 0x00000002; ////// 颜色设置 /// public const int LWA_COLORKEY = 0x00000001; ////// window的基本样式 /// public const int GWL_STYLE = -16; ////// window的扩展样式 /// public const int GWL_EXSTYLE = -20; ////// 设置窗体的样式 /// /// 操作窗体的句柄 /// 进行设置窗体的样式类型. /// 新样式 [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern void SetWindowLong(IntPtr handle, int oldStyle, int newStyle); ////// 获取窗体指定的样式. /// /// 操作窗体的句柄 /// 要进行返回的样式 ///当前window的样式 [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern int GetWindowLong(IntPtr handle, int style); ////// 设置窗体的工作区域. /// /// 操作窗体的句柄. /// 操作窗体区域的句柄. /// if set totrue [regraw]. ///返回值 [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw); ////// 创建带有圆角的区域. /// /// 左上角坐标的X值. /// 左上角坐标的Y值. /// 右下角坐标的X值. /// 右下角坐标的Y值. /// 圆角椭圆的 width. /// 圆角椭圆的 height. ///hRgn的句柄 [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height); ////// Sets the layered window attributes. /// /// 要进行操作的窗口句柄 /// RGB的值 /// Alpha的值,透明度 /// 附带参数 ///true or false [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下载连接:
感谢网友:对我支持
____2012年09月21日17点36分修改__添加Vs2010的Demo_____________________________________
应网友的要求,现将DMEO奉上。
该Demo为Framework4.0的,在Vs2010下执行没有问题。
链接(收您1分,请您体谅和尊重原创):
感谢网友给出的解决方法和对此问题的关注。首先感谢 给出的修改建议,以及 对该问题质疑。
同样感谢其他人的关注。谢谢你们的关注。我将做的更好。
________________________________ _ ___ 至此问题就全部解决了,如果有朋友有疑问,可以留言,能帮助到你,我很荣幸。请尊重作者的劳动果实,支持转载,请注明博客的出处。谢谢。