很無奈,寫了很長時間,最后保存時網頁失去響應,真是要命呢。本來想就此放棄了,但是想還是粗略的重寫一次吧,希望日后可以對朋友有一定的幫助。
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 else25 {26 //MessageBox.Show("沒有找到下拉框");27 }28 29 30 MessageBox.Show("已經找到窗體");31 }32 else33 {34 MessageBox.Show("沒有找到窗體");35 }36 }
執行結果如下:
如果多個按鈕,又沒有標題,則只能一個一個的獲取,如下
如果哪位朋友還有其它方法,還請多多指教。
1 IntPtr button1 = FindWindowEx(win, new IntPtr(), "Button", null);2 IntPtr button2 = FindWindowEx(win, button1, "Button", null);
新聞熱點
疑難解答