C#使用大漠插件 绑定窗口 并向窗口发送 f1和f2
以下是使用大漠插件绑定窗口并向窗口发送 F1 和 F2 的 C#示例代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestDemo
{
class Program
{
// 定义大漠插件的 COM 接口
[ComImport]
[Guid("26037F0E-7CBD-4FFF-9C63-56F2D0770213")]
public class DMSoft
{
}
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
// 创建大漠插件实例
DMSoft dm = new DMSoft();
// 绑定窗口,参数为窗口句柄
int hWnd = dm.FindWindow("", "窗口标题");
dm.BindWindow(hWnd, "normal", "normal", "normal", 0);
// 激活窗口
SetForegroundWindow((IntPtr)hWnd);
// 发送 F1 和 F2 按键
dm.KeyPress(112); // F1
dm.KeyPress(113); // F2
}
}
}
需要注意的是,绑定窗口时需要传入正确的窗口句柄和窗口的状态参数。如果窗口状态参数不正确,可能会导致无法正确地向窗口发送按键。在上面的示例代码中,我们使用了 "normal" 参数表示窗口处于正常状态。如果窗口处于最小化或最大化状态,需要传入相应的参数。
另外,如果窗口处于非活动状态,需要先将窗口激活(即调用 SetForegroundWindow 函数)才能向窗口发送按键
原文地址: https://www.cveoy.top/t/topic/hvm3 著作权归作者所有。请勿转载和采集!