美文网首页
C# 使用Win32 API

C# 使用Win32 API

作者: Ritchie_Li | 来源:发表于2022-08-19 22:19 被阅读0次

引用:

using System.Runtime.InteropServices;

using WinForm = System.Windows.Forms;

鼠标键盘方法定义:

        //--------------------------

        //鼠标

        //----------------------

        // [System.Runtime.InteropServices.DllImport("User32")]

        // private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        //移动鼠标

        const int MOUSEEVENTF_MOVE = 0x0001;

        //模拟鼠标左键按下

        const int MOUSEEVENTF_LEFTDOWN = 0x0002;

        //模拟鼠标左键抬起

        const int MOUSEEVENTF_LEFTUP = 0x0004;

        //模拟鼠标右键按下

        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;

        //模拟鼠标右键抬起

        const int MOUSEEVENTF_RIGHTUP = 0x0010;

        //模拟鼠标中键按下

        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;

        //模拟鼠标中键抬起

        const int MOUSEEVENTF_MIDDLEUP = 0x0040;

        //标示是否采用绝对坐标

        const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        public const int MouseLeftDown = 0x2;

        public const int MouseLeftUp = 0x4;

        //---------------------------

        //键盘

        //---------------------------

        /// <summary>

        /// 寻找窗口

        /// </summary>

        /// <param name="strClass"></param>

        /// <param name="strWindow"></param>

        /// <returns></returns>

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        static extern IntPtr FindWindow(string strClass, string strWindow);

        /// <summary>

        /// 将窗口放置在最上层

        /// </summary>

        /// <param name="hWnd"></param>

        /// <returns></returns>

        [DllImport("user32.dll")]

        static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        static extern int GetWindowTextLength(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        static extern int GetWindowText(IntPtr hWnd, string lpString, int cch);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        static extern IntPtr EnumChildWindows(IntPtr hWndParent, CallBack lpEnumFunc, int lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

        static extern int SetCursorPos(int x, int y);

        public delegate bool CallBack(IntPtr hwnd, int lParam);

        public struct RECT

        {

            public int Left;

            public int Top;

            public int Right;

            public int Bottom;

        }

        public static int nCount;

        public static bool EnumWindowsProc(IntPtr h, int lparam)

        {

            string strText = new string((char)0, 255);

            //RECT hRect = new RECT();

            nCount = nCount + 1;

            int Ret = 0;

            Ret = GetWindowTextLength(h);

            string sSave = new string((char)9, Ret);

            GetWindowText(h, sSave, Ret + 1);

            if (sSave.IndexOf("2", 0) > -1)

            {

                SetCursorPos(200, 160);

                return false;

            }

            return true;

        }

测试输入英文字母:

private void btnKeyInput_Click(object sender, RoutedEventArgs e)

        {

            //英文输入

            this.rtxt1.Focus();

            //for (int i = 65; i < 91; i++) //A-Z

            //{

            //    char Letter = (char)i;

            //    WinForm.SendKeys.SendWait(Letter.ToString());

            //    //SendKeys.Send(Letter.ToString());

            //    System.Threading.Thread.Sleep(100);

            //    WinForm.SendKeys.Flush();

            //}

            for (int i = 97; i < 123; i++)//a-z

            {

                char Letter = (char)i;

                WinForm.SendKeys.SendWait(Letter.ToString());

                //SendKeys.Send(Letter.ToString());

                System.Threading.Thread.Sleep(100);

                WinForm.SendKeys.Flush();

                System.Threading.Thread.Sleep(100);

            }

        }

测试将字符串输入到文本文件:

private async Task ShowMessage(string msg)

        {

            await this.rtxt1.Dispatcher.BeginInvoke(new Action(() =>

            {

                rtxt1.AppendText($"{msg}\r\n");

                rtxt1.ScrollToEnd();

            }));

        }

        private async void btnMouse_Click(object sender, RoutedEventArgs e)

        {

            IntPtr h;

            string strName = "键盘输入.txt - 记事本";

            h = FindWindow(null, strName);

            await ShowMessage($"窗口找到:{h}");

            if (h.ToInt32() != 0)

            {

                SetForegroundWindow(h);

                await ShowMessage($"窗口设置为顶层窗口");

                SetCursorPos(300, 600);

                await ShowMessage($"移到鼠标位置(300,600)");

                mouse_event(MouseLeftDown | MouseLeftUp, 0, 0, 0, 0);

                await ShowMessage($"单击");

                await ShowMessage($@"输入字符串- D:\零基础学SQL.pdf");

                WinForm.SendKeys.SendWait(@"D:\零基础学SQL.pdf");

                System.Threading.Thread.Sleep(200);

                SetCursorPos(750, 670);

                mouse_event(MouseLeftDown | MouseLeftUp, 0, 0, 0, 0);

            }

        }

效果如下:

相关文章

网友评论

      本文标题:C# 使用Win32 API

      本文链接:https://www.haomeiwen.com/subject/lnxpgrtx.html