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

Last updated at Posted at 2025-02-13

概要

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

練習問題

ブックマークレット開発環境を作れ。

方針

  • ドッキング使う。
  • automation使う。

写真

image.png

サンプルコード


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 label0 = new Label();
			label0.Location = new Point(20, 10);
			label0.Text = "url";
			bo1 = new TextBox();
			bo1.Text = "https://auctions.yahoo.co.jp/jp/show/submit?category=0";
			bo1.Font = new Font("MS Pゴシック", 12);
			bo1.Size = new Size(300, 20);
			bo1.Location = new Point(50, 30);
			Controls.AddRange(new Control[] {
				label0,
				bo1
			});
			Label label1 = new Label();
			label1.Location = new Point(20, 50);
			label1.Text = "bookmarklet";
			bo2 = new TextBox();
			bo2.Text = "javascript:document.getElementById('fleaTitleForm').value = '商品名は、これです。';";
			bo2.Multiline = true;
			bo2.Font = new Font("MS Pゴシック", 12);
			bo2.Size = new Size(300, 200);
			bo2.Location = new Point(50, 70);
			Controls.AddRange(new Control[] {
				label1,
				bo2
			});
			Label label2 = new Label();
			label2.Location = new Point(20, 280);
			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
			});
		}
		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("open");
			if (ind == 0)
			{
				open();
				bo3.Text = "";
				return;
			}
			ind = bo3.Text.IndexOf("test");
			if (ind == 0)
			{
				test();
				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;
		}
		private static AutomationElement FindElementById(AutomationElement rootElement, string id) {
			return rootElement.FindFirst(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, id));
		}
		private static AutomationElement FindElementByName(AutomationElement rootElement, string name) {
			return rootElement.FindFirst(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, name));
		}
		public AutomationElement GetMainFrameElement(Process p) {
			return AutomationElement.FromHandle(p.MainWindowHandle);
		}
		void getps() {
			Process edge = UpdateTargetProcess("Edge");
			MessageBox.Show("getps");

			edgeElement = GetMainFrameElement(edge);
			msg = "getps ok\r\n";
		}
		void open() {
			AutomationElement aeTag = FindElementById(edgeElement, "aucHTMLtag");
			if (aeTag == null)
			{
				Console.WriteLine("error3");
			}
			else
			{
				var vButton = (InvokePattern) aeTag.GetCurrentPattern(InvokePattern.Pattern);
				vButton.Invoke();
			}
			msg = "open ok\r\n";
		}
		void test() {
			AutomationElement aeUrl = FindElementByName(edgeElement, "アドレスと検索バー");
			if (aeUrl == null)
			{
				Console.WriteLine("error0");
			}
			else
			{
				var vUrl = (ValuePattern) aeUrl.GetCurrentPattern(ValuePattern.Pattern);
				vUrl.SetValue(bo2.Text);
			}
			msg = "test ok\r\n";
		}
	}
	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");
			//Thread.Sleep(2000);
			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\nopen\r\ntest\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());
		}
	}
}







以上。

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?