C#の問題があります。
現在のウィンドウへのクリックをシミュレートすることはできますが、ウィンドウが最小化または非表示になっている場合にシミュレートしたいと思います。
何か案は?
これは、ウィンドウハンドルを提供し、サブウィンドウをターゲットにして、そのサブウィンドウにメッセージを投稿する、完全に機能するスニペットです。
#include "TCHAR.h"
#include "Windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
HWND hwndWindowTarget;
HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
if (hwndWindowNotepad)
{
// Find the target Edit window within Notepad.
hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
if (hwndWindowTarget) {
PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
}
}
return 0;
}
現時点では、G
文字をメモ帳「無題」に送信します(新しいメモ帳を開き、何もしません。
spy++
VisualStudioに付属しているサブウィンドウを使用して見つけることができます。
SendInputを使用してマウスイベントを送信する例を次に示します。
#include "TCHAR.h"
#include "Windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
POINT pt;
pt.x = 300;
pt.y = 300;
HWND hwndWindowTarget;
HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
if (hwndWindowNotepad)
{
// Find the target Edit window within Notepad.
hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
if (hwndWindowTarget) {
PostMessage ( hwndWindowTarget, WM_RBUTTONDOWN, 0, (pt.x) & (( pt.y) << 16) );
PostMessage ( hwndWindowTarget, WM_RBUTTONUP, 0, (pt.x ) & (( pt.y) << 16) );
}
}
return 0;
}
申し訳ありませんが、これには時間がかかりました。C#バージョンは次のとおりです。
using System;
using System.Runtime.InteropServices;
namespace Sandbox
{
class Program
{
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hWnd, string lpClassName, string lpWindowName, string lParam);
public const Int32 WM_CHAR = 0x0102;
public const Int32 WM_KEYDOWN = 0x0100;
public const Int32 WM_KEYUP = 0x0101;
public const Int32 VK_RETURN = 0x0D;
public const string windowName = "Untitled - Notepad";
static void Main(string[] args)
{
Console.WriteLine($"Finding {windowName}");
var hwndWindowNotepad = FindWindow(null, "Untitled - Notepad");
if (hwndWindowNotepad != 0)
{
// Find the target Edit window within Notepad.
var hwndWindowTarget = FindWindowEx(hwndWindowNotepad, null, "Edit", null);
if (hwndWindowTarget != 0)
{
Console.WriteLine($"Sending Char to {windowName}");
PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
}
}
}
}
}