日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發布時間:2023/12/18 C# 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#之操作窗口模拟键鼠事件文件监控等知识使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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#之操作窗口模拟键鼠事件文件监控等知识使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。