用到了两个程序包,FlaUI .Core,FlaUI.UIA3,在VS的扩展里可以下载安装这两个程序包,UI自动化是用来实现自动测试程序流程的,减少人工测试的成本。有需要源程序的私信我。
获取微信窗口句柄
void GetWxHandle()
{
var process = Process.GetProcessesByName("Wechat").FirstOrDefault();
if (process != null)
{
ProcessId = process.Id;
}
}
检测微信是否登录,并激活微信聊天窗
bool InitWechat()
{
//根据微信进程ID绑定FLAUI
Application = FlaUI.Core.Application.Attach(ProcessId);
if (Application.HasExited)
{
MessageBox.Show("请登录微信!");
return false;
}
var automation = new UIA3Automation();
var windows= Application.GetAllTopLevelWindows(automation);
//获取微信window自动化操作对象
if (windows.Count()== 0)
{
MessageBox.Show("请打开聊天窗口!");
return false;
}
else
{
wxWindow = Application.GetMainWindow(automation);
if (wxWindow.AsWindow().Patterns.Window.PatternOrDefault != null)
{
//将微信窗体设置为默认焦点状态
wxWindow.AsWindow().Patterns.Window.Pattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Normal);
}
return true;
}
}
点击发送按钮,自动搜索聊天记录,并转发给最新发送的人
private async void button1_Click(object sender, EventArgs e)
{
var path = textBox1.Text;
var sendMsg = path.Substring(path.LastIndexOf("\\") + 1, path.Length-path.LastIndexOf("\\")-1);
var itemName = sendMsg;
if( !InitWechat())
{
return;
}
if (wxWindow != null)
{
if (wxWindow.AsWindow().Patterns.Window.PatternOrDefault != null)
{
//将微信窗体设置为默认焦点状态
wxWindow.AsWindow().Patterns.Window.Pattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Normal);
}
}
//await Task.Delay(100);
var search = wxWindow.FindAllDescendants().FirstOrDefault(s => s.Name == "搜索");
var sync = SynchronizationContext.Current;
search.FocusNative();
search.Focus();
search.Click();
var text = wxWindow.FindAllDescendants().FirstOrDefault(s => s.Name == "搜索").Parent;
if (text != null)
{
//await Task.Delay(100);
var txt = text.FindAllChildren().FirstOrDefault(s => s.ControlType == ControlType.Text).AsTextBox();
txt.Text = itemName;
await Task.Delay(500);
var item = wxWindow.FindAllDescendants().ToList();
wxWindow.FocusNative();
if (item != null && item.Count > 0 && !string.IsNullOrWhiteSpace(sendMsg))
{
var first =item.FirstOrDefault(s=>s.Parent != null && s.Parent.Name.Contains("@str:IDS_FAV_SEARCH_RESULT")&&!s.Name.Contains("搜索网络结果"));
first.FocusNative();
first.Focus();
first.Click();
var automation = new UIA3Automation();
var firstWindow= Application.GetAllTopLevelWindows(automation).FirstOrDefault();
var childen= firstWindow.FindAllDescendants().Where(t=>t.ControlType==ControlType.Button&&t.Name=="进入聊天").FirstOrDefault();
childen.Click();
var msgBox = wxWindow.FindFirstDescendant(x => x.ByControlType(FlaUI.Core.Definitions.ControlType.Text)).AsTextBox();
msgBox.Text = "";
var button = wxWindow.FindAllDescendants().Where(s => s.ControlType == ControlType.Button && s.Name == "发送文件").FirstOrDefault();
button?.Click();
var fileWindow = Application.GetAllTopLevelWindows(automation).FirstOrDefault();
var fileChileWindows = fileWindow.FindAllDescendants();
var fileText = fileChileWindows.Where(t => t.ControlType == ControlType.Edit&&t.Name=="文件名(N):" ).FirstOrDefault();
var fileButton = fileChileWindows.Where(t=>t.ControlType==ControlType.SplitButton&&t.Name=="打开(O)").FirstOrDefault();
fileText.AsComboBox().Value= path.Substring(0,path.LastIndexOf("."));
fileButton?.Click();
await Task.Delay(500);
var mainChileWindow = Application.GetAllTopLevelWindows(automation).FirstOrDefault();
var sendWindow= mainChileWindow.FindAllChildren().FirstOrDefault();
var sendButton= sendWindow.FindAllDescendants().Where(t=>t.ControlType==ControlType.Button&&t.Name.Contains("发送")).FirstOrDefault();
sendButton?.Click();
firstWindow.Close();
}
}
ChatListTokenSource.Cancel();
}
标签:FirstOrDefault,wxWindow,C#,微信,UI,&&,var,ControlType From: https://blog.csdn.net/qq_39541524/article/details/143830739