[英]C#: Rectangles drawn with DrawRectangle on a non-primary monitor doesn't render top and left borders
我有一個全屏的窗體,並且在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.Width
或this.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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.