using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace Win32Api_test
{
class Program
{
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindow(string lpszClass, string lpszWindow);
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(
IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
static extern IntPtr GetWindow(IntPtr hWnd, Int32 uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, String lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
const uint WM_SETTEXT = 0x000C;
const uint WM_LBUTTONDOWN = 0x201;
const uint WM_LBUTTONUP = 0x202;
const int GW_HWNDNEXT = 2;
static void Main(string[] args)
{
// Market Speedのログインダイアログを探す
IntPtr h = FindWindow(null, "Market Speed - ログイン");
// ユーザーID入力欄に入力する
IntPtr h2 = FindWindowEx(h, IntPtr.Zero, "Static", "ログインID");
IntPtr h3 = GetWindow(h2, GW_HWNDNEXT);
SendMessage(h3, WM_SETTEXT, IntPtr.Zero, "ゆーざーID");
// パスワード入力欄に入力する
IntPtr h4 = FindWindowEx(h, IntPtr.Zero, "Static", "パスワード");
IntPtr h5 = GetWindow(h4, GW_HWNDNEXT);
SendMessage(h5, WM_SETTEXT, IntPtr.Zero, "ぱすわーど");
// "OK"ボタンを押下する
IntPtr h6 = FindWindowEx(h, IntPtr.Zero, "Button", "OK");
PostMessage(h6, WM_LBUTTONDOWN, 0, 0);
PostMessage(h6, WM_LBUTTONUP, 0, 0);
// ここでウェイトが必要
Thread.Sleep(1000);
}
}
}