概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
edgeのUIAutomationのツリーが見たい。
写真
サンプルコード
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Automation;
using System.Windows.Forms;
using System.Drawing;
class FormMain: Form {
private TreeView tv;
FormMain() {
Text = "Edge UI";
Size = new Size(700, 700);
tv = new TreeView();
tv.Location = new Point(20, 20);
tv.Size = new Size(600, 600);
this.Controls.Add(tv);
this.Load += new System.EventHandler(this.Form_Load);
}
private void Form_Load(object sender, System.EventArgs e) {
Process process = Process.Start(@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", "https://auctions.yahoo.co.jp");
Thread.Sleep(2000);
process = UpdateTargetProcess("Yahoo!オークション - 日本最大級のネットオークション・フリマアプリ");
AutomationElement mainForm = AutomationElement.FromHandle(process.MainWindowHandle);
TreeNode tn = new TreeNode(mainForm.Current.Name);
WalkEnabledElements(mainForm, tn);
tv.Nodes.Add(tn);
}
private static Process UpdateTargetProcess(string title) {
Process process = null;
foreach (Process p in Process.GetProcesses())
{
if (p.MainWindowTitle.Contains(title))
{
process = p;
break;
}
}
if (process == null)
{
Console.WriteLine(title + "のプロセスが見つかりません。");
}
return process;
}
private void WalkEnabledElements(AutomationElement rootElement, TreeNode treeNode) {
Condition condition1 = new PropertyCondition(AutomationElement.IsControlElementProperty, true);
Condition condition2 = new PropertyCondition(AutomationElement.IsEnabledProperty, true);
TreeWalker walker = new TreeWalker(new AndCondition(condition1, condition2));
AutomationElement elementNode = walker.GetFirstChild(rootElement);
while (elementNode != null)
{
TreeNode childTreeNode = treeNode.Nodes.Add(elementNode.Current.Name);
WalkEnabledElements(elementNode, childTreeNode);
elementNode = walker.GetNextSibling(elementNode);
}
}
[STAThread]
public static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
}
以上。