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の作法 その563

Last updated at Posted at 2025-02-19

概要

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

練習問題

automationで、フォーカスが当たったエレメントを取得せよ。

方針

  • timer使う。
  • FocusedElement使う。

サンプルコード

using System;
using System.Data;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Automation;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;

namespace Dock
{
	public partial class Win1: WeifenLuo.WinFormsUI.Docking.DockContent {
		public string msg = "";
		public TextBox bo1;
		public TextBox bo2;
		public TextBox bo3;
		AutomationElement edgeElement;
		public Win1() {
			Label label2 = new Label();
			label2.Location = new Point(20, 270);
			label2.Text = "command";
			bo3 = new TextBox();
			bo3.Text = "getps";
			bo3.Font = new Font("MS Pゴシック", 12);
			bo3.Size = new Size(300, 20);
			bo3.Location = new Point(50, 300);
			Controls.AddRange(new Control[] {
				label2,
				bo3
			});
			Button btn0 = new Button();
			btn0.Location = new Point(250, 330);
			btn0.Text = "send";
			btn0.Click += btn0_Click;
			Controls.AddRange(new Control[] {
				btn0
			});
			System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer {
				Interval = 900
			};
			timer.Tick += timer_Tick;
			timer.Start();
		}
		private void timer_Tick(object sender, EventArgs e) {
			Console.WriteLine(AutomationElement.FocusedElement.Current.Name);
		}
		void btn0_Click(object sender, System.EventArgs e) {
			int ind = bo3.Text.IndexOf("getps");
			if (ind == 0)
			{
				getps();
				bo3.Text = "";
				return;
			}
			ind = bo3.Text.IndexOf("test1");
			if (ind == 0)
			{
				test1();
				bo3.Text = "";
				return;
			}
			MessageBox.Show("No Command!!");
			bo3.Text = "";
		}
		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;
		}
		public AutomationElement GetMainFrameElement(Process p) {
			return AutomationElement.FromHandle(p.MainWindowHandle);
		}
		void getps() {
			Process edge = UpdateTargetProcess("Edge");
			edgeElement = GetMainFrameElement(edge);
			msg = "getps ok\r\n";
		}
		void test1() {
		}
	}
	public partial class Win2: WeifenLuo.WinFormsUI.Docking.DockContent {
		public string msg = "";
		public Win2() {
			Button btn0 = new Button();
			btn0.Location = new Point(50, 50);
			btn0.Text = "start";
			btn0.Click += btn0_Click;
			Controls.AddRange(new Control[] {
				btn0
			});
		}
		void btn0_Click(object sender, System.EventArgs e) {
			Process edge = Process.Start("msedge", "https://auctions.yahoo.co.jp/sell/jp/show/submit");
			msg = "edge start ok\r\n";
		}
	}
	public partial class Win3: WeifenLuo.WinFormsUI.Docking.DockContent {
		public TextBox bo1;
		public Win3() {
			bo1 = new TextBox();
			bo1.Text = "";
			bo1.Multiline = true;
			bo1.Font = new Font("MS Pゴシック", 12);
			bo1.Dock = DockStyle.Fill;
			Controls.AddRange(new Control[] {
				bo1
			});
		}
	}
	public partial class Form1: Form {
		public Win2 left1;
		public Win3 right1;
		public Win1 center1;
		public Win3 bottom1;
		public Form1() {
			ClientSize = new Size(800, 600);
			Text = "dock";
			var dockPanel1 = new DockPanel();
			dockPanel1.ShowDocumentIcon = true;
			dockPanel1.Dock = DockStyle.Fill;
			dockPanel1.DocumentStyle = DocumentStyle.DockingWindow;
			Controls.Add(dockPanel1);
			left1 = new Win2();
			left1.TabText = "left1";
			left1.Show(dockPanel1, DockState.DockLeft);
			right1 = new Win3();
			right1.TabText = "right1";
			right1.Show(dockPanel1, DockState.DockRight);
			right1.bo1.AppendText("getps\r\ntest1\r\n");
			center1 = new Win1();
			center1.TabText = "center1";
			center1.Show(dockPanel1, DockState.Document);
			bottom1 = new Win3();
			bottom1.TabText = "bottom1";
			bottom1.Show(dockPanel1, DockState.DockBottom);
			dockPanel1.UpdateDockWindowZOrder(DockStyle.Left, true);
			System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer {
				Interval = 200
			};
			timer.Tick += timer_Tick;
			timer.Start();
		}
		private void timer_Tick(object sender, EventArgs e) {
			if (center1.msg != "")
			{
				bottom1.bo1.AppendText(center1.msg);
				center1.msg = "";
			}
			if (left1.msg != "")
			{
				bottom1.bo1.AppendText(left1.msg);
				left1.msg = "";
			}
		}
		[STAThread]
		public static void Main() {
			Application.Run(new Form1());
		}
	}
}





写真

image.png

以上。

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?