调整C#调用方式,减少编译次数,优化参数传递

This commit is contained in:
fofolee
2025-01-14 10:44:43 +08:00
parent 55be953eef
commit 601df1f1e3
13 changed files with 956 additions and 1060 deletions

View File

@@ -1,11 +1,9 @@
const window = require("./window");
const message = require("./message");
const automation = require("./automation");
const sendmessage = require("./sendmessage");
const monitor = require("./monitor");
module.exports = {
window,
message,
automation,
sendmessage,
monitor,
};

View File

@@ -1,316 +0,0 @@
/**
* 发送窗口消息
* @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

@@ -1,16 +1,9 @@
const fs = require("fs");
const path = require("path");
// 读取 monitor.cs 模板
const monitorTemplate = fs.readFileSync(
path.join(__dirname, "..", "..", "csharp", "monitor.cs"),
"utf8"
);
const { runCsharpFeature } = require("../../csharp");
// 监控剪贴板变化
const watchClipboard = async function () {
const args = ["-type", "clipboard", "-once"];
const result = await quickcommand.runCsharp(monitorTemplate, args);
const result = await runCsharpFeature("monitor", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
@@ -35,7 +28,7 @@ const watchFileSystem = async function (watchPath, options = {}) {
args.push("-recursive", "false");
}
const result = await quickcommand.runCsharp(monitorTemplate, args);
const result = await runCsharpFeature("monitor", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}

View File

@@ -1,16 +1,8 @@
const fs = require("fs");
const path = require("path");
const quickcommand = require("../../quickcommand");
// 读取 automation.cs 模板
const automationTemplate = fs.readFileSync(
path.join(__dirname, "..", "..", "csharp", "automation.cs"),
"utf8"
);
const { runCsharpFeature } = require("../../csharp");
/**
* 键盘操作
* @param {string} windowType 窗口类型title/handle/active
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {string} keys 键盘按键
* @param {object} options 选项
@@ -18,15 +10,15 @@ const automationTemplate = fs.readFileSync(
* @param {boolean} options.background 是否后台操作
* @returns {boolean} 是否成功
*/
const sendKeys = async function (windowType, window, keys, options = {}) {
const sendKeys = async function (method, window, keys, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "keys", "-value", keys];
if (windowType !== "active" && window) args.push("-window", window);
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
const result = await quickcommand.runCsharp(automationTemplate, args);
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
@@ -35,7 +27,7 @@ const sendKeys = async function (windowType, window, keys, options = {}) {
/**
* 发送文本
* @param {string} windowType 窗口类型title/handle/active
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {string} text 文本
* @param {object} options 选项
@@ -43,15 +35,16 @@ const sendKeys = async function (windowType, window, keys, options = {}) {
* @param {boolean} options.background 是否后台操作
* @returns {boolean} 是否成功
*/
const sendText = async function (windowType, window, text, options = {}) {
const sendText = async function (method, window, text, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "text", "-value", text];
if (windowType !== "active" && window) args.push("-window", window);
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
const result = await quickcommand.runCsharp(automationTemplate, args);
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
@@ -60,7 +53,7 @@ const sendText = async function (windowType, window, text, options = {}) {
/**
* 鼠标点击
* @param {string} windowType 窗口类型title/handle/active
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {string} action 动作click/doubleClick/rightClick
* @param {object} options 选项
@@ -71,7 +64,7 @@ const sendText = async function (windowType, window, text, options = {}) {
* @returns {boolean} 是否成功
*/
const click = async function (
windowType,
method,
window,
action = "click",
options = {}
@@ -79,13 +72,14 @@ const click = async function (
const { control, text, pos, background = false } = options;
const args = ["-type", "mouse", "-action", action];
if (windowType !== "active" && window) args.push("-window", window);
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
if (text) args.push("-text", text);
if (pos) args.push("-pos", pos);
args.push("-background", background.toString());
const result = await quickcommand.runCsharp(automationTemplate, args);
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
@@ -94,22 +88,23 @@ const click = async function (
/**
* 获取窗口控件树
* @param {string} windowType 窗口类型title/handle/active
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @param {boolean} options.background 是否后台操作
* @returns {object} 控件树
*/
const inspectWindow = async function (windowType, window, options = {}) {
const inspectWindow = async function (method, window, options = {}) {
const { filter, background = false } = options;
const args = ["-type", "inspect"];
if (windowType !== "active" && window) args.push("-window", window);
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
args.push("-background", background.toString());
const result = await quickcommand.runCsharp(automationTemplate, args);
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}

View File

@@ -1,3 +1,5 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 窗口置顶
* @param {string} method 查找方式title/handle/active
@@ -5,40 +7,17 @@
* @param {boolean} isTopMost 是否置顶
*/
async function setTopMost(method, value, isTopMost) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
const uint SWP_NOMOVE = 0x0002;
const uint SWP_NOSIZE = 0x0001;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
SetWindowPos(hwnd, ${
isTopMost ? "HWND_TOPMOST" : "HWND_NOTOPMOST"
}, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
}
}
`;
await quickcommand.runCsharp(script);
const args = [
"-type",
"topmost",
"-method",
method,
"-window",
value,
"-value",
isTopMost.toString(),
];
await runCsharpFeature("window", args);
}
/**
@@ -48,38 +27,17 @@ async function setTopMost(method, value, isTopMost) {
* @param {number} opacity 透明度 0-100
*/
async function setOpacity(method, value, opacity) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
const int GWL_EXSTYLE = -20;
const int WS_EX_LAYERED = 0x80000;
const uint LWA_ALPHA = 0x2;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd, 0, (byte)(${opacity} * 2.55), LWA_ALPHA);
}
}
}
`;
await quickcommand.runCsharp(script);
const args = [
"-type",
"opacity",
"-method",
method,
"-window",
value,
"-value",
opacity.toString(),
];
await runCsharpFeature("window", args);
}
/**
@@ -92,30 +50,17 @@ async function setOpacity(method, value, opacity) {
* @param {number} height 高度
*/
async function setWindowRect(method, value, x, y, width, height) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
MoveWindow(hwnd, ${x}, ${y}, ${width}, ${height}, true);
}
}
}
`;
await quickcommand.runCsharp(script);
const args = [
"-type",
"rect",
"-method",
method,
"-window",
value,
"-value",
`${x},${y},${width},${height}`,
];
await runCsharpFeature("window", args);
}
/**
@@ -125,40 +70,17 @@ async function setWindowRect(method, value, x, y, width, height) {
* @param {string} state 状态normal/maximize/minimize
*/
async function setWindowState(method, value, state) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
const int SW_NORMAL = 1;
const int SW_MAXIMIZE = 3;
const int SW_MINIMIZE = 6;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
ShowWindow(hwnd, ${
state === "maximize"
? "SW_MAXIMIZE"
: state === "minimize"
? "SW_MINIMIZE"
: "SW_NORMAL"
});
}
}
}
`;
await quickcommand.runCsharp(script);
const args = [
"-type",
"state",
"-method",
method,
"-window",
value,
"-value",
state,
];
await runCsharpFeature("window", args);
}
/**
@@ -167,32 +89,8 @@ async function setWindowState(method, value, state) {
* @param {string} value 查找值handle时为数字
*/
async function closeWindow(method, value) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
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);
const uint WM_CLOSE = 0x0010;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
PostMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
}
}
`;
await quickcommand.runCsharp(script);
const args = ["-type", "close", "-method", method, "-window", value];
await runCsharpFeature("window", args);
}
/**
@@ -201,30 +99,8 @@ async function closeWindow(method, value) {
* @param {string} value 查找值handle时为数字
*/
async function setFocus(method, value) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
SetForegroundWindow(hwnd);
}
}
}
`;
await quickcommand.runCsharp(script);
const args = ["-type", "focus", "-method", method, "-window", value];
await runCsharpFeature("window", args);
}
/**
@@ -234,44 +110,17 @@ async function setFocus(method, value) {
* @param {boolean} hasBorder 是否显示边框
*/
async function setBorder(method, value, hasBorder) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
const int GWL_STYLE = -16;
const int WS_BORDER = 0x00800000;
const int WS_DLGFRAME = 0x00400000;
const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
int style = GetWindowLong(hwnd, GWL_STYLE);
if (${hasBorder}) {
style |= WS_CAPTION;
} else {
style &= ~WS_CAPTION;
}
SetWindowLong(hwnd, GWL_STYLE, style);
}
}
}
`;
await quickcommand.runCsharp(script);
const args = [
"-type",
"border",
"-method",
method,
"-window",
value,
"-value",
hasBorder.toString(),
];
await runCsharpFeature("window", args);
}
/**
@@ -281,44 +130,17 @@ async function setBorder(method, value, hasBorder) {
* @param {boolean} isTransparent 是否点击穿透
*/
async function setClickThrough(method, value, isTransparent) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
const int GWL_EXSTYLE = -20;
const int WS_EX_TRANSPARENT = 0x00000020;
const int WS_EX_LAYERED = 0x80000;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
exStyle |= WS_EX_LAYERED;
if (${isTransparent}) {
exStyle |= WS_EX_TRANSPARENT;
} else {
exStyle &= ~WS_EX_TRANSPARENT;
}
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle);
}
}
}
`;
await quickcommand.runCsharp(script);
const args = [
"-type",
"clickthrough",
"-method",
method,
"-window",
value,
"-value",
isTransparent.toString(),
];
await runCsharpFeature("window", args);
}
/**
@@ -328,167 +150,8 @@ async function setClickThrough(method, value, isTransparent) {
* @returns {Object} 窗口信息
*/
async function getWindowInfo(method, value) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Web.Script.Serialization;
public class Program {
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsZoomed(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
const int GWL_STYLE = -16;
const int GWL_EXSTYLE = -20;
const int WS_BORDER = 0x00800000;
const int WS_CAPTION = 0x00C00000;
const int WS_CHILD = 0x40000000;
const int WS_POPUP = unchecked((int)0x80000000);
const int WS_SYSMENU = 0x00080000;
const int WS_MINIMIZEBOX = 0x00020000;
const int WS_MAXIMIZEBOX = 0x00010000;
const int WS_EX_TOPMOST = 0x00000008;
const int WS_EX_TRANSPARENT = 0x00000020;
const int WS_EX_TOOLWINDOW = 0x00000080;
const int WS_EX_LAYERED = 0x00080000;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
var windowRect = new RECT();
GetWindowRect(hwnd, out windowRect);
var clientRect = new RECT();
GetClientRect(hwnd, out clientRect);
var titleText = new StringBuilder(256);
GetWindowText(hwnd, titleText, 256);
var className = new StringBuilder(256);
GetClassName(hwnd, className, 256);
uint processId = 0;
GetWindowThreadProcessId(hwnd, out processId);
string processName = "";
string processPath = "";
try {
Process process = Process.GetProcessById((int)processId);
processName = process.ProcessName;
var startInfo = new ProcessStartInfo {
FileName = "wmic",
Arguments = string.Format("process where ProcessId={0} get ExecutablePath /value", processId),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using (var proc = Process.Start(startInfo)) {
string output = proc.StandardOutput.ReadToEnd();
if (!string.IsNullOrEmpty(output)) {
string[] lines = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines) {
if (line.StartsWith("ExecutablePath=")) {
processPath = line.Substring("ExecutablePath=".Length);
break;
}
}
}
}
} catch {}
int style = GetWindowLong(hwnd, GWL_STYLE);
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
var data = new {
handle = hwnd.ToInt64(),
processId = processId,
process = new {
name = processName,
path = processPath
},
title = titleText.ToString(),
className = className.ToString(),
window = new {
x = windowRect.Left,
y = windowRect.Top,
width = windowRect.Right - windowRect.Left,
height = windowRect.Bottom - windowRect.Top
},
client = new {
width = clientRect.Right - clientRect.Left,
height = clientRect.Bottom - clientRect.Top
},
state = new {
visible = IsWindowVisible(hwnd),
minimized = IsIconic(hwnd),
maximized = IsZoomed(hwnd),
focused = GetForegroundWindow() == hwnd
},
style = new {
border = (style & WS_BORDER) != 0,
caption = (style & WS_CAPTION) != 0,
child = (style & WS_CHILD) != 0,
popup = (style & WS_POPUP) != 0,
sysmenu = (style & WS_SYSMENU) != 0,
minimizeBox = (style & WS_MINIMIZEBOX) != 0,
maximizeBox = (style & WS_MAXIMIZEBOX) != 0
},
exStyle = new {
topmost = (exStyle & WS_EX_TOPMOST) != 0,
transparent = (exStyle & WS_EX_TRANSPARENT) != 0,
toolWindow = (exStyle & WS_EX_TOOLWINDOW) != 0,
layered = (exStyle & WS_EX_LAYERED) != 0
}
};
var serializer = new JavaScriptSerializer();
Console.WriteLine(serializer.Serialize(data));
}
}
}
`;
const result = await quickcommand.runCsharp(script);
const args = ["-type", "info", "-method", method, "-window", value];
const result = await runCsharpFeature("window", args);
try {
return JSON.parse(result);
} catch (error) {
@@ -503,50 +166,17 @@ async function getWindowInfo(method, value) {
* @param {boolean} visible 是否可见
*/
async function setVisible(method, value, visible) {
const script = `
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Management;
public class Program {
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
public static void Main() {
${findWindowByMethod(method, value)}
if (hwnd != IntPtr.Zero) {
ShowWindow(hwnd, ${visible ? "SW_SHOW" : "SW_HIDE"});
}
}
}
`;
await quickcommand.runCsharp(script);
}
/**
* 根据不同方式查找窗口
* @param {string} method 查找方式title/handle/active
* @param {string} value 查找值handle时为数字
* @returns {string} C#代码片段
*/
function findWindowByMethod(method, value) {
switch (method) {
case "handle":
return `IntPtr hwnd = new IntPtr(${value});`;
case "active":
return `IntPtr hwnd = GetForegroundWindow();`;
default: // title
return `IntPtr hwnd = FindWindow(null, "${value}");`;
}
const args = [
"-type",
"visible",
"-method",
method,
"-window",
value,
"-value",
visible.toString(),
];
await runCsharpFeature("window", args);
}
module.exports = {