建议[获取窗口列表]输入窗口标识增加输出句柄位置信息词典

功能建议 · 31 次浏览
沉没的白鲸 创建于 2天6小时前

目前输出的IList<Window> 可能对于一般人来说难以掌握,敲打了ai得到了以下代码可以得到窗口句柄和位置,也许更方便操作窗口:

 

using System.Windows.Forms;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Windows;
using System.Windows.Interop;
using System.Runtime.InteropServices;
public class WindowInfo
{
// Quicker将会调用的函数。可以根据需要修改返回值类型。

    // 声明 Windows API 函数 GetWindowRect
    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

    // 结构体用来存储窗口位置和大小
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
        public int Width
    {
        get { return Right - Left; }
    }

    public int Height
    {
        get { return Bottom - Top; }
    }
    }
    
public static void Exec(Quicker.Public.IStepContext context)
{
 // 读取动作里的变量值
var  windowList= context.GetVarValue("windowList") as IList<Window> ;



Dictionary<string, string> dict = new Dictionary<string, string>();

foreach (var window in windowList)
{
    IntPtr handle = IntPtr.Zero;

    // 使用 Dispatcher.Invoke 确保在窗口所属的 UI 线程访问
    window.Dispatcher.Invoke(() =>
    {
        var interopHelper = new WindowInteropHelper(window);
        handle = interopHelper.Handle;
        // 获取窗口位置信息
            RECT rect;
            if (GetWindowRect(handle, out rect))
            {
                // 格式化窗口位置和大小信息
                string windowInfo =  rect.Left+","+rect.Top+","+rect.Right+","+ rect.Bottom +","+ rect.Width +","+ rect.Height ;
                // 将句柄和位置信息存入字典(句柄转换为字符串)
                dict.Add(handle.ToString(), windowInfo);
            }
        
    });


}

    // 将生成的存储在上下文中
    context.SetVarValue("windowInfodict", dict); // 向变量里输出值
}

}

 

 


回复内容
CL 1天12小时前
#1

好的,先记下~

回复主贴