1、通过内置 Process 方式打开程序
1 Process m_Process = null;
2 m_Process = new Process();
3 m_Process.StartInfo.FileName = @“C:\test.exe”;
4 m_Process.Start();
2、通过win32 ,设置桌面鼠标位置,通过方法模拟鼠标双击事件,有点类似于那种外挂一样
复制代码
//在class下面放入以下方法
//绑定事件
[System.Runtime.InteropServices.DllImport(“user32”)]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
//定位
[DllImport(“User32”)]
public extern static void SetCursorPos(int x, int y);
//代码放在需要执行的地方
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004;
SetCursorPos(33, 28);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
3、shell32.dll 方法
复制代码
//class里面放入这段代码
[DllImport(“shell32.dll”)]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
//需要打开的地方插入此段代码
ShellExecute(IntPtr.Zero, new StringBuilder(“Open”), new StringBuilder(“test.exe”), new StringBuilder(“”), new StringBuilder(@“C:\文件夹名”), 1);
本文介绍了三种在C#中启动程序并操作桌面鼠标的方法:1)使用内置Process类启动程序;2)通过win32API设置鼠标位置并模拟鼠标双击事件;3)利用shell32.dll库执行程序。这些技术常用于自动化和测试场景。
9951

被折叠的 条评论
为什么被折叠?



