1.Form1 SOURCE CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;//启动模拟鼠标键盘操作
using System.Data.SqlClient;
namespace Reboot_CCBOOT_V1._00
{
public delegate bool CallBack(int hwnd, int lParam);
public partial class Form1 : Form
{
public List<String> Cfg = new List<String>();
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
//ShowWindow参数
private const int SW_SHOWNORMAL = 1;
private const int SW_RESTORE = 9;
private const int SW_SHOWNOACTIVATE = 4;
//SendMessage参数
private const int WM_KEYDOWN = 0X100;
private const int WM_KEYUP = 0X101;
private const int WM_SYSCHAR = 0X106;
private const int WM_SYSKEYUP = 0X105;
private const int WM_SYSKEYDOWN = 0X104;
private const int WM_CHAR = 0X102;
private const int WS_VISIBLE = 268435456;//窗体可见
private const int WS_MINIMIZEBOX = 131072;//有最小化按钮
private const int WS_MAXIMIZEBOX = 65536;//有最大化按钮
private const int WS_BORDER = 8388608;//窗体有边框
private const int GWL_STYLE = (-16);//窗体样式
private const int GW_HWNDFIRST = 0;
private const int GW_HWNDNEXT = 2;
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
private extern static int GetWindow(int hWnd, int wCmd);
[DllImport("User32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);
public Form1()
{
InitializeComponent();
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Location = new System.Drawing.Point(100, 100);
//获取当前工作宽度和高度(工作区不包含状态栏)
int ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
int ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;
//计算窗体显示的坐标值,可以根据需要微调几个像素
int x = ScreenWidth - this.Width - 5;
int y = ScreenHeight - this.Height - 5;
this.Location = new Point(x, y);
}
private void Form1_Load(object sender, EventArgs e)
{
String strFullPath = Application.ExecutablePath;//读取执行路径
String strFileName = System.IO.Path.GetFileName(strFullPath);//读取程式名称
String FileName = strFileName.Substring(0,strFileName.Length-4);
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(FileName);
if (myProcesses.Length > 1)//判断程式是否启动
{
MessageBox.Show("程式已启动", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
}
else
{
if (ReadCfg("Reboot_CCBOOT.config") == true)
{
timer1.Enabled = true;
}
else
{
MessageBox.Show("读取配置错误", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
}
}
}
public Boolean ReadCfg(String CfgFileName)//读取配置
{
Boolean Flag = false;
try
{
FileStream fs = new FileStream(CfgFileName, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs,Encoding.Default);
String Temp = String.Empty;
while ((Temp = sr.ReadLine()) != null)
{
if (Temp[0] != '#')
{
String[] ArrayStr = Temp.Split(new String[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
if (ArrayStr.Length > 1)
{
Cfg.Add(ArrayStr[1].Trim());
}
}
}
sr.Close();
fs.Close();
Flag = true;
}
catch (Exception ex)
{
WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss "+ex.ToString()));
MessageBox.Show("读取配置错误", "系统提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
}
return Flag;
}
public void BoolMouse(int x, int y)//执行模拟鼠标操作
{
SetCursorPos(x, y);
SendMouseEvent.Click();
}
public void WriteLog(String ErrText)//写错误日志
{
FileStream fs = new FileStream("Err.log",FileMode.Append,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(ErrText);
sw.Close();
fs.Close();
}
//判断进程是否启动
public Boolean RedProcess(String RunBathName)
{
Boolean Flag = false;
Process[] pro = Process.GetProcesses();//获取已开启的所有进程
for (int i = 0; i < pro.Length; i++)
{
if (pro[i].ProcessName.ToString().ToString() == RunBathName)
{
Flag = true;
break;
}
}
return Flag;
}
//调用执行文件
public Boolean CallBath(String CallBathName)
{
Process proc = null;
string targetDir = string.Empty;
targetDir = System.IO.Directory.GetCurrentDirectory() + @"\";
Boolean Flag = false;
try
{
proc = new Process();
proc.StartInfo.WorkingDirectory = targetDir;
proc.StartInfo.FileName = CallBathName;
proc.Start();
proc.WaitForExit();
Flag = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
System.Environment.Exit(1);
Flag = false;
}
return Flag;
}
//关闭进程式
public void KillProcess(string RunBathName)
{
Process[] pro = Process.GetProcesses(); //获取已开启的所有进程
for (int i = 0; i < pro.Length; i++)
{
//判断此进程是否是要查看的进程
if (pro[i].ProcessName.ToString().ToString() == RunBathName)
{
pro[i].Kill();//结束进程
}
}
}
//public String TitleName = String.Empty;
public Boolean GetRunWindowsText()//根据窗体句柄获取窗体名称
{
//StringBuilder s = new StringBuilder(512);
//if (GetWindowText(02098286, s, s.Capacity) == false)
// return false;
//TitleName = s.ToString();
IntPtr hwnd = FindWindow(null,Cfg[2]);
if (hwnd.ToString() == "0") return false;
return true;
}
public void CpyCCbootini()//制复CCboot.ini文件
{
string targetDir = string.Empty;
targetDir = System.IO.Directory.GetCurrentDirectory() + @"\CCBoot.ini";
File.Copy(targetDir,@"C:\CCBoot\CCBoot.ini",true);
}
public uint Boot_i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (GetRunWindowsText() == false)
{
timer1.Enabled = false;
KillProcess(Cfg[3]);
CpyCCbootini();
CallBath(Cfg[0]);
timer2.Enabled = true;
}
else
{
label2.Text = "程式查询" + Boot_i.ToString() + "数";
Boot_i++;
}
}
public uint Intercept = 0;
private void timer2_Tick(object sender, EventArgs e)
{
Intercept++;
label1.Text = "拦截重启动"+Intercept.ToString()+"次CCBOOT";
this.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss " + "拦截重启动" + Intercept.ToString() + "次CCBOOT"));
timer2.Enabled = false;
Boot_i = 0;
timer1.Enabled = true;
}
private void Form1_Shown(object sender, EventArgs e)//点击显示窗体
{
this.BeginInvoke(new Action(() =>
{
this.Hide();
this.Opacity = 1;
}));
}
}
}
2.SendMouseEvent SOURCED CODE:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Reboot_CCBOOT_V1._00
{
public class SendMouseEvent : Form1
{
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[Flags]
public enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
public static void Send(MouseEventFlag mouseEventFlag, int dx, int dy, uint dwData)
{
mouse_event(mouseEventFlag | MouseEventFlag.Absolute, dx, dy, dwData, UIntPtr.Zero);
}
public static void MoveTo(uint scceenTop, uint screenLeft)
{
int x = scceenTop == 0 ? 0 : (int)((float)scceenTop / (float)Screen.PrimaryScreen.Bounds.Height * (float)65535);
int y = screenLeft == 0 ? 0 : (int)((float)screenLeft / (float)Screen.PrimaryScreen.Bounds.Width * (float)65535);
mouse_event(MouseEventFlag.Move | MouseEventFlag.Absolute, x, y, 0, UIntPtr.Zero);
}
public static void Click()
{
LeftDown(); LeftUp();
}
public static void DoubleClick()
{
Click(); Click();
}
public static void LeftDown()
{
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
}
public static void LeftUp()
{
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
public static void RightDown()
{
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
}
public static void RightUp()
{
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
public static void MiddleDown()
{
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
}
public static void MiddleUp()
{
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
}
public class FromInfo : Form1
{
private String title;
private int handle;
public FromInfo(String title, int handle)
{
this.title = title;
this.handle = handle;
}
public String Title
{
get { return title; }
set { title = value; }
}
public int Handle
{
get { return handle; }
set { handle = value; }
}
}
}
3.CONFIG:
4.程式执行窗面: