在开发wpf项目时,需要调用外部com组件,同时需要制作透明窗口,于是问题出现了,当我们在设置 AllowsTransparency="True"后,com组件显示不出来了,只有透明属性为false才能正常显示,此时找到了http://blog.csdn.net/detecyang/article/details/7946237这篇博客,他提供了很好的解决方案,当我使用后问题出现了,他提供的类在vs2008的.net3.5环境运行一切正常,但放到vs2012我的开发环境中,运行就会报错,提示“原托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。”
为了解决这个问题查阅了msdn,找到setwindowlong(intptr,int32,intptr)方法,问题的关键就出在这里,后有修改了类代码,完美运行,不再报错,代码如下:
class NativeMethods { /// <summary> /// 带有外边框和标题的windows的样式 /// </summary> public const long WS_CAPTION = 0x00C00000L; public const long WS_CAPTION_2 = 0X00C0000L; // public const long WS_BORDER = 0X0080000L; /// <summary> /// window 扩展样式 分层显示 /// </summary> public const long WS_EX_LAYERED = 0x00080000L; public const long WS_CHILD = 0x40000000L; /// <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")] //[DllImport("User32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] // public static extern void SetWindowLong(IntPtr handle, int oldStyle, long newStyle); public static extern void SetWindowLong(IntPtr handle, int oldStyle, IntPtr newStyle); /// <summary> /// 获取窗体指定的样式. /// </summary> /// <param name="handle">操作窗体的句柄</param> /// <param name="style">要进行返回的样式</param> /// <returns>当前window的样式</returns> [System.Runtime.InteropServices.DllImport("User32.dll")] // [DllImport("User32.dll", EntryPoint = "GetWindowLong",CallingConvention = CallingConvention.Cdecl)] 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); //================================================================================= /// <summary> /// 设置窗体为无边框风格 /// </summary> /// <param name="hWnd"></param> public static void SetWindowNoBorder(IntPtr hWnd) { int oldstyle = (int)NativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_STYLE); oldstyle &= (int)(~(WS_CAPTION | WS_CAPTION_2)); SetWindowLong(hWnd, GWL_STYLE, (IntPtr)oldstyle); } }
转载请注明来自:闪闪的幸运星
原文地址:http://www.cnblogs.com/dongyang
如若转载,请保留原文地址。谢谢合作。