C# 调用Win32 API的工具类

一、目的

日常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
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值