C#// SendKeys.SendWait仅在最小化进程窗口时有效

问题描述 投票:4回答:2
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}");



        }
    }
}

该程序应该在游戏窗口中发送两次DOWN按钮。它仅在最小化我的窗口时才起作用(它正在激活窗口并完成工作)。如果我的窗口被激活(未最小化),则什么都不会发生。怎么解决呢?

谢谢! :)

c# sendkeys
2个回答
10
投票

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

pinvoke.net

0
投票

非常感谢道格拉斯。您对此非常有技巧...

© www.soinside.com 2019 - 2024. All rights reserved.