2011-02-23 80 views
12

C#形式我使用这个代码,以使我的表格(FormBorderStyle =无)带圆边:与自定义边框和圆滑的边缘

[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] 
private static extern IntPtr CreateRoundRectRgn 
(
    int nLeftRect, // x-coordinate of upper-left corner 
    int nTopRect, // y-coordinate of upper-left corner 
    int nRightRect, // x-coordinate of lower-right corner 
    int nBottomRect, // y-coordinate of lower-right corner 
    int nWidthEllipse, // height of ellipse 
    int nHeightEllipse // width of ellipse 
); 

public Form1() 
{ 
    InitializeComponent(); 
    Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20)); 
} 

而且这个设置自定义边框上的Paint事件:

ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid, Color.Black, 5, ButtonBorderStyle.Solid); 

但是看到这个screenshot

内部表单矩形不具有圆角边缘。

我怎样才能使蓝色的内部表单矩形也有圆润的边缘,所以它不会看起来像截图?

回答

9

该地区propery只是切断了角落。要有一个真正的圆角,你将不得不绘制圆角的矩形。

Drawing rounded rectangles

可能更容易画出你想要的形状的图像,并把该透明的形式。易于绘制,但不能调整大小。

0

请注意,您正在泄漏由CreateRoundRectRgn()返回的手柄,您应该在使用后将其释放DeleteObject()

Region.FromHrgn()复制定义,所以它不会释放句柄。

[DllImport("Gdi32.dll", EntryPoint = "DeleteObject")] 
public static extern bool DeleteObject(IntPtr hObject); 

public Form1() 
{ 
    InitializeComponent(); 
    IntPtr handle = CreateRoundRectRgn(0, 0, Width, Height, 20, 20); 
    if (handle == IntPtr.Zero) 
     ; // error with CreateRoundRectRgn 
    Region = System.Drawing.Region.FromHrgn(handle); 
    DeleteObject(handle); 
} 

(会增加为评论,但口碑DED)