P /从C#调用UpdateLayeredWindowIndirect()时遇到问题

时间:2016-10-14 10:45:04

标签: c# windows winapi pinvoke user32

目前我的应用程序使用了user32.dll中的UpdateLayeredWindow()函数并且它运行良好,但我试图用UpdateLayeredWindowIndirect()调用来替换它以利用{{1} UPDATELAYEREDWINDOWINFO结构中的标志。

目前,UpdateLayeredWindow() P / invoke按如下方式完成:

nameField = JOptionPane.showInputDialog(null, "Enter Username(Default is 000000");

变量类型与委托匹配,函数调用按预期工作。

然后我尝试用UpdateLayeredWindowIndirect()替换呼叫,如下所示:

prcDirty

在控制台上,我收到消息:[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref BLENDFUNCTION pblend, BlendFlags dwFlags); [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BLENDFUNCTION { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; } public enum BlendFlags : uint { None = 0x00, ULW_COLORKEY = 0x01, ULW_ALPHA = 0x02, ULW_OPAQUE = 0x04 } //UpdateLayeredWindow() called here public UpdateMethodHere() { UpdateLayeredWindow(handle, hdcWindow, ref dest, ref size, hDC, ref src, 0, ref blend, BlendFlags.ULW_ALPHA); } 。我尝试将SIZE结构变量更改为[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] public static extern bool UpdateLayeredWindowIndirect(IntPtr hwnd, ref UPDATELAYEREDWINDOWINFO updateInfo); [StructLayout(LayoutKind.Sequential)] public struct UPDATELAYEREDWINDOWINFO { public static readonly int Size = Marshal.SizeOf(typeof(UPDATELAYEREDWINDOWINFO)); public int cbSize; public IntPtr hdcDst; public POINT pptDst; public SIZE psize; public IntPtr hdcSrc; public POINT pptSrc; public int crKey; //BLENDFUNCTION is the same as in the first part of the question public BLENDFUNCTION pblend; //BlendFlags is the same as in the first part of the question public BlendFlags dwFlags; public RECT prcDirty; } [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { this.X = x; this.Y = y; } } [StructLayout(LayoutKind.Sequential)] public struct SIZE { public long cx; public long cy; public SIZE(long width, long height) { cx = width; cy = height; } } [StructLayout(LayoutKind.Sequential)] public struct RECT { public int left; public int top; public int right; public int bottom; public RECT(int left, int top, int width, int height) { this.left = left; this.top = top; right = left + width; bottom = top + height; } } //UpdateLayeredWindowIndirect() called here public UpdateMethodHere() { UPDATELAYEREDWINDOWINFO info = new UPDATELAYEREDWINDOWINFO() { //the variables on the right-hand size used here are the same as the //ones used in part 1 of the question cbSize = UPDATELAYEREDWINDOWINFO.Size, hdcDst = hdcWindow, pptDst = new POINT(dest.X, dest.Y), psize = new SIZE(size.Width, size.Height), hdcSrc = hDC, pptSrc = new POINT(src.X, src.Y), crKey = 0, pblend = blend, dwFlags = BlendFlags.ULW_ALPHA, prcDirty = new RECT(src.X, src.Y, size.Width, size.Height) }; UpdateLayeredWindowIndirect(handle, ref info); Console.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message); } 类型而不是The parameter is incorrect,但我在控制台中收到int消息。

是否有人能够发现与UpdateLayeredWindowIndirect() P / invoke相关的委托和/或结构签名不正确的内容?

0 个答案:

没有答案