cscの作法 その422

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

# 練習問題
automationでペイント、開いて円を描け。


# 写真


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/721fcb24-aaf0-773c-04b9-9ee6da36dc07.png)

# サンプルコード

```
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace App
{
	public class UIAutomationLib {
		readonly string ModuleName = "UIAutomationLib";
		[DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
		static extern void SetCursorPos(int X, int Y);
		[DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
		static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
		private const int MOUSEEVENTF_LEFTDOWN = 0x2;
		private const int MOUSEEVENTF_LEFTUP = 0x4;
		public Process UpdateTargetProcess(string title) {
			Process process = null;
			foreach (Process p in Process.GetProcesses())
			{
				if (p.MainWindowTitle.Contains(title))
				{
					process = p;
					break;
				}
			}
			if (process == null)
			{
				MessageBox.Show(title + "のプロセスが見つかりません。", ModuleName);
			}
			return process;
		}
		public AutomationElement GetMainFrameElement(Process p) {
			return AutomationElement.FromHandle(p.MainWindowHandle);
		}
		public void PushButtonByName(AutomationElement element, string name) {
			InvokePattern button = FindElementsByName(element, name).First().GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
			button.Invoke();
		}
		public void PushButtonById(AutomationElement element, string AutomationId) {
			InvokePattern button = FindElementById(element, AutomationId).GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
			button.Invoke();
		}
		public void ClickElement(AutomationElement element, string AutomationId) {
			AutomationElement target = FindElementById(element, AutomationId);
			System.Windows.Point p = target.GetClickablePoint();
			SetCursorPos((int) p.X, (int) p.Y + 20);
			mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
		}
		public void click(int x, int y) {
			SetCursorPos(x, y);
			mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			//mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
		}
		public void move(int x, int y) {
			SetCursorPos(x, y);
			mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			//mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
		}
		public void clickup(int x, int y) {
			SetCursorPos(x, y);
			//mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
			mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
		}
		public void Keyin(bool focus, AutomationElement element, string text) {
			if (focus)
			{
				element.SetFocus();
			}
			Thread.Sleep(200);
			SendKeys.SendWait(text);
			Thread.Sleep(200);
		}
		public AutomationElement FindElementById(AutomationElement rootElement, string automationId) {
			return rootElement.FindFirst(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, automationId));
		}
		public IEnumerable<AutomationElement> FindElementsByName(AutomationElement rootElement, string name) {
			return rootElement.FindAll(TreeScope.Element | TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, name)).Cast<AutomationElement>();
		}
	}
	class Program {
		static void Main(string[] args) {
			UIAutomationLib ui = new UIAutomationLib();
			Process paint = Process.Start(@"mspaint");
			Thread.Sleep(2000);
			paint = ui.UpdateTargetProcess("ペイント");
			AutomationElement paintElement = ui.GetMainFrameElement(paint);
			ui.click(500, 400);
			for (double x = 0.0; x < 14.0; x += 0.2)
			{
				ui.move((int)(200 * Math.Cos(x) + 500), (int)(200 * Math.Sin(x) + 400));
			}
			ui.clickup(500, 400);
		}
	}
}





```

以上。