将Windows资源管理器窗口移动到设置位置

时间:2014-04-10 13:42:14

标签: c# windows explorer

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

 //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没有任何类型的"主窗口"我怎么能去检测这些窗户,所以我可以移动它们?

2 个答案:

答案 0 :(得分:2)

您可以使用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;
        }
    }
}

答案 1 :(得分:0)

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

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