Loading [MathJax]/extensions/tex2jax.js

2014年6月7日土曜日

C# で AutoIt的なもの 1 ( WinExists )

やりたいこと
AutoIt的なものを自前でやってみたいなぁとおもってます。
まずは、簡単そうなWinExsists()を作ってみようかと思います
AutoItのWinExistsってどんな関数?
引数titleとマッチする、ウィンドウがあるかどうかを教えてくれる関数です。
比較する対象は、ウィンドウタイトル / クラス名 / ウィンドウハンドルです。
詳細はこちら、
http://www.autoitscript.com/autoit3/docs/functions/WinExists.htm
  1. bool WinExists (   
  2.     "title"     // チェックする title/hWnd/class   
  3.     [, "text"]  // オプションの引数  ウィンドウのどっかにあるテキストと比較するみたい。  
  4. )  
自前のWinExistsは?
自前の関数では、以下のようにしたいと思います。
(1) オプションの引数は、ひとまずよくわからないので、除外して考えます。
(2) hWndでもチェックできますが、難しそうなのでやめておこうかと思います。
(3) 文字列は、大文字/小文字は考慮せずに、含まれているかどうかで判断する。
ちょっとシンプルになってこんな感じの関数となります。
  1. bool WinExists (   
  2.     "title"     // チェックする title/class   
  3. )  
こんな風にしました。
UIはこんな感じです。

このまえやったEnumwindowをちょろっと変えて、作りました。

ソース
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. //add  
  12. using System.Runtime.InteropServices;  
  13. using System.Collections;  
  14. using System.Text.RegularExpressions;  
  15.   
  16.   
  17. namespace AutoTest2  
  18. {  
  19.     public partial class Form1 : Form  
  20.     {  
  21.         public Form1()  
  22.         {  
  23.             InitializeComponent();  
  24.         }  
  25.   
  26.         private void btnWinExist_Click(object sender, EventArgs e)  
  27.         {  
  28.             bool bExists = WinExists( tBWinExistTitle.Text );  
  29.             if (bExists)  
  30.             {  
  31.                 // あった。  
  32.                 tBWinExistRet.Text = "Exist";  
  33.             }  
  34.             else  
  35.             {  
  36.                 // なかった。  
  37.                 tBWinExistRet.Text = "Not Exist";  
  38.             }  
  39.         }  
  40.   
  41.         //--------------------  
  42.         // これ。  
  43.         //--------------------  
  44.         private bool WinExists(string title)  
  45.         {  
  46.             bool bret = false;  
  47.   
  48.             if (!title.Equals(string.Empty)) // 入力されなかったらやめる。  
  49.             {  
  50.   
  51.                 // 存在するウィンドウのタイトルとクラス名の一覧を取得  
  52.                 ArrayList alist = new ArrayList();  
  53.                 Win32.EnumWindows(new Win32.EnumWindowsProc(Win32.EnumTheWindows), ref alist);  
  54.   
  55.                 // マッチするかチェック。  
  56.                 foreach (WndInfo winf in alist)  
  57.                 {  
  58.                     // titleとマッチするか?  
  59.                     if (winf.title.IndexOf(title, StringComparison.OrdinalIgnoreCase) >= 0)  
  60.                     {  
  61.                         bret = true;  
  62.                         break;  
  63.                     }  
  64.   
  65.                     // クラス名とマッチするか?  
  66.                     if (winf.className.IndexOf(title, StringComparison.OrdinalIgnoreCase) >= 0)  
  67.                     {  
  68.                         bret = true;  
  69.                         break;  
  70.                     }  
  71.                 }  
  72.             }  
  73.   
  74.             return bret;  
  75.         }  
  76.     }  
  77.   
  78.     //--------------------  
  79.     // win32 api   
  80.     //--------------------  
  81.     public class Win32  
  82.     {  
  83.         public delegate bool EnumWindowsProc(IntPtr hWnd, ref ArrayList data);  
  84.           
  85.         [DllImport("user32.dll", CharSet = CharSet.Unicode)]  
  86.         protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);  
  87.           
  88.         [DllImport("user32.dll", CharSet = CharSet.Unicode)]  
  89.         protected static extern int GetWindowTextLength(IntPtr hWnd);  
  90.           
  91.         [DllImport("user32.dll")]  
  92.         public static extern bool EnumWindows(EnumWindowsProc enumProc, ref ArrayList data);  
  93.           
  94.         [DllImport("user32.dll")]  
  95.         protected static extern bool IsWindowVisible(IntPtr hWnd);  
  96.   
  97.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
  98.         private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);  
  99.   
  100.         public static bool EnumTheWindows(IntPtr hWnd, ref ArrayList alist)  
  101.         {  
  102.             int size = GetWindowTextLength(hWnd);  
  103.             if (size++ > 0 && IsWindowVisible(hWnd))  
  104.             {  
  105.                 // ウィンドウタイトル  
  106.                 StringBuilder sbTitle = new StringBuilder(size);  
  107.                 GetWindowText(hWnd, sbTitle, size);  
  108.                   
  109.                 // class name  
  110.                 StringBuilder className = new StringBuilder(256);  
  111.                 GetClassName(hWnd, className, className.Capacity);  
  112.   
  113.                 alist.Add(new WndInfo(sbTitle.ToString(), className.ToString()));  
  114.             }  
  115.   
  116.               
  117.             return true;  
  118.         }  
  119.     }  
  120.   
  121.     //--------------------  
  122.     // window の情報 を保持する 構造体  
  123.     //--------------------  
  124.     public struct WndInfo  
  125.     {  
  126.         public string title;  
  127.         public string className;  
  128.   
  129.         public WndInfo(string Title, string ClassName)  
  130.         {  
  131.             title = Title;  
  132.             className = ClassName;  
  133.         }  
  134.     }  
  135.   
  136. }  
0 件のコメント:
コメントを投稿