【问题标题】:Properly Sizing Custom-Drawn Controls正确调整自定义绘制控件的大小
【发布时间】:2012-12-13 16:25:03
【问题描述】:

我正在尝试制作一个自定义控件,该控件可以正确地绘制自身以填充其当前大小。我假设我应该使用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);

产生这个:

我应该使用什么来获取控件的可绘制区域?

【问题讨论】:

标签: c# winforms visual-studio gdi+


【解决方案1】:

您可以使用:

ControlPaint.DrawBorder(g, this.ClientRectangle, _
                        Color.Red, ButtonBorderStyle.Solid);

Graphics g = e.Graphics;.

或者像你一样绘制它,但从宽度和高度中减去 1(1 因为宽度和高度包含在内,但绘制矩形需要不包括最后一个像素的大小 - 在内部它计算 x + w/y + h 然后最终在next 像素在最后一个像素之后,因此我们需要减去一个以获得 last 像素的位置)。

rectangle r = this.ClientRectangle;
r.Width -= 1;
r.Height -= 1;

g.DrawRectangle(System.Drawing.Pens.Red, r);

当然这来自OnPaint 事件处理程序。

【讨论】:

  • 看来你需要从底部和右侧减去2,而不是1。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
相关资源
最近更新 更多