vs2012用wpf制作透明窗口中报错的解决方案
在开发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
如若转载,请保留原文地址。谢谢合作。
vs2012用wpf制作透明窗口中报错的解决方案的更多相关文章
- mysql修改root密码及修改密码过程中报错的解决方案
		
参考网站: https://www.linuxidc.com/Linux/2018-05/152586.htmhttps://www.cnblogs.com/wangbaobao/p/7087032. ...
 - 在eclipse中引入jquery.js文件报错的解决方案
		
从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在myeclipse中报错:jquery.js -> 鼠标右键 -> MyE ...
 - WPF制作的小型笔记本
		
WPF制作的小型笔记本-仿有道云笔记 楼主所在的公司不允许下载外部资源, 不允许私自安装应用程序, 平时记录东西都是用记事本,时间久了很难找到以前记的东西. 平时在家都用有道笔记, 因此就模仿着做了一 ...
 - WPF制作的小时钟
		
原文:WPF制作的小时钟 周末无事, 看到WEB QQ上的小时钟挺可爱的, 于是寻思着用WPF模仿着做一个. 先看下WEB QQ的图: 打开VS, 开始动工. 建立好项目后, 面对一个空荡荡的页面, ...
 - js 在myeclipse中报错
		
转myeclipse中的js文件报错 整理一下,希望帮到 遇到此问题的哥们.姐们. 方法一:myeclipse9 很特殊 和 myeclipse10 不一样,所以myeclipse9 不能使用该方 ...
 - 转帖:解决jquery.js在myeclipse中报错的问题
		
转载地址:http://tieba.baidu.com/p/1993753087 从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在my ...
 - laravel框架中报错 DataTables warning: table id=xxx-table - Cannot reinitialise DataTable.
		
laravel框架中报错 DataTables warning: table id=xxx-table - Cannot reinitialise DataTable. 分析: initializin ...
 - springboot项目中报错:listener does not currently know of SID given in connect descriptor
		
springboot项目中报错:listener does not currently know of SID given in connect descriptor 出现这个问题的原因是SID写错了 ...
 - WPF制作表示透明区域的马赛克画刷
		
最近在用WPF制作一款软件,需要像ps一样表示透明区域,于是制作了一个马赛克背景的style.实现比较简单,那么过程和思路就不表了,直接上代码 <DrawingBrush TileMode=&q ...
 
随机推荐
- 【Mood-7】tell 2 my gf-miss u not sudden but always
			
#sorry not coming 2 see u the national day holiday! I'm BULL in the land,fighting 4 u and babies! # ...
 - vim 实现 go to definition的功能
			
如:go to definition的功能,使用ctags配置步骤:1.创建vim ~/.vimrc2.配置vim属性 set number filetype plugin indent on ...
 - Android学习笔记④——页面的布局方式
			
FrameLayout(帧布局) 这个布局的特点是简单的默认把每一个视图组件都放在边框内且放在左上角,即使添加多个视图组件,他们也都是重叠在左上角,新的视图会遮挡住旧的视图.可以根据gravity来改 ...
 - 构建项目AppFuse+QuickStart
			
AppFuse是一个完整的框架来构建web应用程序.它最初是为了构建新的web应用程序少走弯路.多年来,它已成为一个非常成熟的可测试和创建基于java的web应用安全系统.在其核心,AppFuse是项 ...
 - Pascal 语言中二维数组:矩阵问题
			
[题目]方阵填数:在一个 N*N的方阵中,填入 1,2..... N*N个数,并要求构成如下格式: 图例:10 11 12 1 9 16 13 2 8 ...
 - nodejs6-npm包管理工具
			
npm search:在官方仓库中搜索包https://registry.npmjs.org npm view:查看官方仓库中的包 npm install:在当前目录下安装指定包 npm instal ...
 - Table of Contents - Servlet & JSP
			
Servlet Servlet API Overview Java Web 访问资源的路径问题 getParameter 与 request.getAttribute 的区别 转发与重定向的区别 中文 ...
 - 考上好大学,然后进入IT行业是穷人孩子晋级中产的唯一出路?
			
今天看到一篇不错的文章,内容如下: 这个问题来自于我在知乎上参与的一个热贴讨论,作为一个10年的老码农,创业公司呆过,二.三流的互联网公司混过,BAT也遛了一趟,如今再次回归创业,经历算得上狗血了,看 ...
 - Part 3  ViewData and ViewBag in mvc
			
ViewBag and ViewData is a mechanism(机制) to pass data from controller to view. We use '@' symbol(符号) ...
 - iOS中调用系统录音功能及其播放
			
最近做的项目中,用到了录音的功能,简单记录一下. 我的想法是:通过重写button的点击事件,来达到录音的目的. /*----------------------------------[录音]--- ...