• 评论

C#控制台模拟鼠标点击,最小化窗体功能

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AutoMouseMoNi
{
    class Program
    {
        [DllImport("User32.dll", EntryPoint = "ShowWindow")]   //
        private static extern bool ShowWindow(IntPtr hWnd, int type);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int dwData, 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;
        //模拟鼠标滚轮滚动操作,必须配合dwData参数
        const int MOUSEEVENTF_WHEEL = 0x0800;
        static void Main(string[] args)
        {
            GetHide();
            Thread.Sleep(5000);
            Thread ThreadMouseMoni = new Thread(new ThreadStart(MouseMoni));
            ThreadMouseMoni.Start();

            Console.ReadKey();
        }
        public static void TestMoveMouse(int x, int y)
        {
            Console.WriteLine("点击x:" + x + ",y:" + y + "," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE, x * 65535 / 1024, y * 65535 / 1280, 0, 0);//相对当前鼠标位置x轴和y轴分别移动50像素
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//模拟鼠标左键按下
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//模拟鼠标左键抬起
        }
        private static void MouseMoni()
        {
            try
            {
                int x = 0;
                int y = 0;
                string positions = ConfigurationManager.AppSettings["positions"];
                string count = ConfigurationManager.AppSettings["count"];
                int intCount = Convert.ToInt32(count);
                string[] arr = positions.Split(';');
                foreach (var item in arr)
                {
                    string[] poarr = item.Split(',');
                    if (poarr.Length == 2)
                    {
                         x =Convert.ToInt32(poarr[0]);
                         y = Convert.ToInt32(poarr[1]);
                        for (int i = 0; i < intCount; i++)
                        {
                            TestMoveMouse(x, y);
                            Thread.Sleep(1000);
                        }
                    }
                }
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            }

        }
        /// <summary>
        /// 隐藏控制台窗口
        /// </summary>
        private static void GetHide()
        {
            Console.Title = "TestStartHide";            //当前窗口的标题
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr et = new IntPtr(0);
            ParenthWnd = FindWindow(null, "TestStartHide");       //根据Title 找到对应窗口的句柄,进行操作
            ShowWindow(ParenthWnd, 2);//隐藏本dos窗体, 0: 后台执行;1:正常启动;2:最小化到任务栏;3:最大化
        }
    }
}


相关文章