PostMessage不能在Windows 7 x64中工作

本文关键字:x64 工作 Windows 不能 PostMessage | 更新日期: 2023-09-27 18:01:43

我需要模拟游戏窗口中的按键。我尝试发送密钥"A",但它不起作用:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private void button1_Click(object sender, EventArgs e)
{
    IntPtr hWnd = FindWindow(null, "Game Name");  // it's work!
    if (hWnd == IntPtr.Zero)
    {
        MessageBox.Show("Game is not running");
        return;
    }
    SetForegroundWindow(hWnd);  // it's work too and now I have active window of game
    System.Threading.Thread.Sleep(3000);
    const int WM_KEYDOWN = 0x0100;
    PostMessage(hWnd, WM_KEYDOWN, (IntPtr)Keys.A, IntPtr.Zero);     // don't work ;-(
}

PostMessage不能在Windows 7 x64中工作

你得到什么错误?游戏是否以管理员身份运行?