C# 獲取其他窗口程序控件,改變值,觸發事件
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
代碼如下: [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; //增加一個返回值用來判斷操作是否成功 //下面的這些參數都可以用Spy++查到 //string lpszParentClass = "#32770"; //整個窗口的類名 string lpszParentWindow = "Form1"; //窗口標題 string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的類名,也就是輸入框 string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的類名 string lpszName_Submit = "確定"; //需要查找的Button的標題 string text = ""; IntPtr ParenthWnd = new IntPtr(0); IntPtr EdithWnd = new IntPtr(0); //查到窗體,得到整個窗體 ParenthWnd = FindWindow(null, lpszParentWindow); //判斷這個窗體是否有效 if (!ParenthWnd.Equals(IntPtr.Zero)) { //得到User Name這個子窗體,并設置其內容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); if (!EdithWnd.Equals(IntPtr.Zero)) { text = "Book"; //調用SendMessage方法設置其內容 SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text); retval++; } //得到Password這個子窗體,并設置其內容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); if (!EdithWnd.Equals(IntPtr.Zero)) { text = "ITest"; SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text); retval++; } //得到Domain這個子窗體,并設置其內容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); if (!EdithWnd.Equals(IntPtr.Zero)) { text = "Ilove"; SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, text); retval++; } //得到Button這個子窗體,并觸發它的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 編輯過 |
關鍵字查詢
相關文章
正在查詢... |