C#//SendKeys.SendWait 仅在进程窗口最小化时起作用

标签 c# sendkeys

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;


namespace TextSendKeys
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        static void Main(string[] args)
        {
            Process[] processes = Process.GetProcessesByName("game");
            Process game1 = processes[0];


            IntPtr p = game1.MainWindowHandle;

            ShowWindow(p,1);
            SendKeys.SendWait("{DOWN}");
            Thread.Sleep(1000);
            SendKeys.SendWait("{DOWN}");



        }
    }
}

该程序应该在游戏窗口中发送两次向下按钮。它仅在我的窗口最小化时有效(它正在激活窗口并完成它的工作)。如果我的窗口被激活(不是最小化),什么也不会发生。如何解决?

谢谢! :)

最佳答案

尝试使用 SetForegroundWindow Win32 API 调用(而不是 ShowWindow)来激活游戏窗口。 (签名来自pinvoke.net。)

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);

static void Main(string[] args)
{
    Process[] processes = Process.GetProcessesByName("game");
    Process game1 = processes[0];

    IntPtr p = game1.MainWindowHandle;

    SetForegroundWindow(p);
    SendKeys.SendWait("{DOWN}");
    Thread.Sleep(1000);
    SendKeys.SendWait("{DOWN}");
}

关于C#//SendKeys.SendWait 仅在进程窗口最小化时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8953399/

相关文章:

c# - 如何缩短 log4net 记录器路径

excel - 默认情况下在切片器上启用多选

vba - 发送 key 正在禁用 NumLock

excel - SendKeys 并不总是有效

windows - 如何使用 Win32 API 模拟按键组合而不激活 Window

c++ - 在 C++ 中模拟 ALT+F4 按键

c# - 在 C# winforms 中读取 csv 文件时出现数据错误

c# - 如何从一直漂浮到屏幕上的小行星射出一个三角形?

c# - 使用 .NET/C# 将 byte[] 转换为 XML

c# - 无法读取 cookie