模拟鼠标移动程序实现——解决域控制器策略强制电脑锁屏问题

定时让鼠标微微动一下,浮动不要太大(移动一个像素就可以了),这样就达到持续让屏幕常亮的效果。以 Windows 系统和 C# 语言为例,C# 通过DllImport 调用 win32 关于鼠标控制的函数,DllImport 是 System.Runtime.InteropServices 命名空间下的属性类,要先引入该命名空间。

mouse_event 参数含义:

dwFlags: long 常量,鼠标操作类型,可组合(取 |) 
dx: x 轴移动像素 
dy: y 轴移动像素 
cButtons: 0 
dwExtroInfo: 0

dwFlags 常用取值:

MOUSEEVENTF_MOVE = 0x0001; //模拟鼠标移动 
MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下 
MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起 
MOUSEEVENTF_ABSOLUTE = 0x8000; //鼠标绝对位置 
MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下 
MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起 
MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下 
MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起

using System;
using System.Runtime.InteropServices;
using System.Timers;

namespace MouseMoveTimer
{
    class Program
    {
        [DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        private const int MOUSEEVENTF_MOVE = 0x0001;
        //[DllImport("user32")]
        //public static extern bool GetCursorPos(out Point pt);
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            // 1. 输入鼠标定时移动的时间间隔(秒)
            Console.WriteLine("请输入定时器的间隔时间秒数: ");
            string timerInterval = Console.ReadLine();

            // 2. 定义定时器
            int TimerInterval;
            if (Int32.TryParse(timerInterval, out TimerInterval))
            {
                Timer timer = new System.Timers.Timer(); // 定义定时器
                timer.Enabled = true; // 启用定时器
                timer.Interval = 1000 * TimerInterval; // 设定时间间隔(毫秒:1秒 = 1000毫秒)

                timer.Elapsed += new ElapsedEventHandler(MoveMouseHandler); // 等到间隔时间到,执行
                Console.WriteLine("成功干扰域控制器策略强制电脑锁屏,关闭后不干扰!");
            }
            else
            {
                Console.WriteLine("输入错误: 请输入数字!");
            }

            // 3. 保持程序不退出
            Console.ReadKey();
        }

        /// <summary>
        /// 定时器方法体 ,让鼠标沿 x、y 坐标移动一个像素
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected static void MoveMouseHandler(object sender, System.Timers.ElapsedEventArgs e)
        {
            //Point p;

            // 将鼠标移动一个像素
            mouse_event(MOUSEEVENTF_MOVE, 1, 1, 0, 0);

            //GetCursorPos(out p);
            //Console.WriteLine($"({p.X},{p.Y})");

        }
    }
}

 摘录  https://blog.csdn.net/water_0815/article/details/54959062

原文地址:https://www.cnblogs.com/shy1766IT/p/9504081.html