嗯,我正在为我自己的控件编写OnPaint事件,这对我来说是非常必要的,使其像素精确。
矩形的边框有点问题。
见图:
删除死ImageShack链路
这两个矩形是用相同的位置和大小参数绘制的,但使用的是笔的不同大小。看看发生了什么?当边框变大时,它已经占用了矩形(左边)之前的空闲空间。
我想知道是否有某种属性使得边框被绘制在矩形内,这样到矩形的距离就总是一样的。谢谢。
发布于 2010-04-27 10:51:59
您可以通过指定PenAlignment来做到这一点。
Pen pen = new Pen(Color.Black, 2);
pen.Alignment = PenAlignment.Inset; //<-- this
g.DrawRectangle(pen, rect);发布于 2009-05-29 11:39:16
如果希望矩形的外部边界在所有方向上都受到约束,则需要根据笔的宽度重新计算它:
private void DrawRectangle(Graphics g, Rectangle rect, float penWidth)
{
using (Pen pen = new Pen(SystemColors.ControlDark, penWidth))
{
float shrinkAmount = pen.Width / 2;
g.DrawRectangle(
pen,
rect.X + shrinkAmount, // move half a pen-width to the right
rect.Y + shrinkAmount, // move half a pen-width to the down
rect.Width - penWidth, // shrink width with one pen-width
rect.Height - penWidth); // shrink height with one pen-width
}
}发布于 2009-05-29 11:26:13
这并不是对问题的直接回答,但您可能需要考虑使用ControlPaint.DrawBorder方法。您可以指定边框样式、颜色和其他各种属性。我也相信它可以帮你调整页边距。
https://stackoverflow.com/questions/925509
复制相似问题