mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-28 20:02:44 +08:00
发送消息、窗口管理统一通过句柄操作
This commit is contained in:
parent
7a7cb8dd54
commit
0ee3647261
File diff suppressed because it is too large
Load Diff
@ -7,333 +7,340 @@ using System.Collections.Generic;
|
||||
|
||||
public class WindowManager
|
||||
{
|
||||
#region Win32 API
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
|
||||
#region Win32 API
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetForegroundWindow(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetForegroundWindow();
|
||||
[DllImport("user32.dll")]
|
||||
private static extern IntPtr GetForegroundWindow();
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsWindowVisible(IntPtr hWnd);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsWindowVisible(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsIconic(IntPtr hWnd);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsIconic(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsZoomed(IntPtr hWnd);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsZoomed(IntPtr hWnd);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool IsWindow(IntPtr hWnd);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct RECT
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct RECT
|
||||
{
|
||||
public int Left;
|
||||
public int Top;
|
||||
public int Right;
|
||||
public int Bottom;
|
||||
}
|
||||
|
||||
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
|
||||
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
|
||||
private const uint SWP_NOMOVE = 0x0002;
|
||||
private const uint SWP_NOSIZE = 0x0001;
|
||||
private const uint WM_CLOSE = 0x0010;
|
||||
private const int GWL_STYLE = -16;
|
||||
private const int GWL_EXSTYLE = -20;
|
||||
private const int WS_BORDER = 0x00800000;
|
||||
private const int WS_CAPTION = 0x00C00000;
|
||||
private const int WS_CHILD = 0x40000000;
|
||||
private const int WS_POPUP = unchecked((int)0x80000000);
|
||||
private const int WS_SYSMENU = 0x00080000;
|
||||
private const int WS_MINIMIZEBOX = 0x00020000;
|
||||
private const int WS_MAXIMIZEBOX = 0x00010000;
|
||||
private const int WS_EX_TOPMOST = 0x00000008;
|
||||
private const int WS_EX_TRANSPARENT = 0x00000020;
|
||||
private const int WS_EX_TOOLWINDOW = 0x00000080;
|
||||
private const int WS_EX_LAYERED = 0x00080000;
|
||||
private const uint LWA_ALPHA = 0x2;
|
||||
private const int SW_HIDE = 0;
|
||||
private const int SW_SHOW = 5;
|
||||
private const int SW_NORMAL = 1;
|
||||
private const int SW_MAXIMIZE = 3;
|
||||
private const int SW_MINIMIZE = 6;
|
||||
private const int SW_RESTORE = 9;
|
||||
|
||||
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
||||
#endregion
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
if (args.Length == 0 || args[0] == "-h" || args[0] == "--help")
|
||||
{
|
||||
public int Left;
|
||||
public int Top;
|
||||
public int Right;
|
||||
public int Bottom;
|
||||
ShowHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
|
||||
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
|
||||
private const uint SWP_NOMOVE = 0x0002;
|
||||
private const uint SWP_NOSIZE = 0x0001;
|
||||
private const uint WM_CLOSE = 0x0010;
|
||||
private const int GWL_STYLE = -16;
|
||||
private const int GWL_EXSTYLE = -20;
|
||||
private const int WS_BORDER = 0x00800000;
|
||||
private const int WS_CAPTION = 0x00C00000;
|
||||
private const int WS_CHILD = 0x40000000;
|
||||
private const int WS_POPUP = unchecked((int)0x80000000);
|
||||
private const int WS_SYSMENU = 0x00080000;
|
||||
private const int WS_MINIMIZEBOX = 0x00020000;
|
||||
private const int WS_MAXIMIZEBOX = 0x00010000;
|
||||
private const int WS_EX_TOPMOST = 0x00000008;
|
||||
private const int WS_EX_TRANSPARENT = 0x00000020;
|
||||
private const int WS_EX_TOOLWINDOW = 0x00000080;
|
||||
private const int WS_EX_LAYERED = 0x00080000;
|
||||
private const uint LWA_ALPHA = 0x2;
|
||||
private const int SW_HIDE = 0;
|
||||
private const int SW_SHOW = 5;
|
||||
private const int SW_NORMAL = 1;
|
||||
private const int SW_MAXIMIZE = 3;
|
||||
private const int SW_MINIMIZE = 6;
|
||||
private const int SW_RESTORE = 9;
|
||||
|
||||
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
||||
#endregion
|
||||
|
||||
public static void Main(string[] args)
|
||||
try
|
||||
{
|
||||
if (args.Length == 0 || args[0] == "-h" || args[0] == "--help")
|
||||
List<IntPtr> targetWindows = GetTargetWindows(args);
|
||||
if (targetWindows.Count == 0)
|
||||
{
|
||||
throw new Exception("未找到目标窗口");
|
||||
}
|
||||
|
||||
string type = GetArgumentValue(args, "-type");
|
||||
if (type.ToLower() == "info")
|
||||
{
|
||||
var allWindowInfo = new List<Dictionary<string, object>>();
|
||||
foreach (IntPtr windowHandle in targetWindows)
|
||||
{
|
||||
ShowHelp();
|
||||
return;
|
||||
var windowInfo = GetBasicWindowInfo(windowHandle);
|
||||
if (windowInfo != null)
|
||||
{
|
||||
allWindowInfo.Add(windowInfo);
|
||||
}
|
||||
}
|
||||
var serializer = new JavaScriptSerializer();
|
||||
Console.WriteLine(serializer.Serialize(allWindowInfo));
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
IntPtr targetHandle = targetWindows[0];
|
||||
Dictionary<string, object> operatedWindow = null;
|
||||
|
||||
switch (type.ToLower())
|
||||
{
|
||||
case "topmost":
|
||||
bool isTopMost = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
SetWindowPos(targetHandle, isTopMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "opacity":
|
||||
int opacity = int.Parse(GetArgumentValue(args, "-value") ?? "100");
|
||||
SetWindowLong(targetHandle, GWL_EXSTYLE, GetWindowLong(targetHandle, GWL_EXSTYLE) | WS_EX_LAYERED);
|
||||
SetLayeredWindowAttributes(targetHandle, 0, (byte)(opacity * 2.55), LWA_ALPHA);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "rect":
|
||||
string[] rectValues = (GetArgumentValue(args, "-value") ?? "").Split(',');
|
||||
if (rectValues.Length == 4)
|
||||
{
|
||||
int x = int.Parse(rectValues[0]);
|
||||
int y = int.Parse(rectValues[1]);
|
||||
int width = int.Parse(rectValues[2]);
|
||||
int height = int.Parse(rectValues[3]);
|
||||
MoveWindow(targetHandle, x, y, width, height, true);
|
||||
}
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "state":
|
||||
string state = GetArgumentValue(args, "-value") ?? "normal";
|
||||
int cmdShow = state == "maximize" ? SW_MAXIMIZE :
|
||||
state == "minimize" ? SW_MINIMIZE : SW_NORMAL;
|
||||
ShowWindow(targetHandle, cmdShow);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "visible":
|
||||
bool visible = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
ShowWindow(targetHandle, visible ? SW_SHOW : SW_HIDE);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "close":
|
||||
PostMessage(targetHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "focus":
|
||||
if (IsIconic(targetHandle))
|
||||
{
|
||||
ShowWindow(targetHandle, SW_RESTORE);
|
||||
}
|
||||
SetForegroundWindow(targetHandle);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "border":
|
||||
bool hasBorder = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
int style = GetWindowLong(targetHandle, GWL_STYLE);
|
||||
style = hasBorder ? style | WS_CAPTION : style & ~WS_CAPTION;
|
||||
SetWindowLong(targetHandle, GWL_STYLE, style);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "clickthrough":
|
||||
bool isTransparent = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
int exStyle = GetWindowLong(targetHandle, GWL_EXSTYLE);
|
||||
exStyle |= WS_EX_LAYERED;
|
||||
exStyle = isTransparent ? exStyle | WS_EX_TRANSPARENT : exStyle & ~WS_EX_TRANSPARENT;
|
||||
SetWindowLong(targetHandle, GWL_EXSTYLE, exStyle);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "info":
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
default:
|
||||
Console.Error.WriteLine("Error: 不支持的操作类型");
|
||||
return;
|
||||
}
|
||||
|
||||
if (operatedWindow != null)
|
||||
{
|
||||
var serializer = new JavaScriptSerializer();
|
||||
Console.WriteLine(serializer.Serialize(operatedWindow));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Error.WriteLine(string.Format("Error: {0}", ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
private static List<IntPtr> GetTargetWindows(string[] args)
|
||||
{
|
||||
List<IntPtr> targetWindows = new List<IntPtr>();
|
||||
string method = GetArgumentValue(args, "-method") ?? "title";
|
||||
string value = GetArgumentValue(args, "-window") ?? "";
|
||||
|
||||
switch (method.ToLower())
|
||||
{
|
||||
case "handle":
|
||||
IntPtr handle = new IntPtr(long.Parse(value));
|
||||
if (!IsWindow(handle))
|
||||
{
|
||||
List<IntPtr> targetWindows = GetTargetWindows(args);
|
||||
if (targetWindows.Count == 0)
|
||||
{
|
||||
throw new Exception("未找到目标窗口");
|
||||
}
|
||||
|
||||
string type = GetArgumentValue(args, "-type");
|
||||
if (type.ToLower() == "info")
|
||||
{
|
||||
var allWindowInfo = new List<Dictionary<string, object>>();
|
||||
foreach (IntPtr windowHandle in targetWindows)
|
||||
{
|
||||
var windowInfo = GetBasicWindowInfo(windowHandle);
|
||||
if (windowInfo != null)
|
||||
{
|
||||
allWindowInfo.Add(windowInfo);
|
||||
}
|
||||
}
|
||||
var serializer = new JavaScriptSerializer();
|
||||
Console.WriteLine(serializer.Serialize(allWindowInfo));
|
||||
return;
|
||||
}
|
||||
|
||||
IntPtr targetHandle = targetWindows[0];
|
||||
Dictionary<string, object> operatedWindow = null;
|
||||
|
||||
switch (type.ToLower())
|
||||
{
|
||||
case "topmost":
|
||||
bool isTopMost = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
SetWindowPos(targetHandle, isTopMost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "opacity":
|
||||
int opacity = int.Parse(GetArgumentValue(args, "-value") ?? "100");
|
||||
SetWindowLong(targetHandle, GWL_EXSTYLE, GetWindowLong(targetHandle, GWL_EXSTYLE) | WS_EX_LAYERED);
|
||||
SetLayeredWindowAttributes(targetHandle, 0, (byte)(opacity * 2.55), LWA_ALPHA);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "rect":
|
||||
string[] rectValues = (GetArgumentValue(args, "-value") ?? "").Split(',');
|
||||
if (rectValues.Length == 4)
|
||||
{
|
||||
int x = int.Parse(rectValues[0]);
|
||||
int y = int.Parse(rectValues[1]);
|
||||
int width = int.Parse(rectValues[2]);
|
||||
int height = int.Parse(rectValues[3]);
|
||||
MoveWindow(targetHandle, x, y, width, height, true);
|
||||
}
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "state":
|
||||
string state = GetArgumentValue(args, "-value") ?? "normal";
|
||||
int cmdShow = state == "maximize" ? SW_MAXIMIZE :
|
||||
state == "minimize" ? SW_MINIMIZE : SW_NORMAL;
|
||||
ShowWindow(targetHandle, cmdShow);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "visible":
|
||||
bool visible = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
ShowWindow(targetHandle, visible ? SW_SHOW : SW_HIDE);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "close":
|
||||
PostMessage(targetHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "focus":
|
||||
if (IsIconic(targetHandle))
|
||||
{
|
||||
ShowWindow(targetHandle, SW_RESTORE);
|
||||
}
|
||||
SetForegroundWindow(targetHandle);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "border":
|
||||
bool hasBorder = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
int style = GetWindowLong(targetHandle, GWL_STYLE);
|
||||
style = hasBorder ? style | WS_CAPTION : style & ~WS_CAPTION;
|
||||
SetWindowLong(targetHandle, GWL_STYLE, style);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "clickthrough":
|
||||
bool isTransparent = bool.Parse(GetArgumentValue(args, "-value") ?? "true");
|
||||
int exStyle = GetWindowLong(targetHandle, GWL_EXSTYLE);
|
||||
exStyle |= WS_EX_LAYERED;
|
||||
exStyle = isTransparent ? exStyle | WS_EX_TRANSPARENT : exStyle & ~WS_EX_TRANSPARENT;
|
||||
SetWindowLong(targetHandle, GWL_EXSTYLE, exStyle);
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
case "info":
|
||||
operatedWindow = GetBasicWindowInfo(targetHandle);
|
||||
break;
|
||||
|
||||
default:
|
||||
Console.Error.WriteLine("Error: 不支持的操作类型");
|
||||
return;
|
||||
}
|
||||
|
||||
if (operatedWindow != null)
|
||||
{
|
||||
var serializer = new JavaScriptSerializer();
|
||||
Console.WriteLine(serializer.Serialize(operatedWindow));
|
||||
}
|
||||
throw new Exception("指定的句柄不是一个有效的窗口句柄");
|
||||
}
|
||||
catch (Exception ex)
|
||||
targetWindows.Add(handle);
|
||||
break;
|
||||
|
||||
case "active":
|
||||
targetWindows.Add(GetForegroundWindow());
|
||||
break;
|
||||
|
||||
case "process":
|
||||
var processes = Process.GetProcessesByName(value);
|
||||
foreach (var process in processes)
|
||||
{
|
||||
Console.Error.WriteLine(string.Format("Error: {0}", ex.Message));
|
||||
if (process.MainWindowHandle != IntPtr.Zero)
|
||||
{
|
||||
targetWindows.Add(process.MainWindowHandle);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "class":
|
||||
EnumWindows((hwnd, param) =>
|
||||
{
|
||||
if (!IsWindowVisible(hwnd))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
StringBuilder className = new StringBuilder(256);
|
||||
GetClassName(hwnd, className, className.Capacity);
|
||||
string windowClassName = className.ToString();
|
||||
|
||||
if (windowClassName.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
targetWindows.Add(hwnd);
|
||||
}
|
||||
return true;
|
||||
}, IntPtr.Zero);
|
||||
break;
|
||||
|
||||
case "title":
|
||||
default:
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
EnumWindows((hwnd, param) =>
|
||||
{
|
||||
StringBuilder title = new StringBuilder(256);
|
||||
GetWindowText(hwnd, title, title.Capacity);
|
||||
string windowTitle = title.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(windowTitle) &&
|
||||
windowTitle.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
targetWindows.Add(hwnd);
|
||||
}
|
||||
return true;
|
||||
}, IntPtr.Zero);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
private static List<IntPtr> GetTargetWindows(string[] args)
|
||||
if (targetWindows.Count == 0)
|
||||
{
|
||||
List<IntPtr> targetWindows = new List<IntPtr>();
|
||||
string method = GetArgumentValue(args, "-method") ?? "title";
|
||||
string value = GetArgumentValue(args, "-window") ?? "";
|
||||
|
||||
switch (method.ToLower())
|
||||
{
|
||||
case "handle":
|
||||
targetWindows.Add(new IntPtr(long.Parse(value)));
|
||||
break;
|
||||
|
||||
case "active":
|
||||
targetWindows.Add(GetForegroundWindow());
|
||||
break;
|
||||
|
||||
case "process":
|
||||
var processes = Process.GetProcessesByName(value);
|
||||
foreach (var process in processes)
|
||||
{
|
||||
if (process.MainWindowHandle != IntPtr.Zero)
|
||||
{
|
||||
targetWindows.Add(process.MainWindowHandle);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "class":
|
||||
EnumWindows((hwnd, param) =>
|
||||
{
|
||||
if (!IsWindowVisible(hwnd))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
StringBuilder className = new StringBuilder(256);
|
||||
GetClassName(hwnd, className, className.Capacity);
|
||||
string windowClassName = className.ToString();
|
||||
|
||||
if (windowClassName.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
targetWindows.Add(hwnd);
|
||||
}
|
||||
return true;
|
||||
}, IntPtr.Zero);
|
||||
break;
|
||||
|
||||
case "title":
|
||||
default:
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
{
|
||||
EnumWindows((hwnd, param) =>
|
||||
{
|
||||
StringBuilder title = new StringBuilder(256);
|
||||
GetWindowText(hwnd, title, title.Capacity);
|
||||
string windowTitle = title.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(windowTitle) &&
|
||||
windowTitle.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
{
|
||||
targetWindows.Add(hwnd);
|
||||
}
|
||||
return true;
|
||||
}, IntPtr.Zero);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (targetWindows.Count == 0)
|
||||
{
|
||||
throw new Exception("未找到匹配的窗口");
|
||||
}
|
||||
|
||||
return targetWindows;
|
||||
throw new Exception("未找到匹配的窗口");
|
||||
}
|
||||
|
||||
private static Dictionary<string, object> GetBasicWindowInfo(IntPtr hwnd)
|
||||
return targetWindows;
|
||||
}
|
||||
|
||||
private static Dictionary<string, object> GetBasicWindowInfo(IntPtr hwnd)
|
||||
{
|
||||
StringBuilder title = new StringBuilder(256);
|
||||
StringBuilder className = new StringBuilder(256);
|
||||
GetWindowText(hwnd, title, title.Capacity);
|
||||
GetClassName(hwnd, className, className.Capacity);
|
||||
|
||||
// 获取窗口位置和大小
|
||||
RECT rect = new RECT();
|
||||
GetWindowRect(hwnd, out rect);
|
||||
|
||||
// 获取进程信息
|
||||
uint processId = 0;
|
||||
GetWindowThreadProcessId(hwnd, out processId);
|
||||
string processName = "";
|
||||
string processPath = "";
|
||||
try
|
||||
{
|
||||
StringBuilder title = new StringBuilder(256);
|
||||
StringBuilder className = new StringBuilder(256);
|
||||
GetWindowText(hwnd, title, title.Capacity);
|
||||
GetClassName(hwnd, className, className.Capacity);
|
||||
var process = Process.GetProcessById((int)processId);
|
||||
processName = process.ProcessName;
|
||||
processPath = process.MainModule.FileName;
|
||||
}
|
||||
catch { }
|
||||
|
||||
// 获取窗口位置和大小
|
||||
RECT rect = new RECT();
|
||||
GetWindowRect(hwnd, out rect);
|
||||
|
||||
// 获取进程信息
|
||||
uint processId = 0;
|
||||
GetWindowThreadProcessId(hwnd, out processId);
|
||||
string processName = "";
|
||||
string processPath = "";
|
||||
try
|
||||
{
|
||||
var process = Process.GetProcessById((int)processId);
|
||||
processName = process.ProcessName;
|
||||
processPath = process.MainModule.FileName;
|
||||
}
|
||||
catch { }
|
||||
|
||||
return new Dictionary<string, object>
|
||||
return new Dictionary<string, object>
|
||||
{
|
||||
{ "handle", hwnd.ToInt64() },
|
||||
{ "title", title.ToString() },
|
||||
@ -345,21 +352,21 @@ public class WindowManager
|
||||
{ "processName", processName },
|
||||
{ "processPath", processPath }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetArgumentValue(string[] args, string key)
|
||||
private static string GetArgumentValue(string[] args, string key)
|
||||
{
|
||||
int index = Array.IndexOf(args, key);
|
||||
if (index >= 0 && index < args.Length - 1)
|
||||
{
|
||||
int index = Array.IndexOf(args, key);
|
||||
if (index >= 0 && index < args.Length - 1)
|
||||
{
|
||||
return args[index + 1];
|
||||
}
|
||||
return null;
|
||||
return args[index + 1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void ShowHelp()
|
||||
{
|
||||
string help = @"
|
||||
private static void ShowHelp()
|
||||
{
|
||||
string help = @"
|
||||
Windows 窗口管理工具使用说明
|
||||
==========================
|
||||
|
||||
@ -455,6 +462,6 @@ window.exe -type <操作类型> [参数...]
|
||||
1. 窗口标题、类名支持模糊匹配,active方式可不提供window参数
|
||||
2. 只有info操作会返回所有匹配窗口的信息,其他操作只会操作第一个匹配的窗口
|
||||
";
|
||||
Console.WriteLine(help);
|
||||
}
|
||||
Console.WriteLine(help);
|
||||
}
|
||||
}
|
||||
|
@ -123,7 +123,10 @@ async function runAutomation(
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
error = err.toString().trim();
|
||||
error = err
|
||||
.toString()
|
||||
.replace(/^Error: /, "")
|
||||
.trim();
|
||||
}
|
||||
|
||||
if (type === "inspect") return { error };
|
||||
|
@ -2,7 +2,7 @@ const { runCsharpFeature } = require("../../csharp");
|
||||
|
||||
/**
|
||||
* 执行消息发送操作
|
||||
* @param {string} type - 操作类型, 可选值: "keyboard"|"mouse"|"inspect"
|
||||
* @param {string} type - 操作类型, 可选值: "keyboard"|"mouse"|"list"
|
||||
* @param {Object} params - 参数对象
|
||||
* @param {string} params.method - 查找方式:"title"|"handle"|"process"|"class"|"active"
|
||||
* @param {string} params.window - 窗口标题、句柄、进程名、类名
|
||||
@ -47,7 +47,7 @@ async function runSendMessage(type, params = {}) {
|
||||
}
|
||||
break;
|
||||
|
||||
case "inspect":
|
||||
case "list":
|
||||
if (filter) {
|
||||
args.push("-filter", filter);
|
||||
}
|
||||
@ -61,54 +61,53 @@ async function runSendMessage(type, params = {}) {
|
||||
try {
|
||||
const result = await runCsharpFeature("sendmessage", args);
|
||||
if (result) {
|
||||
const jsonResult = JSON.parse(result);
|
||||
if (type === "inspect") {
|
||||
return jsonResult;
|
||||
}
|
||||
return { success: true, control: jsonResult };
|
||||
const resultStr = result.toString().trim();
|
||||
if (type === "list") return JSON.parse(resultStr);
|
||||
if (resultStr === "true") return { success: true };
|
||||
}
|
||||
} catch (err) {
|
||||
error = err
|
||||
.toString()
|
||||
.replace(/^Error: /, "")
|
||||
.trim();
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
if (type === "inspect") return [];
|
||||
if (type === "list") return [];
|
||||
return { success: false, error };
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
sendKeys: (method, window, keys, options = {}) =>
|
||||
sendKeys: (windowHandle, keys, options = {}) =>
|
||||
runSendMessage("keyboard", {
|
||||
method,
|
||||
window,
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
action: "keys",
|
||||
value: keys,
|
||||
options,
|
||||
}),
|
||||
|
||||
sendText: (method, window, text, options = {}) =>
|
||||
sendText: (windowHandle, text, options = {}) =>
|
||||
runSendMessage("keyboard", {
|
||||
method,
|
||||
window,
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
action: "text",
|
||||
value: text,
|
||||
options,
|
||||
}),
|
||||
|
||||
click: (method, window, action = "click", options = {}) =>
|
||||
click: (windowHandle, action = "click", options = {}) =>
|
||||
runSendMessage("mouse", {
|
||||
method,
|
||||
window,
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
action,
|
||||
options,
|
||||
}),
|
||||
|
||||
inspectWindow: (method, window, options = {}) =>
|
||||
runSendMessage("inspect", {
|
||||
method,
|
||||
window,
|
||||
listControls: (windowHandle, options = {}) =>
|
||||
runSendMessage("list", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
options,
|
||||
}),
|
||||
};
|
||||
|
@ -57,7 +57,7 @@ async function runWindow(type, params = {}) {
|
||||
if (type === "info") {
|
||||
return jsonResult;
|
||||
}
|
||||
return { success: true, window: jsonResult };
|
||||
return { success: true };
|
||||
}
|
||||
} catch (err) {
|
||||
error = err
|
||||
@ -72,27 +72,63 @@ async function runWindow(type, params = {}) {
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setTopMost: (method, window, isTopMost) =>
|
||||
runWindow("topmost", { method, window, value: isTopMost }),
|
||||
setTopMost: (windowHandle, isTopMost) =>
|
||||
runWindow("topmost", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
value: isTopMost,
|
||||
}),
|
||||
|
||||
setOpacity: (method, window, opacity) =>
|
||||
runWindow("opacity", { method, window, value: opacity }),
|
||||
setOpacity: (windowHandle, opacity) =>
|
||||
runWindow("opacity", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
value: opacity,
|
||||
}),
|
||||
|
||||
setWindowRect: (method, window, x, y, width, height) =>
|
||||
runWindow("rect", { method, window, value: { x, y, width, height } }),
|
||||
setWindowRect: (windowHandle, x, y, width, height) =>
|
||||
runWindow("rect", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
value: { x, y, width, height },
|
||||
}),
|
||||
|
||||
setWindowState: (method, window, state) =>
|
||||
runWindow("state", { method, window, value: state }),
|
||||
setWindowState: (windowHandle, state) =>
|
||||
runWindow("state", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
value: state,
|
||||
}),
|
||||
|
||||
closeWindow: (method, window) => runWindow("close", { method, window }),
|
||||
closeWindow: (windowHandle) =>
|
||||
runWindow("close", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
}),
|
||||
|
||||
setFocus: (method, window) => runWindow("focus", { method, window }),
|
||||
setFocus: (windowHandle) =>
|
||||
runWindow("focus", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
}),
|
||||
|
||||
setBorder: (method, window, hasBorder) =>
|
||||
runWindow("border", { method, window, value: hasBorder }),
|
||||
setBorder: (windowHandle, hasBorder) =>
|
||||
runWindow("border", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
value: hasBorder,
|
||||
}),
|
||||
|
||||
setClickThrough: (method, window, isTransparent) =>
|
||||
runWindow("clickthrough", { method, window, value: isTransparent }),
|
||||
setClickThrough: (windowHandle, isTransparent) =>
|
||||
runWindow("clickthrough", {
|
||||
method: windowHandle ? "handle" : "active",
|
||||
window: windowHandle,
|
||||
value: isTransparent,
|
||||
}),
|
||||
|
||||
getWindowInfo: (method, window) => runWindow("info", { method, window }),
|
||||
getWindowInfo: (method, window) =>
|
||||
runWindow("info", {
|
||||
method,
|
||||
window,
|
||||
}),
|
||||
};
|
||||
|
@ -177,15 +177,19 @@ const searchWindowConfig = [
|
||||
},
|
||||
];
|
||||
|
||||
const searchElementConfig = [
|
||||
const windowHandleConfig = [
|
||||
{
|
||||
label: "窗口句柄",
|
||||
component: "VariableInput",
|
||||
icon: "window",
|
||||
width: 12,
|
||||
placeholder: "留空则使用当前活动窗口",
|
||||
placeholder: "可从搜索/选择窗口获取,留空则使用当前活动窗口",
|
||||
defaultValue: newVarInputVal("str", ""),
|
||||
},
|
||||
];
|
||||
|
||||
const searchElementConfig = [
|
||||
windowHandleConfig,
|
||||
{
|
||||
label: "元素查找方式",
|
||||
component: "q-select",
|
||||
@ -267,7 +271,7 @@ export const windowsCommands = {
|
||||
value: "quickcomposer.windows.window.setTopMost",
|
||||
label: "窗口控制",
|
||||
icon: "window",
|
||||
config: searchWindowConfig,
|
||||
config: windowHandleConfig,
|
||||
subCommands: [
|
||||
{
|
||||
value: "quickcomposer.windows.window.setTopMost",
|
||||
@ -630,14 +634,14 @@ export const windowsCommands = {
|
||||
},
|
||||
// sendmessage
|
||||
{
|
||||
value: "quickcomposer.windows.sendmessage.inspectWindow",
|
||||
value: "quickcomposer.windows.sendmessage.listControls",
|
||||
label: "发送控制消息",
|
||||
icon: "smart_button",
|
||||
isAsync: true,
|
||||
config: searchWindowConfig,
|
||||
config: windowHandleConfig,
|
||||
subCommands: [
|
||||
{
|
||||
value: "quickcomposer.windows.sendmessage.inspectWindow",
|
||||
value: "quickcomposer.windows.sendmessage.listControls",
|
||||
label: "获取控件树",
|
||||
icon: "account_tree",
|
||||
outputVariable: "controlsTree",
|
||||
|
Loading…
x
Reference in New Issue
Block a user