如何使用 .net 获取 Windows 资源管理器的位置?

How to get Windows explorer’s position using .net?

我正在开发一个应用程序,该应用程序在前台有 windows 资源管理器 window 时触发。我的应用程序触发了一个 window(表单),它将放置在屏幕上打开的 windows 资源管理器附近(计划将其保持在搜索选项下方)。

但我没有得到任何东西来获得前景 window 的位置 "windows explorer" window。

有什么方法可以使用 .net 读取当前前景“Windows Explorer”window 的位置吗?

您可以使用非托管代码执行此操作。

创建 class:

    class RectMethods
    {
        // http://msdn.microsoft.com/en-us/library/ms633519(VS.85).aspx
        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        // http://msdn.microsoft.com/en-us/library/a5ch4fda(VS.80).aspx
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
    }

然后,识别资源管理器进程以获取您的句柄,并获取 window 的坐标和大小,然后您可以从那里做您想做的事:

            var processes = System.Diagnostics.Process.GetProcesses();
            foreach (var process in processes)
            {
                if (process.ProcessName == "explorer")
                {
                    var hWnd = process.Handle;
                    RectMethods.RECT rect = new RectMethods.RECT();
                    if (RectMethods.GetWindowRect(hWnd, ref rect))
                    {
                        Size size = new Size(rect.Right - rect.Left,
                                 rect.Bottom - rect.Top);
                    }
                }
            }

在 Properties/Build 上设置 'Allow unsafe code'...