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/