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



我有一个应用程序,它要求将窗口移动到屏幕上的特定位置。我有以下代码来实现这一点。

 //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似乎没有任何类型的"主窗口",我该如何检测这些窗口,以便移动它们?

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

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

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

最新更新