一般程序启动时会默认显示在主显示屏,当多个程序启动运行后,会有相互遮挡的情况;
当有两个显示屏的时候,可以使用程序将其中的一些程序移动至副显示屏中显示;
移动程序设置开机自启,即可实现开机移动窗口。
1.找出当前桌面显示的所有窗口
2.匹配窗口的标题或者其他属性
3.获取显示屏的属性,区分主显示屏和副显示屏
4.计算副显示屏的坐标
5.移动窗口,并设置显示大小
Console.WriteLine("开始查找窗口");
var windows = WindowEnumerator.FindAll();
Console.WriteLine("找到窗口:"+windows.Count);
for (int i = 0; i < windows.Count; i++)
{
var window = windows[i];
Console.WriteLine($@"{i.ToString().PadLeft(3, ' ')}. {window.Title} {window.Bounds.X}, {window.Bounds.Y}, {window.Bounds.Width}, {window.Bounds.Height}");
if (window.IsVisible && window.Title == "运行界面")
{
Screen[] screens = Screen.AllScreens;//获取当前所有显示屏
Screen screenCurrent = Screen.PrimaryScreen;//获取主显示屏
Screen otherScreen = null;
//int count = screens.Count();
foreach (var item in screens)
{
if (item.DeviceName != screenCurrent.DeviceName)
{
//获取到副显示屏,因为机器只有两个显示屏,所以此处这么处理,如果是多显示屏的话,可能要对属性值进行判断
otherScreen = item;
}
}
if (otherScreen != null)
{
//计算副屏幕零点
int X0 = otherScreen.Bounds.X - screenCurrent.Bounds.X;
int Y0 = otherScreen.Bounds.Y - screenCurrent.Bounds.Y;
//算出中心位置的偏移量
int w = (otherScreen.Bounds.Width - window.Bounds.Width) / 2;
int h = (otherScreen.Bounds.Height - window.Bounds.Height) / 2;
//得出实际的坐标点
int x = X0 + w;
int y = Y0 + h;
//移动窗口
MoveWindow(window.Hwnd, x, y, window.Bounds.Width, window.Bounds.Height, true);
return;
}
}
//Console.WriteLine($@"{i.ToString().PadLeft(3, ' ')}. {window.Title} {window.Bounds.X}, {window.Bounds.Y}, {window.Bounds.Width}, {window.Bounds.Height}");
}
项目代码下载:
https://download.csdn.net/download/rotion135/88278065
开机自启的功能:
https://download.csdn.net/download/rotion135/88278073