2017-08-22 56 views
1

我想在C#Windows窗体中制作一个Windows覆盖图,但它不会填充我的设置上的第二个屏幕。我使用的命令找出所有的屏幕分辨率:C#窗体不会填满整个屏幕

表1:

Rectangle a; 
    Rectangle b; 
    public Form1() 
    { 
     InitializeComponent(); 
     pictureBox1.Hide(); 
     pictureBox2.Hide(); 
     Positions(); 
     int i = 0; 
     foreach (Screen S in AllScreens) 
     { 
      if (i == 0) 
      { 
       a = S.Bounds; 
       i++; 
      } 
      else 
      { 
       b = S.Bounds; 
      } 
     } 
     FormBorderStyle = FormBorderStyle.None; 
     Work(); 
     IDK(b); 
    } 
    void Work() 
    { 
     Height = a.Height; 
     Width = a.Width; 
    } 
    void MsgBox(string a) 
    { 
     MessageBox.Show(a); 
    } 
    void IDK(Rectangle b) 
    { 
     int showOnMonitor = 1; 
     Screen[] sc; 
     sc = AllScreens; 
     Form2 f = new Form2(b) 
     { 
      FormBorderStyle = FormBorderStyle.None, 
      Left = sc[showOnMonitor].Bounds.Left, 
      Top = sc[showOnMonitor].Bounds.Top, 
      StartPosition = FormStartPosition.Manual 
     }; 
     f.Show(); 
    } 
    void Positions() 
    { 
     label1.Location = new Point(
     Width/2 - label1.Width/2, 
     Height/2 - label1.Height - 40); 
     label1.Anchor = AnchorStyles.None; 
     button1.Location = new Point(
     Width/2 - button1.Width/2, 
     Height/2 - button1.Height + 40); 
     button1.Anchor = AnchorStyles.None; 
     linkLabel1.Location = new Point(
     Width/2 - linkLabel1.Width/2, 
     Height/2 - linkLabel1.Height + 500); 
     linkLabel1.Anchor = AnchorStyles.None; 
    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     LoadGUI(); 
     MsgBox(Width.ToString()); 
     MsgBox(Height.ToString()); 
    } 
    void LoadGUI() 
    { 
     label1.Hide(); 
     button1.Hide(); 
     linkLabel1.Hide(); 
     pictureBox1.Show(); 
     pictureBox1.Location = new Point(20, 20); 
     pictureBox1.Anchor = AnchorStyles.None; 
     pictureBox2.Show(); 
     pictureBox2.Location = new Point(20, 188); 
     pictureBox2.Anchor = AnchorStyles.None; 
    } 
    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
    { 
     Application.Exit(); 
    } 

表2:

Rectangle a; 
    public Form2(Rectangle b) 
    { 
     InitializeComponent(); 
     a = b; 
     Work(); 
    } 
    void Work() 
    { 
     Height = a.Height; 
     Width = a.Width; 
    } 
    private void Form2_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     Application.Exit(); 
    } 

的问题是,表2不会充满整个第2屏幕。 我不知道为什么屏幕没有完全填满,代码工作了一天前,但它后Windows更新(我不知道这是否是问题。)。对于我所知道的这将永远不会工作...任何帮助将不胜感激。 -Alex

图片证明: Image (右下)

回答

2

我设法复制这一点。我检查屏幕WorkingArea相比界:

屏幕[0]

屏幕\ \ DISPLAY1;

界限:{X = 0,Y = 0,Width = 1920,Height = 1080};

工作区域:{X = 0,Y = 0,宽度= 1920,高度= 1040}

屏幕[1]

屏幕\ \ DISPLAY2;

界限:{X = -1920,Y = -74,Width = 1920,Height = 1080};

工作地区:{X = -1920,Y = -74,宽1920,高1080 =}

我想这可能是事做与Y-74,但没”解释宽度。

最终,在玩了一下之后,我设法让它按照你的意图显示,并且我简化了一下。

实际的修复是代替设置表单大小,我使用form.SetBounds(),通过它的屏幕边界。我包好了一个简单的函数,你传递给它的形式和屏幕:

public static class SetScreen 
{ 
    public static void setFormLocation(Form form, Screen screen) 
    { 
     Rectangle bounds = screen.Bounds; 
     form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); 
    } 
} 

然后我把它叫做是这样的:

public Form1() 
{ 
    InitializeComponent(); 
    pictureBox1.Hide(); 
    pictureBox2.Hide(); 
    Positions(); 
    this.StartPosition = FormStartPosition.Manual; 
    this.FormBorderStyle = FormBorderStyle.None; 
    SetScreen.setFormLocation(this, Screen.PrimaryScreen); 

    Form2 f = new Form2(); 
    f.StartPosition = FormStartPosition.Manual; 
    f.FormBorderStyle = FormBorderStyle.None; 
    SetScreen.setFormLocation(f, Screen.AllScreens[1]); 
    f.Show(); 
} 
+0

什么码的地方我用这种替换形式1 ? – user6481546

+0

和我的视觉工作室说SetScreen不存在 – user6481546

+0

我编辑了我的答案,以便您可以用我的替换您的Form1初始化代码。关注SetScreen,我创建了一个单独的静态类“setScreen”。编辑这一点 - 只要确保它在相同的名称空间。 – ainwood