Windows API 查找窗体,发送Windows消息

本文介绍了一种在Windows环境中进行消息截获和模拟发送的方法,通过使用.NET中的DllImport属性调用User32.dll库函数,实现了窗口查找、消息发送等功能。文中详细列举了常用的API函数及其用途,如FindWindow、SendMessage等,并提供了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目中需要做Windows消息截获操作,在网上找了一些资料。

    public class WindowsAPI
    {
        /// <summary> 
        /// 回调函数代理 
        /// </summary> 
        public delegate bool CallBack(int hwnd, int lParam);
        public const int WM_GETTEXT = 0x000D;
        public const int WM_SETTEXT = 0x000C;
        /// <summary>
        /// 鼠标click事件
        /// </summary>
        public const int WM_CLICK = 0x00F5;

        public const int WM_KEYUP = 0x0101;

        #region
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);


        [DllImport("user32.dll")]
        public static extern int GetLastActivePopup(int hWnd);
        [DllImport("user32.dll")]
        public static extern int AnyPopup();
        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")]
        public static extern int EnumThreadWindows(int dwThreadId, CallBack lpfn, int lParam);
        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBack lpfn, int lParam);
        [DllImport("user32.dll")]
        public static extern int EnumChildWindows(int hWndParent, CallBack lpfn, int lParam);

        /// <summary>
        /// 获取活动窗体句柄
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);


        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);

        #endregion

        /// <summary>
        /// 获取枚举描述
        /// </summary>
        /// <param name="enumValue">枚举值</param>
        /// <returns>描述信息</returns>
        public static string GetEnumDescription(Enum enumValue)
        {
            string str = enumValue.ToString();
            System.Reflection.FieldInfo field = enumValue.GetType().GetField(str);
            object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
            if (objs == null || objs.Length == 0) return str;
            System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0];
            return da.Description;

        }
    }

至于发送的消息可以通过“Spy++”工具抓取,然后模拟发送。

 

            var pHand = WindowsAPI.FindWindow(null, windowsName);//查找进程窗体
//查找子窗体
var tabControl = WindowsAPI.FindWindowEx(pHand, (IntPtr)0, "SysTabControl32", "");        //多页选择框,其中第三个参数是第几页,如0,1,2,3,4...
//4912会点击按钮,并切换到对应页。4876只会点按钮,不会切换页
var r = WindowsAPI.SendMessage(tabControl, 4912, 1, 0);//4912//4876 //发送点击事件 int clickPrintDoneOk = WindowsAPI.SendMessage(childHandPrintDoneOk, WindowsAPI.WM_CLICK, 0, 0);

 

转载于:https://www.cnblogs.com/LittleJin/p/5799252.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值