C#使用Automation實(shí)現(xiàn)控制自動(dòng)撥打接聽電話
效果圖
啟動(dòng)找到第三方軟件的進(jìn)程,并獲取到它的句柄
實(shí)現(xiàn)代碼
int pid = 0; private void Form2_Load(object sender, EventArgs e) { Process[] ps = Process.GetProcessesByName("MicroSIP"); if (ps.Length > 0) { foreach (Process p in ps) p.Kill(); } pid = StartExe(@"D:\Users\lenovo\AppData\Local\MicroSIP\microsip.exe"); } private void Button_Click(object sender, EventArgs e) { Button btn = (Button)sender as Button; int btntext = Convert.ToInt32(btn.Text); ButtonLeftClick(btnList[btntext]); } private void button1_Click(object sender, EventArgs e) { automationElement = GetWindowHandle(pid, 1); var autoBtn = automationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Button")); btnList = autoBtn; var textBox_str = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ComboBox")); textBox1.Text = textBox_str.Current.Name; timer1.Start(); } ///<summary> ///根據(jù)傳入的路徑啟動(dòng)相應(yīng)的可執(zhí)行程序,并返回進(jìn)程ID ///</summary> public Int32 StartExe(string strExePath) { if (null == strExePath) { return 0; } Process ps = Process.Start(strExePath); Thread.Sleep(3000); return ps.Id; } ///<summary> ///根據(jù)進(jìn)程ID,查找相應(yīng)窗體,并返回窗體句柄 ///</summary> public AutomationElement GetWindowHandle(Int32 pid, int iWaitSecond) { AutomationElement targetWindow = null; int iWaitTime = 0; try { Process ps = Process.GetProcessById(pid); targetWindow = AutomationElement.FromHandle(ps.MainWindowHandle); while (null == targetWindow) { if (iWaitTime > iWaitSecond) { break; } Thread.Sleep(500); targetWindow = AutomationElement.FromHandle(ps.MainWindowHandle); } return targetWindow; } catch (System.Exception ex) { string msg = "沒有找到指定的窗口,請(qǐng)確認(rèn)窗口已經(jīng)啟動(dòng)!"; throw new InvalidProgramException(msg, ex); } }
定位button按鈕
///<summart> ///根據(jù)Button按鈕句柄,進(jìn)行鼠標(biāo)左鍵單擊 ///</summary> public static bool ButtonLeftClick(AutomationElement ButtonHandle) { object objButton = null; InvokePattern ivkpButton = null; try { if (null == ButtonHandle) { return false; } if (!ButtonHandle.TryGetCurrentPattern(InvokePattern.Pattern, out objButton)) { return false; } ivkpButton = (InvokePattern)objButton; ivkpButton.Invoke(); return true; } catch (System.Exception ex) { string msg = "鼠標(biāo)左鍵單擊失?。?; throw new InvalidProgramException(msg, ex); } }
定位復(fù)選框
/// <summary> /// 判斷復(fù)選框的值 /// </summary> /// <param name="element"></param> /// <returns></returns> private bool IsElementToggledOn(AutomationElement element) { if (element == null) { return false; } Object objPattern; TogglePattern togPattern; if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern)) { togPattern = objPattern as TogglePattern; return togPattern.Current.ToggleState == ToggleState.On; } return false; } /// <summary> /// 點(diǎn)擊復(fù)選框 /// </summary> /// <param name="element"></param> private void ClickToggledOn(AutomationElement element) { if (element == null) { // TODO: Invalid parameter error handling. return; } Object objPattern; TogglePattern togPattern; if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern)) { togPattern = objPattern as TogglePattern; togPattern.Toggle(); } } private void timer1_Tick(object sender, EventArgs e) { try { var status = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "msctls_statusbar32")); string name = status.Current.Name; label1.Text = name; var textBox_str = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ComboBox")); textBox1.Text = textBox_str.Current.Name; } catch (Exception ex) { } } private void button22_Click(object sender, EventArgs e) { Form3 form = new Form3(); form.ShowDialog(); var textBox_str = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ComboBox")); ValuePattern valuePattern = (ValuePattern)textBox_str.GetCurrentPattern(ValuePattern.Pattern); valuePattern.SetValue(Form3.textNumber); }
到此這篇關(guān)于C#使用Automation實(shí)現(xiàn)控制自動(dòng)撥打接聽電話的文章就介紹到這了,更多相關(guān)C# Automation控制自動(dòng)撥打接聽電話內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式
學(xué)完設(shè)計(jì)模式之后,你就感覺它會(huì)慢慢地影響到你寫代碼的思維方式,下面這篇文章主要給大家介紹了關(guān)于C#設(shè)計(jì)模式實(shí)現(xiàn)之生成器模式和責(zé)任鏈模式的相關(guān)資料,需要的朋友可以參考下2021-08-08C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)導(dǎo)入和導(dǎo)出Excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12C#實(shí)現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類,結(jié)合實(shí)例形式分析了C#針對(duì)日期的轉(zhuǎn)換與計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2017-06-06C# HttpClient Cookie驗(yàn)證解決方法
本文將詳細(xì)介紹C# HttpClient Cookie驗(yàn)證解決方法,需要了解的朋友可以參考下2012-11-11Unity實(shí)現(xiàn)新手引導(dǎo)鏤空效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)新手引導(dǎo)的鏤空效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02C# Onnx實(shí)現(xiàn)DIS高精度圖像二類分割
這篇文章主要為大家詳細(xì)介紹了C# Onnx實(shí)現(xiàn)DIS高精度圖像二類分割的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11