C# 窗体不会填满整个屏幕



我正在尝试在C#Windows窗体中制作Windows覆盖,但它不会填满我的设置的第二个屏幕。我使用命令查找所有屏幕分辨率:

表格 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 更新后它坏了(我不知道这是否是问题所在。 据我所知,这永远不会奏效...任何帮助将不胜感激。-亚历克斯

图像校样: 图像 (右下(

我设法复制了这个。 我检查了屏幕工作区与边界的比较:

屏幕[0]

屏幕 \.\DISPLAY1;

边界: {x=0,y=0,宽度=1920,高度=1080};

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

屏幕[1]

屏幕 \.\DISPLAY2;

边界: {X=-1920,Y=-74,宽度=1920,高度=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();
}

最新更新