请问如何让 Form 显示在指定的 Screen 上面?

youzelin 2013-03-21 10:04:20
我现在的开发电脑有两个显示器,两个显示器的分辨率是一样的,一个主显示器,一个附加显示器。
Q:请问,我想让我程序 Run 起来之后,Form 显示在指定的显示器屏幕上,而这个屏幕也是 Visual Studio 所在屏幕,请问有什么好的方法?

谢谢。
...全文
339 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzelin 2013-03-22
  • 打赏
  • 举报
回复
引用 5 楼 gomoku 的回复:
你可以问“请问如何确定Visual Studio所在的显示器?” C# code?1234567891011private void button1_Clicked(object sender, EventArgs e){ var visualStudio = Process.GetProcessesByName("devenv").FirstOrDefault(……
灰常感谢。
Hauk 2013-03-21
  • 打赏
  • 举报
回复
this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2); this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2); 获取显示屏数量,计算location。
gomoku 2013-03-21
  • 打赏
  • 举报
回复
设置Form的Location,比如:
public Form1()
{
    this.StartPosition = FormStartPosition.Manual;
    this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width + 100, 0);
}
mingcsharp 2013-03-21
  • 打赏
  • 举报
回复
有点意思
gomoku 2013-03-21
  • 打赏
  • 举报
回复
你可以问“请问如何确定Visual Studio所在的显示器?”

private void button1_Clicked(object sender, EventArgs e)
{
    var visualStudio = Process.GetProcessesByName("devenv").FirstOrDefault();
    Rectangle rect = new Rectangle();
    if (visualStudio != null && GetWindowRect(visualStudio.MainWindowHandle, ref rect))
    {
        MessageBox.Show("visual studio @" + rect);
    }
}
[DllImport("User32")]
static extern bool GetWindowRect(IntPtr hWnd, ref Rectangle rect);
当然可能有多个Visual Studio在运行,也可能一个VS跨越好几个显示器。
youzelin 2013-03-21
  • 打赏
  • 举报
回复
引用 2 楼 haukwong 的回复:
this.Left = ((Screen.AllScreens[1].Bounds.Width - this.Width) / 2); this.Top = ((Screen.AllScreens[1].Bounds.Height - this.Height) / 2); 获取显示屏数量,计算location。
随意指定哪个 Screen 我知道怎么做,即便遍历 Screens 都行,但是哪个显示器是我应该要的(Visual Studio 所在的)我无法确定。
youzelin 2013-03-21
  • 打赏
  • 举报
回复
引用 1 楼 gomoku 的回复:
设置Form的Location,比如: C# code?12345public Form1(){ this.StartPosition = FormStartPosition.Manual; this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width + 100, 0);} ……
我的意思是:我需要显示在和 Visual Studio 同一个 Screen 当中! 这里的代码是指定放到哪个显示器去,但是问题是我怎么确定这个显示器就是我 Visual Studio 所在的显示器?

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧