gpt4 book ai didi

C#:在非主监视器上使用 DrawRectangle 绘制的矩形不呈现顶部和左侧边框

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:06 25 4
gpt4 key购买 nike

我有一个全屏表单,在 Paint 事件的处理程序中,我在整个表单周围绘制了一个 2px 的边框。我为连接到计算机的每个屏幕创建了这些表格之一。出于某种原因,顶部和左侧边框未绘制在任何非主显示器上。窗体的背景覆盖了整个屏幕,但我看不到要在距屏幕顶部向下约 3px 和距屏幕左侧约 3px 的区域上绘制(使用 GDI)。

我的 Paint 事件处理程序代码如下。

private void OnPaint(object sender, PaintEventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
int border = 2;
int startPos = 0;
// offset used to correctly paint all the way to the right and bottom edges
int offset = 1;
Rectangle rect = new Rectangle(startPos, startPos, this.Width - border + offset, this.Height - border + offset);
Pen pen = new Pen(Color.Red, border);

// draw a border
g.DrawRectangle(pen, rect);
}
}

有人见过这个吗?

最佳答案

您的代码工作正确。您应该知道在使用 this.Widththis.Height 时,这些值是根据表单周围的框架计算的。对于高度,您的表单控件的高度添加到计算的高度。您可以使用此代码:

using (Graphics g = this.CreateGraphics())
{
int border = 2;
int startPos = 0;
// offset used to correctly paint all the way to the right and bottom edges
int offset = 1;
Rectangle rect = new Rectangle(startPos, startPos, this.Width-20, this.Height-40);
Pen pen = new Pen(Color.Red, border);

// draw a border
g.DrawRectangle(pen, rect);
}

更新:

如果你想计算精确的尺寸,你可以使用这个代码:

 int width,height;
public Form1()
{
InitializeComponent();
PictureBox pc = new PictureBox();
pc.Dock = DockStyle.Fill;
this.Controls.Add(pc);
pc.Visible = false;
width = pc.Width;
height = pc.Height;
pc.Dispose();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{


using (Graphics g = this.CreateGraphics())
{
int border = 2;
int startPos = 0;
// offset used to correctly paint all the way to the right and bottom edges
int offset = 1;
Rectangle rect = new Rectangle(startPos, startPos, width,height);
Pen pen = new Pen(Color.Red, border);

// draw a border
g.DrawRectangle(pen, rect);
}

}

关于C#:在非主监视器上使用 DrawRectangle 绘制的矩形不呈现顶部和左侧边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18967021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com