2012-12-13 40 views
0

我正在嘗試製作一個自定義控件,以正確繪製自己以填充其當前大小。我假設我應該使用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); 

抽獎事件處理債收益率這樣的:

ClientRecangle is clipped

我應該使用得到控制的繪製區域?

+1

可能的重複:http://stackoverflow.com/a/8377709/577417 –

+0

可能重複的[FillRectangle和DrawRectangle的像素行爲](http://stackoverflow.com/questions/3147569/pixel-behaviour-of- fillrectangle-and-drawrectangle) –

+0

@Benjamin可能重複的答案似乎是最正確的。看來你需要從底部和右側減去2,而不是1。 –

回答

2

您可以使用:

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事件處理程序中當然這的。

+0

看來,你需要從底部和右側減去2,而不是1。謝謝! –

相關問題