一、目的
日常C#进行工控桌面应用程序开发过程中,不可避免的需要调用Win32 API, 每次都一番搜索之后,可能找到一个C描述的函数原型,还要进行转换,不少时候因为粗心敲错一个字母,自己给自己挖一个大坑。所以,在大量用到Win32 API的时候,我宁愿通过C/C++写个DLL,再用CLR Wapper成托管的库,供C#调用,不过一番折腾,也挺费劲的,前几天还是搜肠刮肚,包括借鉴网上的一些资源,搞了个C#的WIN32 API调用的静态库工具类。
方便自己,也方便Everyone!
二、直接上代码了
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace SparkWin32API
{
public static class Win32
{
#region AllowSetForegroundWindow
[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(int dwProcessId);
#endregion
#region CallNextHookEx
[DllImport("user32.dll")]
public static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);
#endregion
#region Const_Vars
public const uint WM_COPYDATA = 74u;
public const int WM_KEYDOWN = 256;
public const int WM_KEYUP = 257;
public const int WM_SYSKEYDOWN = 260;
public const int WM_NCLBUTTONDOWN = 161;
public const int HTCAPTION = 2;
public const int WH_CALLWNDPROCRET = 12;
public static int WM_SETREDRAW = 11;
public static int WS_EX_COMPOSITED = 33554432;
#endregion
#region DefWindowProc
[DllImport("user32.dll")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, IntPtr lParam);
#endregion
#region EndDialog
[DllImport("user32.dll")]
public static extern int EndDialog(IntPtr hDlg, IntPtr nResult);
#endregion
#region EnumWindows
public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
#endregion
#region EnumThreadWindows
public delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
#endregion
#region GetForegroundWindow
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
#endregion
#region GetProp
[DllImport("user32.dll")]
public static extern IntPtr GetProp(IntPtr hWnd, string lpString);
#endregion
#region GetWindowRect