使用C#制作小程序实现自动移动鼠标防止进入屏保

2次阅读
没有评论

论坛上看到一篇使用Autoit3制作小程序实现自动移动鼠标防止进入屏保的帖子,脚本语言打包方式容易引起一些安全提示,我就想那为啥不干脆换个方式。我简单翻了翻之前的帖子,发现主流的防屏保主要是动鼠标和动键盘,于是整合了一下,用C#复刻了一版,框架用的3.5,当时考虑win7自带3.5,后来发现win10默认不带3.5,就有点尴尬,所以有需要自行取用。
程序包后239K,对一个.NET程序来说当然有点大,主要原因是我为了提升体验,用了224K的大图标,如果想换成公司logo潜伏,可以自己改
方案一和方案二看起来没啥区别,其实写的时候有调用过不同的API,后来实测发现sendkeys对防屏保没用,就都切换成keybd_event,所以现在实际就是没啥区别,看着不舒服可以自己改
间隔时间我嫌麻烦就做成整数秒了,需要一呼一吸之间点下18次鼠标的,可以自己改
点鼠标本意是挂公司培训课时用的,可是我现在没啥需要点鼠标的挂课时的,所以写的很随意,暂时我也不打算完善,就这样先
虽然程序就等于源码,但由于我写的比较糙,我还是在网盘放了源码,我纯粹是因为觉得.NET的环境太开放才想着学习下C#,这也是我第一个C#程序,纯粹的野路子自己折腾,有啥问题也请大佬们不吝赐教,在此谢过。
检测进程那块纯纯是抄的代码,就不贴了
[C#] 纯文本查看 复制代码

using System;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Drawing;
namespace NoSleep_NET
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
notifyIcon1.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
}
static int timespan_sec = 200;
static bool scheme_1 = false;
static bool scheme_2 = false;
static bool scheme_3 = false;
static bool scheme_4 = false;
Thread othread = new Thread(No_Sleep);
[DllImport("user32")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("user32")]
public static extern void keybd_event(byte bVk,byte bScan,int dwFlags,int dwExtraInfo);
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//允许输入:数字、退格键(8)、全选(1)
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 1)
{
e.Handled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
timespan_sec = Convert.ToInt32(textBox1.Text);
scheme_1 = checkBox1.Checked;
scheme_2 = checkBox2.Checked;
scheme_3 = checkBox3.Checked;
scheme_4 = checkBox4.Checked;
othread.Abort();
this.Hide();
othread = new Thread(No_Sleep);
othread.Start();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
textBox1.Text = Convert.ToString(timespan_sec);
checkBox1.Checked = scheme_1;
checkBox2.Checked = scheme_2;
checkBox3.Checked = scheme_3;
checkBox4.Checked = scheme_4;
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (Visible)
{
this.Hide();
}
else
{
Show();
WindowState = FormWindowState.Normal;
}
}
private void 参数设置ToolStripMenuItem_Click(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
othread.Abort();
Application.Exit();
}
private static void No_Sleep()
{
while (true)
{
Thread.Sleep(timespan_sec * 1000);
if (scheme_1)
{
keybd_event(0x91, 0, 0, 0);
keybd_event(0x91, 0, 2, 0);
Thread.Sleep(10);
keybd_event(0x91, 0, 0, 0);
keybd_event(0x91, 0, 2, 0);
}
if (scheme_2)
{
keybd_event(0x90, 0, 0, 0);
keybd_event(0x90, 0, 2, 0);
Thread.Sleep(10);
keybd_event(0x90, 0, 0, 0);
keybd_event(0x90, 0, 2, 0);
}
if (scheme_3)
{
mouse_event(0x0001, 1, 0, 0, 0);
mouse_event(0x0001, -1, 0, 0, 0);
}
if (scheme_4)
{
mouse_event(0x0002, 0, 0, 0, 0);
mouse_event(0x0004, 0, 0, 0, 0);
}
}
}
}
}

链接:https://pan.baidu.com/s/1bO-yzjbnWeJO3ZyGg8Svdw
提取码:cr4y

正文完
 0
116博客
版权声明:本篇文章由 116博客 于2024-11-28发表,共计2793字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码