【问题标题】:Moving windows explorer windows to a set location将 Windows 资源管理器窗口移动到设定位置
【发布时间】:2014-04-10 13:42:13
【问题描述】:

我有一个应用程序需要将窗口移动到屏幕上的特定位置。我有以下代码来完成此操作。

 //get an array of open processes
        Process[] processes = Process.GetProcesses();

        //clear the list of open window handles
        WindowHandles.Clear();

        //loop through each process
        foreach (Process process in processes)
        {
            //check if the process has a main window associated with it
            if (!String.IsNullOrEmpty(process.MainWindowTitle))
            {
                //add this process' handle to the open window handles list
                WindowHandles.Add(process.MainWindowHandle);
            }
        }

        //move windows
        AutoMoveWindows();

这里是实际移动窗口的方法。

  private void AutoMoveWindows()
    {
        foreach (IntPtr handle in WindowHandles)
        {
            //check if the handle has already been moved
            if(!MovedHandles.Contains(handle))
            {
                //move the window to the top left of the screen, set its size to 800 x 600
                MoveWindow(handle, 0, 0, 800, 600, true);

                //add the handle to the moved handles list
                MovedHandles.Add(handle);
            }
        }
    }

    [DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

这适用于所有窗口,除了 explorer.exe 的一部分,例如文件浏览器或文件夹属性。由于 explorer.exe 似乎没有任何类型的“主窗口”,我该如何检测这些窗口,以便我可以移动它们?

【问题讨论】:

  • 是否可以只关闭所有资源管理器窗口? Taskkill /IM explorer.exe /F 之类的东西?
  • 遗憾的是,这是一个用于在计算机实验室执行更新/安装软件的应用程序。用户坐在一台电脑前,他们所做的一切都反映在实验室的每台电脑上,因此将所有内容排成一行非常重要。
  • 那太糟糕了。这当然是一种做事方式,但似乎编写脚本会容易得多。您要更新哪些应用程序?它们可以从带有开关的命令行运行吗?
  • 我们确实有很多脚本化/自动化的东西,但是有一些程序,例如特定的测试服务(这是一个学校环境),必须经常手动更新。跨度>

标签: c# windows explorer


【解决方案1】:

您可以使用 ShellWindows 获取 shell 拥有的窗口列表,然后移动每个窗口;这将是与您上面所得到的不同的流程,但它应该可以工作。请注意,您需要添加对 shell32.dllshdocvw.dll 的引用(在 Windows 7 中,两者都在 c:\windows\system32 中)。

private void MoveAllExplorerWindows(object sender, EventArgs e)
{
    string filename;

    foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
    {
        filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
        if (filename.ToLowerInvariant() == "explorer")
        {
            window.Left = 0;
            window.Top = 0;
            window.Width = 800;
            window.Height = 600;
        }
    }
}

【讨论】:

  • 我刚刚将它插入了一个测试应用程序,它似乎正在工作。谢谢!
  • 不用担心 - 不客气。请注意删除未使用变量的小编辑。
【解决方案2】:

游戏当然晚了,但我认为这个项目可能会引起人们的兴趣,因为它解决了 OP 的问题,即如何设置 Windows 资源管理器窗口的位置。该项目是可配置的,并且能够处理其他应用程序类型。这似乎是相关的,因为 OP 似乎对移动所有窗口感兴趣,而不仅仅是 Windows 资源管理器窗口。

http://www.codeproject.com/Tips/1057230/Windows-Resize-and-Move

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2019-08-03
    • 2017-01-28
    • 1970-01-01
    相关资源
    最近更新 更多