DrawRectangle中的边框 [英] Border in DrawRectangle

查看:288
本文介绍了DrawRectangle中的边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我为自己的控件编写了OnPaint事件,并且它对于像素精度非常必要。



我有一个矩形边框的小问题。



请参阅图片:

已移除ImageShack链接



这两个矩形是用相同的位置和大小参数绘制的,但使用不同的笔大小。看看发生了什么?当边框变大时,它已经吃掉矩形前面的空闲空间(左边)。

不知道是否有某种属性可以在矩形内部绘制边框,以便与矩形的距离始终相同。感谢。

解决方案

您可以通过指定 PenAlignment

  Pen pen = new Pen(Color .Black,2); 
pen.Alignment = PenAlignment.Inset; //< - this
g.DrawRectangle(pen,rect);


Well, I'm coding the OnPaint event for my own control and it is very nescessary for me to make it pixel-accurate.

I've got a little problem with borders of rectangles.

See picture:

removed dead ImageShack link

These two rectangles were drawn with the same location and size parameters, but using different size of the pen. See what happend? When border became larger it has eaten the free space before the rectangle (on the left).

I wonder if there is some kind of property which makes border be drawn inside of the rectangle, so that the distance to rectangle will always be the same. Thanks.

解决方案

You can do this by specifying PenAlignment

Pen pen = new Pen(Color.Black, 2);
pen.Alignment = PenAlignment.Inset; //<-- this
g.DrawRectangle(pen, rect);

这篇关于DrawRectangle中的边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆