宣言
//API
#region API
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int length);
[DllImport("user32.dll")]
public static extern bool OpenIcon(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONUP = 0x0202;
private const int MK_LBUTTON = 0x0001;
////EnumChildWindows
#region EnumChildWindows
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private extern static bool EnumChildWindows(IntPtr hWnd, EnumWindowsDelegate lpEnumFunc, IntPtr lparam);
private delegate bool EnumWindowsDelegate(IntPtr hWnd, IntPtr lparam);
static List<IntPtr> childList;
static private List<IntPtr> GetChildWindows(IntPtr hwnd)
{
childList = new List<IntPtr>();
EnumChildWindows(hwnd, new EnumWindowsDelegate(EnumWindowCallBack), IntPtr.Zero);
return childList;
}
private static bool EnumWindowCallBack(IntPtr hWnd, IntPtr lparam)
{
childList.Add(hWnd);
return true;
}
#endregion EnumChildWindows ----- END ----
#endregion API ----- END -----
子ウィンドウのハンドルリストの取得する
List<IntPtr> list = GetChildWindows((IntPtr)tmpInt);
プログラム名からハンドルを取得する
IntPtr hWnd = FindWindow(null, "Google - Mozilla Firefox");
if (hWnd == IntPtr.Zero) throw new Exception("Error");