2014-04-10 116 views
0

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

//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没有任何一种“主窗口”我怎么能去检测这些窗口,所以我可以移动它们?

+0

关闭所有资源管理器窗口是否可以接受?像“Taskkill/IM explorer.exe/F”? – FreeAsInBeer

+0

不幸的是,这是一个在计算机实验室中执行更新/安装软件的应用程序。用户坐在一台计算机上,他们所做的每件事都在实验室中的每台计算机上进行镜像,因此让所有内容排列起来相当重要。 –

+0

太糟糕了。这当然是一种做事的方式,但看起来脚本会更容易一些。你正在更新哪些应用程序?它们是否可以通过交换机从命令行运行? – FreeAsInBeer

回答

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; 
     } 
    } 
} 
+0

我刚插入一个测试应用程序,它似乎工作。谢谢! –

+0

不用担心 - 不客气。注意小编辑删除未使用的变量。 – Geoff

0

肯定迟到了比赛,但我觉得这个项目可能是兴趣,因为这解决了OP的问题,如何设置的Windows资源管理器窗口的位置。该项目是可配置的,并且能够处理其他应用程序类型。这看起来很有意义,因为OP似乎对移动所有窗口感兴趣,而不仅仅是Windows资源管理器窗口。

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

相关问题