実用例
Win32API を使う
クラスの先頭でWin32API 関数を宣言します。構造体や値の定義もココで行います。
using System.Runtime.InteropServices; // for DllImport
public partial class Form1 : Form
{
[DllImport("user32.dll")]
extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
extern static bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private void Form1_Load(object sender, EventArgs e)
{
IntPtr hWndTarget = FindWindow("WINDOW_CLASS", "WINDOW_TEXT");
if (hWndTarget != IntPtr.Zero)
{
Rectangle rectTarget = new Rectangle();
{
RECT rect = new RECT();
GetWindowRect(hWndTarget, out rect);
rectTarget = new Rectangle( rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top);
}
SetForegroundWindow(hWndTarget);
}
}
}S