2013-10-22 86 views
0

使用下面的代碼我可以繪製箭頭形狀的按鈕(如下所示),但是我想繪製六邊形(以下顯示爲結果圖像),以便可以使用png圖像大小175x154作爲按鈕圖像,我需要使用什麼點來繪製?我需要畫6個這樣的按鈕,我如何實現這一點?用於繪製六角形按鈕的要點

enter image description here

private void Parent_Load(object sender, EventArgs e) 
{ 
    // Define the points in the polygonal path. 
    Point[] pts = { 
     new Point(20, 60), 
     new Point(140, 60), 
     new Point(140, 20), 
     new Point(220, 100), 
     new Point(140, 180), 
     new Point(140, 140), 
     new Point(20, 140) 
    }; 

    // Make the GraphicsPath. 
    GraphicsPath polygon_path = new GraphicsPath(FillMode.Winding); 
    polygon_path.AddPolygon(pts); 

    // Convert the GraphicsPath into a Region. 
    Region polygon_region = new Region(polygon_path); 

    // Constrain the button to the region. 
    btnExam.Region = polygon_region; 

    // Make the button big enough to hold the whole region. 
    btnExam.SetBounds(
     btnExam.Location.X, 
     btnExam.Location.Y, 
     pts[3].X + 5, pts[4].Y + 5); 
} 

回答

3

輸入應該是一個Rectangle其中包含Hexagonal shape,從輸入,我們將計算PointsHexagonal shape,這樣的事情:

public Point[] GetPoints(Rectangle container){ 
    Point[] points = new Point[6]; 
    int half = container.Height/2; 
    int quart = container.Width/4; 
    points[0] = new Point(container.Left + quart, container.Top); 
    points[1] = new Point(container.Right - quart, container.Top); 
    points[2] = new Point(container.Right, container.Top + half); 
    points[3] = new Point(container.Right - quart, container.Bottom); 
    points[4] = new Point(container.Left + quart, container.Bottom); 
    points[5] = new Point(container.Left, container.Top + half); 
    return points; 
} 
private void Parent_Load(object sender, EventArgs e) { 
    //This should be placed first 
    // Make the button big enough to hold the whole region. 
    btnExam.SetBounds(btnExam.Location.X, btnExam.Location.Y, 100, 100); 

    // Make the GraphicsPath. 
    GraphicsPath polygon_path = new GraphicsPath(FillMode.Winding); 
    polygon_path.AddPolygon(GetPoints(btnExam.ClientRectangle)); 

    // Convert the GraphicsPath into a Region. 
    Region polygon_region = new Region(polygon_path); 

    // Constrain the button to the region. 
    btnExam.Region = polygon_region; 
} 

你應該更新當您的btnExam的尺寸發生變化時,該區域因此您應該定義一些稱爲「 UpdateRegion並調用它在SizeChanged事件處理程序:

private void UpdateRegion(){ 
    GraphicsPath polygon_path = new GraphicsPath(FillMode.Winding); 
    polygon_path.AddPolygon(GetPoints(btnExam.ClientRectangle)); 
    btnExam.Region = new Region(polygon_path); 
} 
//SizeChanged event handler for your btnExam 
private void btnExam_SizeChanged(object sender, EventArgs e){ 
    UpdateRegion(); 
} 
//Then you just need to change the size of your btnExam in Parent_Load 
private void Parent_Load(object sender, EventArgs e) { 
    //The button should be square 
    btnExam.SetBounds(btnExam.Location.X, btnExam.Location.Y, 100, 100); 
} 
+0

@Durga看到我的更新。請注意,我的代碼是用於「方形按鈕」的,如果您想要任何矩形的Button,請告訴我,我將在'GetPoints'方法中稍微修改一下代碼。 –

+0

通過使用您的解決方案我得到上面的圖像,但上面兩個滑動年齡不正確,這些可以糾正和背景空白,我沒有得到,爲什麼btnexam大小會改變,你的意思是如果我需要改變大小正確? – Durga

+0

@Durga不清楚你的意思是「兩個滑動年齡......」,因爲你的按鈕的大小變化,當然向「SizeChanged」事件處理程序添加代碼更好,因爲如果你想改變你的尺寸按鈕,'Region'會相應地爲你更新。在某些情況下,更改大小可能會**意外**,並且在這種情況下您的代碼不會被破壞。 –

0

這是什麼意思?

var xDisp = 10; 

var yDisp = 10; 

var length = 10; 

var ls32 = (int)(length * Math.Sqrt(3)/2.0); 

var half = (int)(length/2.0); 

var points = new[] 
{ 
    new Point(xDisp + length, yDisp), 
    new Point(xDisp + half,  yDisp + ls32), 
    new Point(xDisp - half,  yDisp + ls32), 
    new Point(xDisp - length, yDisp), 
    new Point(xDisp - half,  yDisp - ls32), 
    new Point(xDisp + half,  yDisp - ls32) 
};