C#使用Automation實現(xiàn)控制自動撥打接聽電話
更新時間:2024年02月23日 16:40:44 作者:搬磚的詩人Z
這篇文章主要為大家詳細介紹了C#如何使用Automation實現(xiàn)控制自動撥打接聽電話,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習一下
效果圖
啟動找到第三方軟件的進程,并獲取到它的句柄
實現(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ù)傳入的路徑啟動相應(yīng)的可執(zhí)行程序,并返回進程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ù)進程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 = "沒有找到指定的窗口,請確認窗口已經(jīng)啟動!"; throw new InvalidProgramException(msg, ex); } }
定位button按鈕
///<summart> ///根據(jù)Button按鈕句柄,進行鼠標左鍵單擊 ///</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 = "鼠標左鍵單擊失??!"; 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> /// 點擊復(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實現(xiàn)控制自動撥打接聽電話的文章就介紹到這了,更多相關(guān)C# Automation控制自動撥打接聽電話內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
C#設(shè)計模式實現(xiàn)之生成器模式和責任鏈模式
學(xué)完設(shè)計模式之后,你就感覺它會慢慢地影響到你寫代碼的思維方式,下面這篇文章主要給大家介紹了關(guān)于C#設(shè)計模式實現(xiàn)之生成器模式和責任鏈模式的相關(guān)資料,需要的朋友可以參考下2021-08-08C# Winform實現(xiàn)導(dǎo)入和導(dǎo)出Excel文件
這篇文章主要為大家詳細介紹了C# Winform實現(xiàn)導(dǎo)入和導(dǎo)出Excel文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12