在多显示器上启动应用程序






4.62/5 (10投票s)
在多显示器上启动应用程序窗体
引言
这已经是一个持续讨论的话题,我一直收到关于如何在不同的屏幕上启动 Windows 或在我们的例子中应用程序的问题。在我目前工作的梦想之城,我们有许多不同类型的适配器用于许多视听系统,并且像幸运抽奖、老虎机结果等应用程序必须自动启动到正确的视听设备上,以实现动画效果、其他屏幕上的随机数字等。
启动器

已启动

所有屏幕
获取系统当前托管的所有屏幕的信息,只需要这样做:System.Windows.Forms.Screen
。
因此,要填充我们的下拉菜单,循环遍历
Screen[] scr = Screen.AllScreens
this.cmbDevices.Items.Clear();
foreach (System.Windows.Forms.Screen s in scr)
{
strDeviceName = s.DeviceName.Replace("\\\\.\\", "");
this.cmbDevices.Items.Add(strDeviceName);
// you can check the device is primary or not this way
if (s.Primary) this.cmbDevices.Items.Add(">" + strDeviceName);
else this.cmbDevices.Items.Add(strDeviceName);
}
this.cmbDevices.SelectedIndex = 0;
启动
为了确保正确启动到正确的显示器,只需要正确设置两个属性:Form.StartPosition
& Form.Location
。
Screen[] scr = Screen.AllScreens
Form oForm = new Form()
oForm.Left = scr[0].Bounds.Width;
oForm.Top = scr[0].Bounds.Height;
oForm.StartPosition = FormStartPosition.Manual;
oForm.Location = scr[0].Bounds.Location;
oForm.Show();
整合
好了,这就是将您的窗体启动到用户选择的显示器所需要的一切。
public partial class frmMain : Form
{
Form[] aryForms = new Form[5]; // Allow 5 total forms.
int currentPointer = 0; //
Screen[] scr = Screen.AllScreens;
public frmMain()
{
InitializeComponent();
LoadScreen();
}
private void LoadScreen()
{
String strDeviceName = String.Empty;
this.cmbDevices.Items.Clear();
foreach (Screen s in scr)
{
strDeviceName = s.DeviceName.Replace("\\\\.\\", "");
this.cmbDevices.Items.Add(strDeviceName);
/* Enable this section, if you want to point to
user this is the default screen.
if (s.Primary) this.cmbDevices.Items.Add(">" + strDeviceName);
else this.cmbDevices.Items.Add(strDeviceName);
*/
}
this.cmbDevices.SelectedIndex = 0;
}
private void btnLaunchIn_Click(object sender, EventArgs e)
{
int intLaunchScreen = getScreenNumber(this.cmbDevices.SelectedItem.ToString());
if (currentPointer <= 4)
{
aryForms[currentPointer] = new frmLaunchedWindow();
aryForms[currentPointer].Text =
aryForms[currentPointer].Text + currentPointer;
aryForms[currentPointer].Left = scr[intLaunchScreen].Bounds.Width;
aryForms[currentPointer].Top = scr[intLaunchScreen].Bounds.Height;
aryForms[currentPointer].StartPosition = FormStartPosition.Manual;
aryForms[currentPointer].Location = scr[intLaunchScreen].Bounds.Location;
//Point p = new Point(scr[0].Bounds.Location.X, scr[0].Bounds.Location.Y);
//aryForms[currentPointer].Location = p;
aryForms[currentPointer].Show();
currentPointer += 1;
}
}
private int getScreenNumber(String DeviceID)
{
int i = 0;
foreach (Screen s in scr)
{
if (s.DeviceName == "\\\\.\\"+DeviceID) return i;
i += 1;
}
// if cannot find the device reset to the default 0
return 0;
}
}
历史
- 2010年9月13日:初始发布