C# 獲取其他窗口程序控件,改變值,觸發(fā)事件
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
代碼如下: [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindowEx")] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam); const int WM_GETTEXT = 0x000D; const int WM_SETTEXT = 0x000C; const int WM_CLICK = 0x00F5; private void button3_Click(object sender, EventArgs e) { int retval = 0; //增加一個(gè)返回值用來判斷操作是否成功 //下面的這些參數(shù)都可以用Spy++查到 //string lpszParentClass = "#32770"; //整個(gè)窗口的類名 string lpszParentWindow = "Form1"; //窗口標(biāo)題 string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的類名,也就是輸入框 string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的類名 string lpszName_Submit = "確定"; //需要查找的Button的標(biāo)題 string text = ""; IntPtr ParenthWnd = new IntPtr(0); IntPtr EdithWnd = new IntPtr(0); //查到窗體,得到整個(gè)窗體 ParenthWnd = FindWindow(null, lpszParentWindow); //判斷這個(gè)窗體是否有效 if (!ParenthWnd.Equals(IntPtr.Zero)) { //得到User Name這個(gè)子窗體,并設(shè)置其內(nèi)容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); if (!EdithWnd.Equals(IntPtr.Zero)) { text = "Book"; //調(diào)用SendMessage方法設(shè)置其內(nèi)容 SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text); retval++; } //得到Password這個(gè)子窗體,并設(shè)置其內(nèi)容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); if (!EdithWnd.Equals(IntPtr.Zero)) { text = "ITest"; SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text); retval++; } //得到Domain這個(gè)子窗體,并設(shè)置其內(nèi)容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); if (!EdithWnd.Equals(IntPtr.Zero)) { text = "Ilove"; SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text); retval++; } //得到Button這個(gè)子窗體,并觸發(fā)它的Click事件 EdithWnd = FindWindowEx(ParenthWnd, (IntPtr)0, lpszClass_Submit, lpszName_Submit); if (!EdithWnd.Equals(IntPtr.Zero)) { SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0"); retval++; } } 該文章在 2023/12/29 23:45:06 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |