我正在尝试编写一个控制台应用程序,它将在多个监视器设置中找到所有应用程序窗口的尺寸和位置。
我能够对仅打开一个窗口的应用程序执行此操作,但是如果每个应用程序都打开多个窗口(例如chrome或Excel,并且打开了多个文件),则仅返回1条记录/窗口每个过程。
我可以获取所有子进程(我已经完成了,但不包括我的代码),但是每个进程都可以获取,因此对于Chrome,如果我打开了100个标签页,但只有4个窗口,则我得到了100个进程当我只希望每个窗口一个时返回。
我需要能够获取所有打开的窗口的WindowHandle,以便可以使用它来获取窗口的尺寸和位置。以下是我从打开的窗口中获取的值的示例(请参见下面的代码)。这只是显示这些值的我的代码的打印输出。
我的目标是运行该应用程序以获取所有打开的窗口及其位置/大小并保存。然后运行另一个应用程序,并将所有窗口移至其先前保存的位置和尺寸。
我正在处理将获取位置的代码,然后将窗口移动到保存的/硬编码的位置(我使用1个硬编码的应用程序进行了此测试),并且可以正常工作。我没有对该程序进行任何进一步的编程,因为我无法为每个进程具有多个窗口的应用程序获取所有窗口。
感谢您的帮助。
WindowPosistionChange.vshost - LEFT: 64 - RIGHT: 1057 - TOP: 64 - BOTTOM: 583 - WIDTH: 993 - HEIGHT: -519
devenv - LEFT: -7 - RIGHT: 1927 - TOP: -7 - BOTTOM: 1054 - WIDTH: 1934 - HEIGHT: -1061
notepad++ - LEFT: -25600 - RIGHT: -25472 - TOP: -25600 - BOTTOM: -25573 - WIDTH: 128 - HEIGHT: -27
Teams - LEFT: -32000 - RIGHT: -31840 - TOP: -32000 - BOTTOM: -31966 - WIDTH: 160 - HEIGHT: -34
devenv - LEFT: -7 - RIGHT: 1927 - TOP: -7 - BOTTOM: 1054 - WIDTH: 1934 - HEIGHT: -1061
SnippingTool - LEFT: -32000 - RIGHT: -31840 - TOP: -32000 - BOTTOM: -31966 - WIDTH: 160 - HEIGHT: -34
EXCEL - LEFT: -25600 - RIGHT: -25472 - TOP: -25600 - BOTTOM: -25573 - WIDTH: 128 - HEIGHT: -27
mstsc - LEFT: -32000 - RIGHT: -31840 - TOP: -32000 - BOTTOM: -31966 - WIDTH: 160 - HEIGHT: -34
chrome - LEFT: 1912 - RIGHT: 3848 - TOP: -12 - BOTTOM: 1054 - WIDTH: 1936 - HEIGHT: -1066
OUTLOOK - LEFT: 1913 - RIGHT: 3847 - TOP: -11 - BOTTOM: 1054 - WIDTH: 1934 - HEIGHT: -1065
这是我的应用代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing; // for POINT need to add reference from references
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics; // to get all the processes
// had to add reference for System.Windows.Forms in references in project
namespace WindowPosistionChange
{
class Program
{
static void Main(string[] args)
{
Process[] processes = Process.GetProcesses(".");
foreach (var process in processes)
{
if (String.IsNullOrEmpty(process.MainWindowTitle) == false)
{
IntPtr h1 = process.MainWindowHandle;
Screen scrn = Screen.FromHandle(h1);
WINDOWINFO winfo = new WINDOWINFO();
GetWindowInfo(h1, ref winfo);
string LineOutput = process.ProcessName + " " + process.MainWindowTitle + " - LEFT: " + winfo.rcWindow.Left.ToString() + "" +
" - RIGHT: " + winfo.rcWindow.Right.ToString() + "" +
" - TOP: " + winfo.rcWindow.Top.ToString() + "" +
" - BOTTOM: " + winfo.rcWindow.Bottom.ToString() + "" +
" - WIDTH: " + (winfo.rcWindow.Right - winfo.rcWindow.Left).ToString() + "" +
" - HEIGHT: " + (winfo.rcWindow.Top - winfo.rcWindow.Bottom).ToString() + "";
Console.WriteLine(LineOutput);
// I am able to set window possistion as well by using this where h1 is the process.MainWindowHandle
//SetWindowPos(h1, 0, -1920, 0, 1920, 1080, SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}// end if for title window null
}// end for each loop
}// end Main APP
//********************************************************************************************************************************
//********************************************************************************************************************************
//********************************************************************************************************************************
// for windows posision?
const short SWP_NOMOVE = 0X2;
const short SWP_NOSIZE = 1;
const short SWP_NOZORDER = 0X4;
const int SWP_SHOWWINDOW = 0x0040;
// WINDOW DLLs import and function generation
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
public extern static bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
public static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
public struct WINDOWINFO
{
public uint cbSize;
public RECT rcWindow; //holds the coords of the window
public RECT rcClient;
public uint dwStyle;
public uint dwExStyle;
public uint dwWindowStatus;
public uint cxWindowBorders;
public uint cyWindowBorders;
public ushort atomWindowType;
public ushort wCreatorVersion;
}
//this holds the coordinates
public struct RECT
{
public int Left; // Specifies the x-coordinate of the upper-left corner of the rectangle.
public int Top; // Specifies the y-coordinate of the upper-left corner of the rectangle.
public int Right; // Specifies the x-coordinate of the lower-right corner of the rectangle.
public int Bottom; // Specifies the y-coordinate of the lower-right corner of the rectangle.
}
}// end ProgramClass
}// end namespae