概要
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());
}
}
}
写真
以上。