8

winapi を使用して C# Windows フォームのメッセージ ボックスの [OK] ボタンをクリックしようとしています。以下は私が取り組んでいるコードです。

private const int WM_CLOSE = 16;
private const int BN_CLICKED = 245;

[DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);

[DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string  windowTitle);

//this works
hwnd = FindWindow(null, "Message");
if(hwnd!=0)
      SendMessage(hwnd, WM_CLOSE, 0, IntPtr.Zero);

//this doesn't work.
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", "ok");
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);

で値を取得していますがhwndChild、認識していませんBN_CLICKED。何が欠けているのかわかりません。助けはありますか?

別のアプリケーションのメッセージ ボックス ボタンを閉じようとしていますが、これが私がやっていることです。しかし、私はまだ何かが欠けています。

IntPtr hwndChild = IntPtr.Zero;
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero,' '"Button", "OK");
SendMessage((int)hwndChild, WM_COMMAND, (BN_CLICKED '<<16) | IDOK, hwndChild);
4

2 に答える 2

12

BN_CLICKED はメッセージではありません。BN_CLICKED通知とボタン ID を含む WM_COMMAND メッセージwParamと のボタン ハンドルを送信する必要がありますlParam

ボタンの親ウィンドウは、WM_COMMAND メッセージを通じてこの通知コードを受け取ります。

private const uint WM_COMMAND = 0x0111;
private const int BN_CLICKED = 245;
private const int IDOK = 1;

[DllImport("user32.dll", CharSet=CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);

[DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string  windowTitle);

SendMessage(hwndChild, WM_COMMAND, (BN_CLICKED << 16) | IDOK, hwndChild);
于 2013-02-19T16:18:29.860 に答える
7

Finallu、これは私にとってはうまくいきます。最初のクリックでおそらくウィンドウがアクティブになり、2 回目のクリックでボタンがクリックされます。

SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
SendMessage(btnHandle, WM_LBUTTONDOWN, 0, 0);
SendMessage(btnHandle, WM_LBUTTONUP, 0, 0);
于 2013-02-19T18:26:33.107 に答える