C# 大漠插件后台绑定窗口发送F1和F2按键
以下是使用大漠插件绑定窗口并向窗口发送F1和F2键的C#代码:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using dm;
namespace DMTest
{
class Program
{
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static void Main(string[] args)
{
string title = '窗口标题'; // 要绑定的窗口标题
string className = '窗口类名'; // 要绑定的窗口类名
int handle = dm.dmsoft.FindWindow(className, title); // 获取窗口句柄
if (handle == 0)
{
MessageBox.Show("找不到指定窗口!");
return;
}
int bindResult = dm.dmsoft.BindWindow(handle, "normal", "normal", "normal", 0);
if (bindResult == 0)
{
MessageBox.Show("绑定窗口失败!");
return;
}
Console.WriteLine("绑定窗口成功!");
// 后台运行
IntPtr hWnd = (IntPtr)handle;
ShowWindow(hWnd, 0);
// 模拟按键
Thread.Sleep(1000); // 等待1秒钟,确保窗口已经激活
dm.dmsoft.KeyPress(dmsoft.VK_F1); // 发送F1键
Thread.Sleep(1000); // 等待1秒钟,确保窗口已经激活
dm.dmsoft.KeyPress(dmsoft.VK_F2); // 发送F2键
Console.WriteLine("按键发送成功!");
Console.ReadLine();
}
}
}
上述代码中,dm是大漠插件的命名空间,需要先在项目中引入大漠插件的DLL文件。dmsoft是大漠插件的对象,表示一个大漠插件实例。dmsoft.FindWindow方法用于查找窗口句柄,dmsoft.BindWindow方法用于绑定窗口。dmsoft.KeyPress方法用于模拟按键操作,dmsoft.VK_F1和dmsoft.VK_F2是大漠插件中定义好的F1和F2键的虚拟键码。ShowWindow方法用于将窗口隐藏起来。
原文地址: https://www.cveoy.top/t/topic/oYeM 著作权归作者所有。请勿转载和采集!