C#使用Automation實(shí)現(xiàn)控制自動(dòng)撥打接聽(tī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 = "沒(méi)有找到指定的窗口,請(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)撥打接聽(tīng)電話的文章就介紹到這了,更多相關(guān)C# Automation控制自動(dòng)撥打接聽(tīng)電話內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中前臺(tái)線程和后臺(tái)線程的區(qū)別與聯(lián)系
這篇文章主要介紹了C#中前臺(tái)線程和后臺(tái)線程的區(qū)別與聯(lián)系,本文先講解了它們的區(qū)別,然后給出了一個(gè)例子來(lái)驗(yàn)證這些區(qū)別,需要的朋友可以參考下2015-06-06
C#實(shí)現(xiàn)頁(yè)面GZip或Deflate壓縮的方法
這篇文章主要介紹了C#實(shí)現(xiàn)頁(yè)面GZip或Deflate壓縮的方法,涉及C#通過(guò)GZipStream與DeflateStream實(shí)現(xiàn)頁(yè)面壓縮的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#高性能動(dòng)態(tài)獲取對(duì)象屬性值的步驟
這篇文章主要介紹了C#高性能動(dòng)態(tài)獲取對(duì)象屬性值的步驟,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2020-12-12
使用C#實(shí)現(xiàn)上位機(jī)與PLC通信的過(guò)程詳解
隨著工業(yè)自動(dòng)化技術(shù)的不斷發(fā)展,PLC(可編程邏輯控制器)已成為現(xiàn)代生產(chǎn)過(guò)程中不可或缺的設(shè)備,為了實(shí)現(xiàn)設(shè)備之間的數(shù)據(jù)交換和遠(yuǎn)程控制,上位機(jī)系統(tǒng)需要與PLC進(jìn)行通信,在本文中,我們將從零開(kāi)始,介紹如何使用C#實(shí)現(xiàn)上位機(jī)與PLC的通信,需要的朋友可以參考下2025-01-01

