c#使用spy进行模拟操作

很无奈,写了很长时间,最后保存时网页失去响应,真是要命呢。本来想就此放弃了,但是想还是粗略的重写一次吧,希望日后可以对朋友有一定的帮助。

Microsoft.Spy工具是一个基础工具,我们简要介绍一下使用方法:

spy在vs有自带的,也可以在网下直接下载。

打开spy工具,主界面如下:

今天我们使用vnc作为一个示例

目标是在server内写入192.168.2.200,并点击Options第二个按钮

第一步,如何获取vnc窗体,使用spy进行窗体查找

拖动查找工具图标到需要的界面上。

这样我们就可以找到需要的窗体。

FindWindow 可以查找第一个主窗体,使和类名或标题。

FindWindowEx可以查找窗体下的控件。

SendMessage向窗体发送消息。

使和窗口搜索查找控件。

 1  [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
 2         //参数1:指的是类名。参数2,指的是窗口的标题名。两者至少要知道1个
 3         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 4 
 5 
 6         [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
 7         public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
 8 
 9         [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
10         public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam);
11 
12         [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
13         public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

 

 1 IntPtr win =FindWindow(null, "VNC Viewer : Connection Details");
 2              if (win != IntPtr.Zero)
 3              {
 4                  IntPtr winComboBox = FindWindowEx(win, new IntPtr(), "ComboBox", null);
 5                  if (winComboBox != IntPtr.Zero)
 6                  {
 7                      IntPtr winEdit = FindWindowEx(winComboBox, new IntPtr(), "Edit", null);
 8 
 9                      IntPtr resultEdit = SendMessage(winEdit, 0xC, 0, "192.168.2.100");
10 
11 
12                      //获取第一个按钮
13                      IntPtr button1 = FindWindowEx(win, new IntPtr(), "Button", "&Options...");
14 
15                      if(button1 != IntPtr.Zero)
16                         SendMessage(button1, 0xF5, 0, 0); //点击事件
17 
18                      if (winEdit != IntPtr.Zero)
19                      {
20                          MessageBox.Show("找到编辑框");
21                      }
22                      //MessageBox.Show("找到下拉框");
23                  }
24                  else
25                  {
26                      //MessageBox.Show("没有找到下拉框");
27                  }
28 
29 
30                  MessageBox.Show("已经找到窗体");
31              }
32              else
33              {
34                  MessageBox.Show("没有找到窗体");
35              }
36         }


执行结果如下:

 

 

如果多个按钮,又没有标题,则只能一个一个的获取,如下

如果哪位朋友还有其它方法,还请多多指教。

1 IntPtr button1 = FindWindowEx(win, new IntPtr(), "Button", null);
2 IntPtr button2 = FindWindowEx(win, button1, "Button", null);

 

posted @ 2014-09-19 01:16  迟来的春天  阅读(7905)  评论(9编辑  收藏  举报