我有一个应用程序需要将窗口移动到屏幕上的特定位置。我有以下代码来完成此操作。将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没有任何一种“主窗口”我怎么能去检测这些窗口,所以我可以移动它们?
关闭所有资源管理器窗口是否可以接受?像“Taskkill/IM explorer.exe/F”? – FreeAsInBeer
不幸的是,这是一个在计算机实验室中执行更新/安装软件的应用程序。用户坐在一台计算机上,他们所做的每件事都在实验室中的每台计算机上进行镜像,因此让所有内容排列起来相当重要。 –
太糟糕了。这当然是一种做事的方式,但看起来脚本会更容易一些。你正在更新哪些应用程序?它们是否可以通过交换机从命令行运行? – FreeAsInBeer