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