C#中的所有控件都有Region屬性,在設定Region屬性時,注意Region的參考系是以所設定的控件的左上角坐标,即 Custom.Location。
是以,在設定Region的GraphicsPath時,其左上角坐标始終為(0,0)
錯誤範例:
設定pB_headimg的Region為圓滑矩形。
private GraphicsPath GetRectPath(Rectangle rect,int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
// 左上角
path.AddArc(arcRect, 180, 90);
// 右上角
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
// 右下角
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
// 左下角
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();//閉合曲線
return path;
}
private void setPicbox()
{
GraphicsPath formPath = new GraphicsPath();
//錯誤使用控件的位置坐标,即使用了以視窗左上角為參考系
Rectangle rect = new Rectangle(pB_headimg.Location.X,pB_headimg.Location.Y,pB_headimg.Width, pB_headimg.Height);
formPath = GetRectPath(rect, 5);
pB_headimg.Region = new Region(formPath);
}
結果:
Region的範圍為( pB_headimg.Right ,pB_headimg.Bottom ,pB_headimg.Width ,pB_headimg.Height ) 超出了pB_headimg的範圍,是以pB_headimg消失了。