在开发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)方法,问题的关键就出在这里,后有修改了类代码,完美运行,不再报错,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
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
如若转载,请保留原文地址。谢谢合作。