2011-02-23 79 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)