C# 通過(guò)窗口句柄為指定窗口發(fā)送指令(user32.dll,mshtml)
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
Spy++
點(diǎn)擊確認(rèn)可以看到詳細(xì)信息 點(diǎn)擊同步可以自動(dòng)定位到目錄樹上的位置 在目錄樹上就可以看到這個(gè)窗口下的所有控件的句柄信息 獲取常規(guī)窗口的句柄并傳輸命令//根據(jù)窗口類型或者窗口標(biāo)題獲取窗口句柄 [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//根據(jù)父窗口句柄、子窗口句柄、窗口類型、窗口標(biāo)題 獲取句柄 [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)] private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); /// <summary> /// 向指定句柄的窗口發(fā)送消息 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <param name="Msg">消息類型</param> /// <param name="wParam">消息1</param> /// <param name="lParam">消息2</param> /// <returns></returns> [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
//將指定句柄的窗口置頂 [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")] private static extern bool SetForegroundWindow(IntPtr hWnd); const int WM_SETTEXT = 0x000C; //為指定句柄的窗口設(shè)置文本信息 const int WM_CLICK = 0x00F5; //為指定句柄的窗口發(fā)送點(diǎn)擊事件 //根據(jù)窗口名稱獲取出啊港口句柄 IntPtr IPWnd = new IntPtr(0); IPWnd = FindWindow(null, "運(yùn)維客戶端"); if (IPWnd != IntPtr.Zero) { //根據(jù)找到的窗口 找到里面的輸入框 IntPtr txtWnd = new IntPtr(0); txtWnd = FindWindowEx(IPWnd, IntPtr.Zero, "Edit", null); if (txtWnd != IntPtr.Zero) { //如果找到了,就往里面輸入文本信息 SendMessage(txtWnd, WM_SETTEXT, IntPtr.Zero, "文本信息"); }
//對(duì)于存在相同類型的控件,尋找起來(lái)是相對(duì)麻煩一些的,因?yàn)樗麄儧](méi)有獨(dú)特的標(biāo)志,但是有獨(dú)自的index //如該例中的Button ,我們可以通過(guò)index獲取想要的控件句柄 //但是因?yàn)樵摪粹o是第一個(gè),所以默認(rèn)找到的就是它,可以不做特殊處理 //如果要按照INDEX獲取 可以使用下面的 FindWindowByIndex 方法 IntPtr buttonQRWnd = new IntPtr(0); buttonQRWnd = FindWindowEx(IPWnd, IntPtr.Zero, null, null); if (buttonQRWnd != IntPtr.Zero) { //將窗體置頂 點(diǎn)擊事件要確保被點(diǎn)擊的單位在最頂層,要不然點(diǎn)不到 SetForegroundWindow(IPWnd); //在任何操作之前最好等待一下,在這里主要避免置頂操作還未完成就觸發(fā)點(diǎn)擊事件,從而導(dǎo)致點(diǎn)擊失效 Thread.Sleep(150); //給按鈕發(fā)送點(diǎn)擊消息 SendMessage(buttonQRWnd, WM_CLICK, IntPtr.Zero, ""); } static IntPtr FindWindowByIndex(IntPtr hWndParent, int index) { if (index == 0) return hWndParent; else { int ct = 0; IntPtr result = IntPtr.Zero; do { result = FindWindowEx(hWndParent, result, "Button", null); if (result != IntPtr.Zero) ++ct; } while (ct < index && result != IntPtr.Zero); return result; } } 獲取內(nèi)嵌網(wǎng)頁(yè)的窗體中的元素,并發(fā)送消息//獲取主窗口句柄 IntPtr LoginWnd = new IntPtr(0); LoginWnd = FindWindow("OMClientPlus", null); if (LoginWnd != IntPtr.Zero) { //下面的操作都是為了找到最里面的控件的句柄 //也就是sehlln內(nèi)包含了shellView,shellView內(nèi)又包含了ies ,最終ies才是承載html的容器控件 //視情況按照Spy++中顯示的控件包含關(guān)系確認(rèn)最終的控件信息 IntPtr shell = FindWindowEx(LoginWnd,IntPtr.Zero, "Shell Embedding",null); Thread.Sleep(50); IntPtr shellview = FindWindowEx(shell, IntPtr.Zero, "Shell DocObject View", null); Thread.Sleep(50); IntPtr ies = FindWindowEx(shellview, IntPtr.Zero, "Internet Explorer_Server", null); Thread.Sleep(50);
//獲取容器控件中的HtmlDoucument mshtml.IHTMLDocument2 id = GetHtmlDocument(ies.ToInt32());
//獲取到所有的Input類型的組件 mshtml.IHTMLElementCollection inputs; inputs = (mshtml.IHTMLElementCollection)id.all.tags("input");
//找到用戶名 mshtml.IHTMLElement accountElement = (mshtml.IHTMLElement)inputs.item("u",0); mshtml.IHTMLInputElement accountInputElement = (mshtml.IHTMLInputElement)accountElement; accountInputElement.value = account; Thread.Sleep(150);
//找到密碼 mshtml.IHTMLElement passwordElement = (mshtml.IHTMLElement)inputs.item("p", 0); mshtml.IHTMLInputElement passwordInputElement = (mshtml.IHTMLInputElement)passwordElement; passwordInputElement.value = password; Thread.Sleep(150);
//點(diǎn)擊確認(rèn)按鈕 mshtml.IHTMLElement login = (mshtml.IHTMLElement)inputs.item("Login", 0); login.click(); } public mshtml.IHTMLDocument2 GetHtmlDocument(int hwnd) { System.Object domObject = new System.Object(); int tempInt = 0; System.Guid guidIEDocument2 = new Guid(); int WM_Html_GETOBJECT = Win32API.RegisterWindowMessage("WM_Html_GETOBJECT");//定義一個(gè)新的窗口消息 int W = Win32API.SendMessage(hwnd, WM_Html_GETOBJECT, 0, ref tempInt);//注:第二個(gè)參數(shù)是RegisterWindowMessage函數(shù)的返回值 int lreturn = Win32API.ObjectfromLresult(W, ref guidIEDocument2, 0, ref domObject); mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)domObject; return doc; } 該文章在 2023/12/16 11:20:24 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |