mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-29 20:32:44 +08:00
移除quickcommand.runCSharp,模拟操作中win下的屏幕截图使用runCsharpFeature实现
This commit is contained in:
parent
c177584274
commit
272cf488a3
@ -174,7 +174,6 @@ const runCsharpFeature = async (feature, args = [], options = {}) => {
|
|||||||
console.log(featureExePath, args.join(" "));
|
console.log(featureExePath, args.join(" "));
|
||||||
currentChild = child_process.spawn(featureExePath, args, {
|
currentChild = child_process.spawn(featureExePath, args, {
|
||||||
encoding: null,
|
encoding: null,
|
||||||
windowsHide: true,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let stdoutData = Buffer.from([]);
|
let stdoutData = Buffer.from([]);
|
||||||
|
@ -5,6 +5,8 @@ using System.Net.NetworkInformation;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.Drawing.Imaging;
|
||||||
|
|
||||||
public class SystemUtils
|
public class SystemUtils
|
||||||
{
|
{
|
||||||
@ -24,6 +26,47 @@ public class SystemUtils
|
|||||||
[DllImport("kernel32.dll")]
|
[DllImport("kernel32.dll")]
|
||||||
private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
|
private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
static extern IntPtr GetDesktopWindow();
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
static extern IntPtr GetWindowDC(IntPtr hWnd);
|
||||||
|
|
||||||
|
[DllImport("gdi32.dll")]
|
||||||
|
static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
|
||||||
|
int nWidth, int nHeight, IntPtr hdcSrc,
|
||||||
|
int nXSrc, int nYSrc, int dwRop);
|
||||||
|
|
||||||
|
[DllImport("gdi32.dll")]
|
||||||
|
static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
|
||||||
|
|
||||||
|
[DllImport("gdi32.dll")]
|
||||||
|
static extern IntPtr CreateCompatibleDC(IntPtr hDC);
|
||||||
|
|
||||||
|
[DllImport("gdi32.dll")]
|
||||||
|
static extern bool DeleteDC(IntPtr hDC);
|
||||||
|
|
||||||
|
[DllImport("gdi32.dll")]
|
||||||
|
static extern bool DeleteObject(IntPtr hObject);
|
||||||
|
|
||||||
|
[DllImport("gdi32.dll")]
|
||||||
|
static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
public struct RECT
|
||||||
|
{
|
||||||
|
public int left;
|
||||||
|
public int top;
|
||||||
|
public int right;
|
||||||
|
public int bottom;
|
||||||
|
}
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
private static extern int GetWindowRect(IntPtr hWnd, ref RECT rect);
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
private static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
|
||||||
|
|
||||||
private const int SPI_SETDESKWALLPAPER = 20;
|
private const int SPI_SETDESKWALLPAPER = 20;
|
||||||
private const int SPIF_UPDATEINIFILE = 0x01;
|
private const int SPIF_UPDATEINIFILE = 0x01;
|
||||||
private const int SPIF_SENDCHANGE = 0x02;
|
private const int SPIF_SENDCHANGE = 0x02;
|
||||||
@ -122,6 +165,17 @@ public class SystemUtils
|
|||||||
SetBrightness(int.Parse(brightness));
|
SetBrightness(int.Parse(brightness));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "screenshot":
|
||||||
|
string savePath = GetArgumentValue(args, "-path");
|
||||||
|
if (string.IsNullOrEmpty(savePath))
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine("Error: 必须指定保存路径 (-path)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CaptureScreenToFile(savePath);
|
||||||
|
Console.WriteLine("成功保存截图");
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
Console.Error.WriteLine("Error: 不支持的操作类型");
|
Console.Error.WriteLine("Error: 不支持的操作类型");
|
||||||
break;
|
break;
|
||||||
@ -324,6 +378,37 @@ public class SystemUtils
|
|||||||
Console.WriteLine("成功设置亮度");
|
Console.WriteLine("成功设置亮度");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Image CaptureScreen()
|
||||||
|
{
|
||||||
|
IntPtr handle = GetDesktopWindow();
|
||||||
|
IntPtr hdcSrc = GetWindowDC(handle);
|
||||||
|
RECT windowRect = new RECT();
|
||||||
|
GetWindowRect(handle, ref windowRect);
|
||||||
|
int width = windowRect.right - windowRect.left;
|
||||||
|
int height = windowRect.bottom - windowRect.top;
|
||||||
|
|
||||||
|
IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
|
||||||
|
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);
|
||||||
|
IntPtr hOld = SelectObject(hdcDest, hBitmap);
|
||||||
|
|
||||||
|
BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 0x00CC0020);
|
||||||
|
SelectObject(hdcDest, hOld);
|
||||||
|
DeleteDC(hdcDest);
|
||||||
|
|
||||||
|
Image img = Image.FromHbitmap(hBitmap);
|
||||||
|
DeleteObject(hBitmap);
|
||||||
|
ReleaseDC(handle, hdcSrc);
|
||||||
|
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CaptureScreenToFile(string path)
|
||||||
|
{
|
||||||
|
Image img = CaptureScreen();
|
||||||
|
img.Save(path, ImageFormat.Png);
|
||||||
|
img.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
private static void ShowHelp()
|
private static void ShowHelp()
|
||||||
{
|
{
|
||||||
string help = @"
|
string help = @"
|
||||||
@ -378,6 +463,11 @@ utils.exe -type <操作类型> [参数...]
|
|||||||
-level <亮度> 亮度级别(0-100)
|
-level <亮度> 亮度级别(0-100)
|
||||||
示例: utils.exe -type brightness -level 75
|
示例: utils.exe -type brightness -level 75
|
||||||
|
|
||||||
|
8. screenshot - 屏幕截图
|
||||||
|
参数:
|
||||||
|
-path <保存路径> 截图保存路径
|
||||||
|
示例: utils.exe -type screenshot -path ""C:\screenshot.png""
|
||||||
|
|
||||||
注意事项:
|
注意事项:
|
||||||
--------
|
--------
|
||||||
1. 某些操作可能需要管理员权限
|
1. 某些操作可能需要管理员权限
|
||||||
|
@ -3,122 +3,29 @@ const { promisify } = require("util");
|
|||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const os = require("os");
|
const os = require("os");
|
||||||
|
const { runCsharpFeature } = require("../../csharp");
|
||||||
|
|
||||||
const execFileAsync = promisify(execFile);
|
const execFileAsync = promisify(execFile);
|
||||||
const readFileAsync = promisify(fs.readFile);
|
const readFileAsync = promisify(fs.readFile);
|
||||||
const unlinkAsync = promisify(fs.unlink);
|
const unlinkAsync = promisify(fs.unlink);
|
||||||
|
|
||||||
// Windows C# 截图代码
|
|
||||||
const csharpScript = `
|
|
||||||
using System;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Drawing;
|
|
||||||
using System.Drawing.Imaging;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.VisualBasic;
|
|
||||||
|
|
||||||
public class ScreenCapture
|
|
||||||
{
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
static extern IntPtr GetDC(IntPtr hwnd);
|
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
static extern IntPtr GetDesktopWindow();
|
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
static extern IntPtr GetWindowDC(IntPtr hWnd);
|
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
|
|
||||||
|
|
||||||
[DllImport("gdi32.dll")]
|
|
||||||
static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
|
|
||||||
int nWidth, int nHeight, IntPtr hdcSrc,
|
|
||||||
int nXSrc, int nYSrc, int dwRop);
|
|
||||||
|
|
||||||
[DllImport("gdi32.dll")]
|
|
||||||
static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);
|
|
||||||
|
|
||||||
[DllImport("gdi32.dll")]
|
|
||||||
static extern IntPtr CreateCompatibleDC(IntPtr hDC);
|
|
||||||
|
|
||||||
[DllImport("gdi32.dll")]
|
|
||||||
static extern bool DeleteDC(IntPtr hDC);
|
|
||||||
|
|
||||||
[DllImport("gdi32.dll")]
|
|
||||||
static extern bool DeleteObject(IntPtr hObject);
|
|
||||||
|
|
||||||
[DllImport("gdi32.dll")]
|
|
||||||
static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
|
|
||||||
|
|
||||||
public Image CaptureScreen()
|
|
||||||
{
|
|
||||||
return CaptureWindow(GetDesktopWindow());
|
|
||||||
}
|
|
||||||
|
|
||||||
private Image CaptureWindow(IntPtr handle)
|
|
||||||
{
|
|
||||||
IntPtr hdcSrc = GetWindowDC(handle);
|
|
||||||
User32.RECT windowRect = new User32.RECT();
|
|
||||||
User32.GetWindowRect(handle, ref windowRect);
|
|
||||||
int width = windowRect.right - windowRect.left;
|
|
||||||
int height = windowRect.bottom - windowRect.top;
|
|
||||||
|
|
||||||
IntPtr hdcDest = CreateCompatibleDC(hdcSrc);
|
|
||||||
IntPtr hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);
|
|
||||||
IntPtr hOld = SelectObject(hdcDest, hBitmap);
|
|
||||||
|
|
||||||
BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, 0x00CC0020);
|
|
||||||
SelectObject(hdcDest, hOld);
|
|
||||||
DeleteDC(hdcDest);
|
|
||||||
|
|
||||||
Image img = Image.FromHbitmap(hBitmap);
|
|
||||||
DeleteObject(hBitmap);
|
|
||||||
ReleaseDC(handle, hdcSrc);
|
|
||||||
|
|
||||||
return img;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void CaptureScreenToFile(string filename, ImageFormat format)
|
|
||||||
{
|
|
||||||
Image img = CaptureScreen();
|
|
||||||
img.Save(filename, format);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class User32
|
|
||||||
{
|
|
||||||
public struct RECT
|
|
||||||
{
|
|
||||||
public int left;
|
|
||||||
public int top;
|
|
||||||
public int right;
|
|
||||||
public int bottom;
|
|
||||||
}
|
|
||||||
|
|
||||||
[DllImport("user32.dll")]
|
|
||||||
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Main()
|
|
||||||
{
|
|
||||||
ScreenCapture sc = new ScreenCapture();
|
|
||||||
sc.CaptureScreenToFile(">>tempscreenshot<<", ImageFormat.Png);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
// Windows 截图实现
|
// Windows 截图实现
|
||||||
async function captureWindowsScreen() {
|
async function captureWindowsScreen() {
|
||||||
const tmpFile = path.join(os.tmpdir(), `screen-${Date.now()}.png`);
|
const tmpFile = path.join(os.tmpdir(), `screen-${Date.now()}.png`);
|
||||||
try {
|
try {
|
||||||
await window.quickcommand.runCsharp(
|
await runCsharpFeature("utils", [
|
||||||
csharpScript.replace(">>tempscreenshot<<", tmpFile.replace(/\\/g, "\\\\"))
|
"-type",
|
||||||
);
|
"screenshot",
|
||||||
|
"-path",
|
||||||
|
tmpFile,
|
||||||
|
]);
|
||||||
const imageBuffer = await readFileAsync(tmpFile);
|
const imageBuffer = await readFileAsync(tmpFile);
|
||||||
return `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
return `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Windows截图失败:", error);
|
console.error("Windows截图失败:", error);
|
||||||
return null;
|
return null;
|
||||||
|
} finally {
|
||||||
|
await unlinkAsync(tmpFile).catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,12 +42,5 @@ export const scriptCommands = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
|
||||||
value: "quickcommand.runCsharp",
|
|
||||||
label: "执行C#脚本",
|
|
||||||
icon: "script",
|
|
||||||
outputVariable: "result",
|
|
||||||
saveOutput: true,
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
23
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
23
src/plugins/monaco/types/quickcommand.api.d.ts
vendored
@ -347,29 +347,6 @@ interface quickcommandApi {
|
|||||||
*/
|
*/
|
||||||
runPowerShell(script: string): Promise<string>;
|
runPowerShell(script: string): Promise<string>;
|
||||||
|
|
||||||
/**
|
|
||||||
* 运行C#脚本,并返回运行结果 (Promise)
|
|
||||||
*
|
|
||||||
* 需要安装.NET Framework
|
|
||||||
*
|
|
||||||
* ```js
|
|
||||||
* const script = `using System;
|
|
||||||
* class Program {
|
|
||||||
* static void Main(string[] args) {
|
|
||||||
* Console.WriteLine("Hello, World!");
|
|
||||||
* }
|
|
||||||
* }`
|
|
||||||
* quickcommand.runCsharp(script).then(res => {
|
|
||||||
* console.log(res)
|
|
||||||
* }).catch(err => {
|
|
||||||
* console.log(err)
|
|
||||||
* })
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* @param script C# 脚本代码
|
|
||||||
*/
|
|
||||||
runCsharp(script: string): Promise<string>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MacOS 下运行 AppleScript 脚本并返回运行结果 (Promise)
|
* MacOS 下运行 AppleScript 脚本并返回运行结果 (Promise)
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user