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

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

    public class WindowsAPI
    {
        ///  
        /// 回调函数代理 
        ///  
        public delegate bool CallBack(int hwnd, int lParam);
        public const int WM_GETTEXT = 0x000D;
        public const int WM_SETTEXT = 0x000C;
        /// 
        /// 鼠标click事件
        /// 
        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);

        /// 
        /// 获取活动窗体句柄
        /// 
        /// 
        [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

        /// 
        /// 获取枚举描述
        /// 
        /// 枚举值
        /// 描述信息
        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

你可能感兴趣的:(Windows API 查找窗体,发送Windows消息)