增加发送消息(功能未完善)

This commit is contained in:
fofolee 2025-01-10 21:07:49 +08:00
parent 87a2ac3133
commit bf5a2306d0
3 changed files with 538 additions and 0 deletions

View File

@ -1,5 +1,7 @@
const window = require("./window");
const message = require("./message");
module.exports = {
window,
message,
};

View File

@ -0,0 +1,316 @@
/**
* 发送窗口消息
* @param {string} method 查找方式title/handle/active
* @param {string} value 查找值handle时为数字
* @param {string} type 消息类型click/key/text/custom
* @param {Object} params 消息参数
*/
async function sendMessage(method, value, type, params) {
let messageScript = "";
switch (type) {
case "click":
messageScript = `
const uint WM_LBUTTONDOWN = 0x0201;
const uint WM_LBUTTONUP = 0x0202;
const uint WM_RBUTTONDOWN = 0x0204;
const uint WM_RBUTTONUP = 0x0205;
const uint WM_MBUTTONDOWN = 0x0207;
const uint WM_MBUTTONUP = 0x0208;
IntPtr wParam = IntPtr.Zero;
IntPtr lParam = (IntPtr)((${params.y} << 16) | ${params.x});
switch("${params.button}") {
case "left":
PostMessage(hwnd, WM_LBUTTONDOWN, wParam, lParam);
PostMessage(hwnd, WM_LBUTTONUP, wParam, lParam);
break;
case "right":
PostMessage(hwnd, WM_RBUTTONDOWN, wParam, lParam);
PostMessage(hwnd, WM_RBUTTONUP, wParam, lParam);
break;
case "middle":
PostMessage(hwnd, WM_MBUTTONDOWN, wParam, lParam);
PostMessage(hwnd, WM_MBUTTONUP, wParam, lParam);
break;
}`;
break;
case "key":
messageScript = `
const uint WM_KEYDOWN = 0x0100;
const uint WM_KEYUP = 0x0101;
const uint WM_CHAR = 0x0102;
// 处理组合键
int modifiers = 0;
if(${params.ctrl}) modifiers |= 0x0008;
if(${params.alt}) modifiers |= 0x0001;
if(${params.shift}) modifiers |= 0x0004;
IntPtr wParam = (IntPtr)${params.keyCode};
IntPtr lParam = (IntPtr)((0x00000001 | (modifiers << 16)));
PostMessage(hwnd, WM_KEYDOWN, wParam, lParam);
if(!${params.hold}) {
PostMessage(hwnd, WM_KEYUP, wParam, lParam);
}`;
break;
case "text":
messageScript = `
const uint WM_CHAR = 0x0102;
string text = @"${params.text}";
foreach(char c in text) {
PostMessage(hwnd, WM_CHAR, (IntPtr)c, IntPtr.Zero);
}`;
break;
case "custom":
messageScript = `
uint msg = ${params.message};
IntPtr wParam = (IntPtr)${params.wParam};
IntPtr lParam = (IntPtr)${params.lParam};
PostMessage(hwnd, msg, wParam, lParam);`;
break;
}
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
public class Program {
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
${messageScript}
}
}
}
`;
await quickcommand.runCsharp(script);
}
/**
* 发送鼠标点击消息
*/
async function sendMouseClick(method, value, params) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
public class Program {
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
const uint WM_LBUTTONDOWN = 0x0201;
const uint WM_LBUTTONUP = 0x0202;
const uint WM_RBUTTONDOWN = 0x0204;
const uint WM_RBUTTONUP = 0x0205;
const uint WM_MBUTTONDOWN = 0x0207;
const uint WM_MBUTTONUP = 0x0208;
IntPtr wParam = IntPtr.Zero;
IntPtr lParam = (IntPtr)((${params.y} << 16) | ${params.x});
switch("${params.button}") {
case "left":
PostMessage(hwnd, WM_LBUTTONDOWN, wParam, lParam);
PostMessage(hwnd, WM_LBUTTONUP, wParam, lParam);
break;
case "right":
PostMessage(hwnd, WM_RBUTTONDOWN, wParam, lParam);
PostMessage(hwnd, WM_RBUTTONUP, wParam, lParam);
break;
case "middle":
PostMessage(hwnd, WM_MBUTTONDOWN, wParam, lParam);
PostMessage(hwnd, WM_MBUTTONUP, wParam, lParam);
break;
}
}
}
}
`;
await quickcommand.runCsharp(script);
}
/**
* 发送键盘按键消息
*/
async function sendKeyPress(method, value, params) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
public class Program {
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
const uint WM_KEYDOWN = 0x0100;
const uint WM_KEYUP = 0x0101;
const uint WM_CHAR = 0x0102;
int modifiers = 0;
if(${params.ctrl}) modifiers |= 0x0008;
if(${params.alt}) modifiers |= 0x0001;
if(${params.shift}) modifiers |= 0x0004;
IntPtr wParam = (IntPtr)${params.keyCode};
IntPtr lParam = (IntPtr)((0x00000001 | (modifiers << 16)));
PostMessage(hwnd, WM_KEYDOWN, wParam, lParam);
if(!${params.hold}) {
PostMessage(hwnd, WM_KEYUP, wParam, lParam);
}
}
}
}
`;
await quickcommand.runCsharp(script);
}
/**
* 发送文本输入消息
*/
async function sendText(method, value, params) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
public class Program {
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
const uint WM_CHAR = 0x0102;
string text = @"${params.text}";
foreach(char c in text) {
PostMessage(hwnd, WM_CHAR, (IntPtr)c, IntPtr.Zero);
}
}
}
}
`;
await quickcommand.runCsharp(script);
}
/**
* 发送窗口命令消息
*/
async function sendCommand(method, value, params) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
public class Program {
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
uint msg = ${params.message};
IntPtr wParam = (IntPtr)${params.wParam};
IntPtr lParam = (IntPtr)${params.lParam};
PostMessage(hwnd, msg, wParam, lParam);
}
}
}
`;
await quickcommand.runCsharp(script);
}
/**
* 发送自定义消息
*/
async function sendCustom(method, value, params) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
public class Program {
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
uint msg = ${params.message};
IntPtr wParam = (IntPtr)${params.wParam};
IntPtr lParam = (IntPtr)${params.lParam};
PostMessage(hwnd, msg, wParam, lParam);
}
}
}
`;
await quickcommand.runCsharp(script);
}
module.exports = {
sendMessage,
sendMouseClick,
sendKeyPress,
sendText,
sendCommand,
sendCustom,
};

View File

@ -203,5 +203,225 @@ export const windowsCommands = {
],
isAsync: true,
},
{
value: "quickcomposer.windows.message.sendMouseClick",
label: "发送消息",
icon: "send",
isAsync: true,
config: [
{
key: "method",
label: "查找方式",
component: "q-select",
icon: "search",
width: 3,
options: [
{ label: "标题", value: "title" },
{ label: "句柄", value: "handle" },
{ label: "活动窗口", value: "active" },
],
defaultValue: "title",
},
{
key: "value",
label: "窗口标题/句柄",
component: "VariableInput",
icon: "title",
width: 9,
placeholder: "标题支持模糊匹配,选择活动窗口无需输入",
},
],
subCommands: [
{
value: "quickcomposer.windows.message.sendMouseClick",
label: "鼠标点击",
icon: "mouse",
config: [
{
key: "type",
defaultValue: "click",
hidden: true,
},
{
key: "button",
label: "按键",
component: "q-select",
icon: "mouse",
width: 4,
options: [
{ label: "左键", value: "left" },
{ label: "右键", value: "right" },
{ label: "中键", value: "middle" },
],
defaultValue: "left",
},
{
key: "x",
label: "X坐标",
component: "NumberInput",
icon: "arrow_right",
width: 4,
defaultValue: 0,
},
{
key: "y",
label: "Y坐标",
component: "NumberInput",
icon: "arrow_drop_down",
width: 4,
defaultValue: 0,
},
],
},
{
value: "quickcomposer.windows.message.sendKeyPress",
label: "键盘按键",
icon: "keyboard",
config: [
{
key: "type",
defaultValue: "key",
hidden: true,
},
{
key: "keyCode",
label: "按键码",
component: "NumberInput",
icon: "keyboard",
width: 6,
defaultValue: 0,
},
{
key: "ctrl",
label: "Ctrl",
component: "Switch",
width: 3,
defaultValue: false,
},
{
key: "alt",
label: "Alt",
component: "Switch",
width: 3,
defaultValue: false,
},
{
key: "shift",
label: "Shift",
component: "Switch",
width: 3,
defaultValue: false,
},
{
key: "hold",
label: "按住",
component: "Switch",
width: 3,
defaultValue: false,
},
],
},
{
value: "quickcomposer.windows.message.sendText",
label: "文本输入",
icon: "text_fields",
config: [
{
key: "type",
defaultValue: "text",
hidden: true,
},
{
key: "text",
label: "文本内容",
component: "VariableInput",
icon: "text_fields",
width: 12,
},
],
},
{
value: "quickcomposer.windows.message.sendCommand",
label: "窗口命令",
icon: "window",
config: [
{
key: "type",
defaultValue: "custom",
hidden: true,
},
{
key: "message",
label: "命令",
component: "q-select",
icon: "code",
width: 12,
options: [
{ label: "关闭窗口", value: "0x0010" }, // WM_CLOSE
{ label: "销毁窗口", value: "0x0002" }, // WM_DESTROY
{ label: "激活窗口", value: "0x0006" }, // WM_ACTIVATE
{ label: "显示窗口", value: "0x0018" }, // WM_SHOWWINDOW
{ label: "重绘窗口", value: "0x000F" }, // WM_PAINT
{ label: "窗口尺寸", value: "0x0005" }, // WM_SIZE
{ label: "窗口位置", value: "0x0003" }, // WM_MOVE
],
defaultValue: "0x0010",
},
{
key: "wParam",
label: "参数1",
component: "NumberInput",
icon: "input",
width: 6,
defaultValue: 0,
},
{
key: "lParam",
label: "参数2",
component: "NumberInput",
icon: "input",
width: 6,
defaultValue: 0,
},
],
},
{
value: "quickcomposer.windows.message.sendCustom",
label: "自定义消息",
icon: "settings",
config: [
{
key: "type",
defaultValue: "custom",
hidden: true,
},
{
key: "message",
label: "消息ID",
component: "NumberInput",
icon: "tag",
width: 12,
defaultValue: 0,
},
{
key: "wParam",
label: "wParam",
component: "NumberInput",
icon: "input",
width: 6,
defaultValue: 0,
},
{
key: "lParam",
label: "lParam",
component: "NumberInput",
icon: "input",
width: 6,
defaultValue: 0,
},
],
},
],
},
],
};