C#之操作窗口模拟键鼠事件文件监控等知识使用

实现的效果图片,这里我在输入框输入一个号码,在C盘下创建一个文件txt文件,
被filewatch监控到,里面往指定窗口送sn被输入一些键盘鼠标的动作等操作;通过窗口句柄的操作发现窗口;





主要代码贴出来,

public partial class Form1 : Form{public Form1(){InitializeComponent();this.fileSystemWatcher1.Path = @"C:\ITF\XMLCEXPORT\";}//文件改变事件private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e){string message = "文件:" + e.FullPath + " " + "改变于:" + DateTime.Now;if (!File.Exists("D:\\log.txt")){FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write);using (StreamWriter sw = new StreamWriter(fs1)){sw.WriteLine(message);}// ||//StreamWriter sw = new StreamWriter(fs1);//sw.WriteLine(message);//sw.Close();fs1.Close();}else{FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write);//FileMode.Append继续写入using (StreamWriter sw2 = new StreamWriter(fs2)){sw2.WriteLine(message);}//StreamWriter sw2 = new StreamWriter(fs2);//sw2.WriteLine(message);//sw2.Close();fs2.Close();}//CopyDiretory(this.txtWatchFiles.Text.Trim(), this.txtGoalFiles.Text.Trim());}//文件创建事件private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e){//FtpHelper ftpHelper = new FtpHelper(this.txtipAdd.Text.Trim(), this.txtUserName.Text.Trim(), this.txtPass.Text.Trim());string message = "文件:" + e.FullPath + " " + "创建与:" + DateTime.Now;Console.WriteLine(e.Name.ToString());Console.WriteLine(e.Name.ToString() + "=======哈哈哈");if (!File.Exists("D:\\log.txt")){FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write);using (StreamWriter sw = new StreamWriter(fs1)){sw.WriteLine(message);}fs1.Close();}else{FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write);using (StreamWriter sw2 = new StreamWriter(fs2)){sw2.WriteLine(message);}fs2.Close();}//CopyDiretory(this.txtWatchFiles.Text.Trim(), this.txtGoalFiles.Text.Trim());//下载//ftpHelper.Download("java.rar", "D:\\java1.rar");//上传//FileInfo fileInfo = new FileInfo(e.FullPath.ToString());//Thread.Sleep(100);//string s = e.Name.ToString();//string lastname = s.Substring(s.LastIndexOf('.'));//string firsts = s.Substring(0, s.LastIndexOf('.'));//string dataName = DateTime.Now.ToString("mmddss");//string finalName = firsts + "_" + dataName + lastname;//string ftpath = DateTime.Now.ToString("yyyyMMdd");//ftpHelper.Upload(fileInfo, "\\" + ftpath + "\\" + finalName);//  ftpHelper.Upload(fileInfo, "\\"  + finalName);string lastname = e.Name.ToString().Substring(0,e.Name.ToString().LastIndexOf('_'));//  MessageBox.Show(lastname);IntPtr awin = MouseHookHelper.FindWindow("WindowsForms10.Window.8.app.0.141b42a_r10_ad1", "Repair");if (awin == IntPtr.Zero){MessageBox.Show("没有找到窗体");return;}MouseHookHelper.RECT rect = new MouseHookHelper.RECT();MouseHookHelper.GetWindowRect(awin, ref rect);int width = rect.Right - rect.Left;             //窗口的宽度int height = rect.Bottom - rect.Top;            //窗口的高度int x = rect.Left;int y = rect.Top;// MessageBox.Show("窗口的宽度 {0}" + width + " 窗口的高度 " + height + " 距离x " + x + " 距离 y " + y);// 3.设置为当前窗体MouseHookHelper.SetForegroundWindow(awin);MouseHookHelper.ShowWindow(awin, MouseHookHelper.SW_SHOWNOACTIVATE);//4、5Clipboard.SetText(lastname);Clipboard.GetText();SendKeys.SendWait("^V");Thread.Sleep(1000);SendKeys.Send("{TAB}");Thread.Sleep(1000);//   Clipboard.SetText("NTF");//从剪贴板获取数据//   Clipboard.GetText();//  Thread.Sleep(100);SendKeys.Send("{N}");Thread.Sleep(100);SendKeys.Send("{N}");Thread.Sleep(100);SendKeys.Send("{F}");Thread.Sleep(100);SendKeys.Send("{Enter}");}//文件删除事件private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e){string message = "文件:" + e.FullPath + " " + "删除于:" + DateTime.Now;if (!File.Exists("D:\\log.txt")){FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write);using (StreamWriter sw = new StreamWriter(fs1)){sw.WriteLine(message);}fs1.Close();}else{FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write);using (StreamWriter sw2 = new StreamWriter(fs2)){sw2.WriteLine(message);}fs2.Close();}}//文件重命名事件private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e){//string message = "文件:" + e.FullPath + " " + "重命名于:" + DateTime.Now;string message = "文件:" + e.OldFullPath + "由原来名" + e.OldName + "改为:" + e.Name + " " + "重命名于:" + DateTime.Now;if (!File.Exists("D:\\log.txt")){FileStream fs1 = new FileStream("D:\\log.txt", FileMode.Create, FileAccess.Write);using (StreamWriter sw = new StreamWriter(fs1)){sw.WriteLine(message);}fs1.Close();}else{FileStream fs2 = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write);using (StreamWriter sw2 = new StreamWriter(fs2)){sw2.WriteLine(message);}fs2.Close();}//CopyDiretory(this.txtWatchFiles.Text.Trim(), this.txtGoalFiles.Text.Trim());}private void button4_Click(object sender, EventArgs e){IntPtr awin = MouseHookHelper.FindWindow("WindowsForms10.Window.8.app.0.141b42a_r10_ad1", "Repair");if (awin == IntPtr.Zero){MessageBox.Show("没有找到窗体");return;}MouseHookHelper.RECT rect = new MouseHookHelper.RECT();MouseHookHelper.GetWindowRect(awin, ref rect);int width = rect.Right - rect.Left;             //窗口的宽度int height = rect.Bottom - rect.Top;            //窗口的高度int x = rect.Left;int y = rect.Top;MessageBox.Show("窗口的宽度 {0}"+ width  + " 窗口的高度 "+ height +" 距离x "+ x +" 距离 y " + y);// 3.设置为当前窗体MouseHookHelper.SetForegroundWindow(awin);MouseHookHelper.ShowWindow(awin, MouseHookHelper.SW_SHOWNOACTIVATE);//4、5Clipboard.SetText("NTF");//从剪贴板获取数据Clipboard.GetText();//粘贴SendKeys.SendWait("^V");Thread.Sleep(100);SendKeys.Send("{TAB}");Thread.Sleep(100);SendKeys.Send("{Enter}");}private void button1_Click(object sender, EventArgs e){if (!File.Exists("C:\\ITF\\XMLCEXPORT\\" + this.textBox1.Text.Trim().ToString() + ".txt")){FileStream fs1 = new FileStream("C:\\ITF\\XMLCEXPORT\\"+ this.textBox1.Text.Trim().ToString() +"_"+ DateTime.Now.ToString("mmddss") +".txt", FileMode.Create, FileAccess.Write);}}}

MouseHookHelper.cs类的代码

public  class MouseHookHelper{#region 根据句柄寻找窗体并发送消息[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]//参数1:指的是类名。参数2,指的是窗口的标题名。两者至少要知道1个public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam);[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);#endregion#region 获取窗体位置[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);[StructLayout(LayoutKind.Sequential)]public struct RECT{public int Left;                             //最左坐标public int Top;                             //最上坐标public int Right;                           //最右坐标public int Bottom;                        //最下坐标}#endregion#region 设置窗体显示形式public enum nCmdShow : uint{SW_NONE,//初始值SW_FORCEMINIMIZE,//:在WindowNT5.0中最小化窗口,即使拥有窗口的线程被挂起也会最小化。在从其他线程最小化窗口时才使用这个参数。SW_MIOE,//:隐藏窗口并激活其他窗口。SW_MAXIMIZE,//:最大化指定的窗口。SW_MINIMIZE,//:最小化指定的窗口并且激活在Z序中的下一个顶层窗口。SW_RESTORE,//:激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志。SW_SHOW,//:在窗口原来的位置以原来的尺寸激活和显示窗口。SW_SHOWDEFAULT,//:依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的。SW_SHOWMAXIMIZED,//:激活窗口并将其最大化。SW_SHOWMINIMIZED,//:激活窗口并将其最小化。SW_SHOWMINNOACTIVATE,//:窗口最小化,激活窗口仍然维持激活状态。SW_SHOWNA,//:以窗口原来的状态显示窗口。激活窗口仍然维持激活状态。SW_SHOWNOACTIVATE,//:以窗口最近一次的大小和状态显示窗口。激活窗口仍然维持激活状态。SW_SHOWNOMAL,//:激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。}public const int SW_HIDE = 0;public const int SW_SHOWNORMAL = 1;public const int SW_SHOWMINIMIZED = 2;public const int SW_SHOWMAXIMIZED = 3;public const int SW_MAXIMIZE = 3;public const int SW_SHOWNOACTIVATE = 4;public const int SW_SHOW = 5;public const int SW_MINIMIZE = 6;public const int SW_SHOWMINNOACTIVE = 7;public const int SW_SHOWNA = 8;public const int SW_RESTORE = 9;[DllImport("User32.dll")]public static extern bool SetForegroundWindow(IntPtr hWnd);[DllImport("User32.dll")]public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);#endregion#region 控制鼠标移动//移动鼠标 public const int MOUSEEVENTF_MOVE = 0x0001;//模拟鼠标左键按下 public const int MOUSEEVENTF_LEFTDOWN = 0x0002;//模拟鼠标左键抬起 public const int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标右键按下 public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;//模拟鼠标右键抬起 public const int MOUSEEVENTF_RIGHTUP = 0x0010;//模拟鼠标中键按下 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;//模拟鼠标中键抬起 public const int MOUSEEVENTF_MIDDLEUP = 0x0040;//标示是否采用绝对坐标 public const int MOUSEEVENTF_ABSOLUTE = 0x8000;[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}//[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)][DllImport("user32.dll")]public static extern bool SetCursorPos(int X, int Y);[DllImport("user32.dll")]public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);#endregion#region 获取坐标钩子[StructLayout(LayoutKind.Sequential)]public class POINT{public int X;public int Y;}[StructLayout(LayoutKind.Sequential)]public class MouseHookStruct{public POINT pt;public int hwnd;public int wHitTestCode;public int dwExtraInfo;}public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);//安装钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);//卸载钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern bool UnhookWindowsHookEx(int idHook);//调用下一个钩子[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);#endregion}

C#之操作窗口模拟键鼠事件文件监控等知识使用相关推荐

  1. C++模拟键盘操作窗口入门

    C++模拟键盘操作窗口入门之文件监控操作句柄操作 #include <iostream> #include <windows.h> #include <tchar.h&g ...

  2. AutoRunner模拟键鼠操作

    在AR的使用过程中,有些地方需要用到模拟键盘操作,这种键盘的模拟操作命令有很多,具体请见AR界面的:[帮助–脚本命令].那么今天我们一起看一些模拟键盘操作的命令和其使用: 1.inputDown 命令 ...

  3. C#和C常用的API操作窗口的代码积累

    C#和C常用的API操作窗口的代码积累 IntPtr awin = MouseHookHelper.FindWindow("WeChatMainWndForPC", "微 ...

  4. python模拟鼠标点击和键盘输入的操作_python模拟鼠标点击和键盘输入的操作

    所有代码都是网上百度出来的,通过个人实践找到适合自己的. 采用的python 库是 pymouse.pykeyboard 安装时直接pip安装的,pip install PyUserInput 安装不 ...

  5. 通过获取窗口句柄模拟键鼠点击的一些方法

    目的: 实现能自动在窗口中实现鼠标点击与键盘输入的操作(以QQ自动发送某些为例) 语言: C/C++ 实现原理: 一.窗口句柄(HWND): [1] 每个程序窗口都有一个窗口句柄,在程序的一次运行中句 ...

  6. access根据所属院系修改学号_国家二级ACCESS机试(操作题)模拟试卷437

    国家二级ACCESS机试(操作题)模拟试卷437 (总分:6.00,做题时间:90分钟) 一.基本操作题(总题数:1,分数:2.00) 1.在考生文件夹下,"sampl.accdb" ...

  7. windows常见窗口样式和控件风格以及类样式

    Windows常见窗口样式和控件风格 王佰营 徐丽红 一.窗口样式 WS_POPUP 弹出式窗口(不能与WS_CHILDWINDOW样式同时使用) WS_CHILDWINDOW 子窗口(不能与WS_P ...

  8. Windows常见窗口样式和控件风格

    Windows常见窗口样式和控件风格 王佰营 徐丽红 一.窗口样式 WS_POPUP 弹出式窗口(不能与WS_CHILDWINDOW样式同时使用) WS_CHILDWINDOW 子窗口(不能与WS_P ...

  9. 用回车键实现MFC对话框中TAB键控件输入焦点在控件中跳转的效果(转)

    用回车键实现MFC对话框中TAB键控件输入焦点在控件中跳转的效果(转) 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://hcq11.blogbus.com/logs/5 ...

最新文章

  1. LayoutParams cannot be resolved to a type
  2. C语言重点——指针篇(一篇让你完全搞懂指针)
  3. 《JUnit实战(第2版)》—第1章1.7节小结
  4. python如何调用字典_如何访问python字典里的值?(实例解析)
  5. 关于将一个数输出为原码、反码和补码的解惑
  6. kafka和flume进行整合的日志采集的confi文件编写
  7. HTTP的长链接和短链接说明
  8. c++中的继承--2(继承中的析构函数和构造函数,继承中同名成员,继承中静态成员)
  9. 现代程序设计 作业 第1次
  10. 不做会死!互联网时代的企业必定都要实现中台
  11. linux ulimit
  12. ASP.NET 安全性
  13. python socket文件传输
  14. java互喷群,夸夸群和喷喷群的心里需求和投资如此相似
  15. 定时任务执行利器Timer和ScheduledThreadPoolExecutor使用
  16. c语言p1-melepeo,C语言做个学生选课系统 -电脑资料
  17. virtualbox虚拟机安装win10详细教程
  18. react-app-rewired 修改 react 项目默认端口号
  19. python文件写入后,但文件还是空白
  20. 什么是Java的反射机制

热门文章

  1. mysql 360怎么安装_MySQL安装方法
  2. linux通过字符串查看,linux命令strings有什么用?查找二进制文件可打印字符串。...
  3. 怎么把文件导入python_如何导入其他Python文件?
  4. python3.5安装教程linux_linux安装python3.5
  5. id3决策树_信息熵、信息增益和决策树(ID3算法)
  6. 简述一下动态sql的执行原理_简述一下Pogo Pin电连接器的设计原理和考虑因素
  7. 物联网第一台设备是烤面包机?
  8. 【视频】如何正确焊接贴片、直插元件?
  9. zipentry压缩乱码linux,java生成压缩文件 ZipEntry出现乱码的有关问题
  10. 学校为什么要单位接收函_学校为什么要做校园文化建设?