2013-10-22 80 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) 
};