0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その550

Last updated at Posted at 2025-02-06

概要

cscの作法、調べてみた。
練習問題やってみた。

練習問題

edgeのUIAutomationのツリーが見たい。

写真

image.png

サンプルコード

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());
	}
}



以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?