调整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,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 = {