[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);
}
}
}
}
}