
[英]Custom-drawn control won't render controls behind it even though it's transparent
[英]Properly Sizing Custom-Drawn Controls
我正在尝试制作一个自定义控件,正确地绘制自己以填充其当前大小。 我假设我应该使用ClientRectangle属性进行大小调整,但客户端矩形的右侧和底部似乎被剪裁了。
使用填充绘制事件处理程序
Rectangle smaller = new Rectangle(5, 5, ClientRectangle.Width - 10, ClientRectangle.Height - 10);
e.Graphics.DrawRectangle(System.Drawing.Pens.Black, smaller);
e.Graphics.DrawRectangle(System.Drawing.Pens.Red, ClientRectangle);
得出这个:
我应该用什么来获得控件的可绘制区域?
你可以使用:
ControlPaint.DrawBorder(g, this.ClientRectangle, _
Color.Red, ButtonBorderStyle.Solid);
其中Graphics g = e.Graphics;
。
或者像你一样绘制但是从宽度和高度减去1(1因为宽度和高度是包含的,但是绘制矩形需要大小排除最后一个像素 - 在内部它计算x + w/y + h
然后最终位于在最后一个像素之后的下一个像素,因此我们需要减去一个以获得最后一个像素的位置)。
rectangle r = this.ClientRectangle;
r.Width -= 1;
r.Height -= 1;
g.DrawRectangle(System.Drawing.Pens.Red, r);
当然,这来自OnPaint
事件处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.