【发布时间】:2017-07-07 04:54:22
【问题描述】:
首先,就上下文而言,我是 C# 的初学者,我正在玩表单。
我试图在表单(“Form1”)上的面板(“myPanel”)上绘制一个矩形,但是有一个边距或某种我无法删除的填充。
我已将“myPanel”的“padding”和“margin”属性设置为 0,但没有成功。
代码是:
namespace Forms___Playing_with_Graphics
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myPanel_Paint(object sender, PaintEventArgs e)
{
// rectangle, slightly smaller than size of panel
int topLeftx = myPanel.Location.X;
int topLefty = myPanel.Location.Y;
int width = myPanel.Size.Width - 5;
int height = myPanel.Size.Height - 5;
Graphics g = e.Graphics;
Rectangle r = new Rectangle(topLeftx, topLefty, width, height);
Pen p = new Pen(Color.Black, 5F);
g.DrawRectangle(p, r);
}
}
}
结果截图:
如何删除矩形与内部左边缘和上边缘之间的填充?我天真的期望矩形从左上角开始。
任何帮助将不胜感激。
【问题讨论】:
-
面板内部有自己的坐标,所以topLeftx = 0, topLefty = 0。
-
不要使用
myPanel.Location,图纸应该在myPanel.ClientRectangle里面