wpf软件模拟鼠标键盘操作

wpf编写的软件,模拟鼠标和键盘对别的软件执行外部操作

以下是示例代码,自动访问内网通用户执行给同事信息发送操作

xaml代码:

1 <Window x:Class="WpfRemote.MainWindow"
2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4         Title="MainWindow" Height="350" Width="525">
5     <Grid>
6         <Button Content="Button" HorizontalAlignment="Left" Margin="85,77,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
7 
8     </Grid>
9 </Window>
View Code

后台代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Runtime.InteropServices;
  5 using System.Text;
  6 using System.Windows;
  7 using System.Windows.Controls;
  8 using System.Windows.Data;
  9 using System.Windows.Documents;
 10 using System.Windows.Forms;
 11 using System.Windows.Input;
 12 using System.Windows.Media;
 13 using System.Windows.Media.Imaging;
 14 using System.Windows.Navigation;
 15 using System.Windows.Shapes;
 16 
 17 namespace WpfRemote
 18 {
 19     /// <summary>
 20     /// MainWindow.xaml 的交互逻辑
 21     /// </summary>
 22     public partial class MainWindow : Window
 23     {
 24         /// <summary>
 25         /// 寻找窗口
 26         /// </summary>
 27         /// <param name="strClass"></param>
 28         /// <param name="strWindow"></param>
 29         /// <returns></returns>
 30         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 31         static extern IntPtr FindWindow(string strClass, string strWindow);
 32 
 33         /// <summary>
 34         /// 将窗口放置在最上层
 35         /// </summary>
 36         /// <param name="hWnd"></param>
 37         /// <returns></returns>
 38         [DllImport("user32.dll")]
 39         static extern bool SetForegroundWindow(IntPtr hWnd);
 40 
 41 
 42 
 43         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 44         static extern int GetWindowTextLength(IntPtr hWnd);
 45 
 46 
 47         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 48         static extern int GetWindowText(IntPtr hWnd, string lpString, int cch);
 49 
 50         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 51         static extern IntPtr EnumChildWindows(IntPtr hWndParent, CallBack lpEnumFunc, int lParam);
 52 
 53         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 54         static extern int SetCursorPos(int x, int y);
 55 
 56 
 57         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 58         static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
 59         public const int MouseLeftDown = 0x2;
 60         public const int MouseLeftUp = 0x4;
 61 
 62 
 63         public MainWindow()
 64         {
 65             InitializeComponent();
 66         }
 67         public delegate bool CallBack(IntPtr hwnd, int lParam);
 68         private void Button_Click(object sender, RoutedEventArgs e)
 69         {
 70             IntPtr h;
 71             string strName = "李海波";
 72             h = FindWindow(null, strName);
 73             if (h.ToInt32() != 0)
 74             {
 75                 SetForegroundWindow(h);
 76                 SetCursorPos(300, 600);
 77                 mouse_event(MouseLeftDown | MouseLeftUp, 0, 0, 0, 0);
 78                 SendKeys.SendWait("你猜我在干嘛");
 79                 ////SendKeys.Send("测试信息");
 80                 SetCursorPos(750, 670);
 81                 mouse_event(MouseLeftDown | MouseLeftUp, 0, 0, 0, 0);
 82 
 83                 //SendKeys.SendWait("");
 84                 //SendKeys.SendWait("2");
 85                 //SendKeys.SendWait("*");
 86                 //SendKeys.SendWait("11");
 87                 //SendKeys.SendWait("=");
 88                 //CallBack myCall = new CallBack(EnumWindowsProc);
 89                 //EnumChildWindows(h, myCall, 0);
 90             }
 91         }
 92 
 93         public static int nCount;
 94         public static bool EnumWindowsProc(IntPtr h, int lparam)
 95         {
 96             string strText = new string((char)0, 255);
 97             RECT hRect = new RECT();
 98             nCount = nCount + 1;
 99             int Ret = 0;
100             Ret = GetWindowTextLength(h);
101 
102             string sSave = new string((char)9, Ret);
103             GetWindowText(h, sSave, Ret + 1);
104             if (sSave.IndexOf("2", 0) > -1)
105             {
106                 SetCursorPos(200, 160);
107 
108 
109                 return false;
110             }
111             return true;
112         }
113     }
114     public struct RECT
115     {
116         public int Left;
117         public int Top;
118         public int Right;
119         public int Bottom;
120     }
121 }
View Code
敲击键盘,创造价值
原文地址:https://www.cnblogs.com/weiweiboqi/p/14576240.html