C# // SendKeys.SendWait 仅在进程窗口最小化时才有效
本文关键字:最小化 有效 窗口 SendKeys SendWait 进程 | 更新日期: 2023-09-27 18:21:13
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}");
}