0

使用下面的代码我可以绘制箭头形状的按钮(如下所示),但我想绘制六边形(如下所示的结果图像),这样我就可以使用尺寸为 175x154 的 png 图像作为按钮图像,我需要使用什么点画这个?我需要画6个这样的按钮,我该如何实现?

在此处输入图像描述

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);
}
4

2 回答 2

3

输入应该是Rectangle包含 的Hexagonal shape,我们将根据该输入Points为您计算Hexagonal 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;
}

您应该在您的 Size 更改时更新 Region ,因此您应该定义一些调用的方法并在事件处理程序中调用它:btnExamUpdateRegionSizeChanged

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); 
}
于 2013-10-22T09:32:58.377 回答
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)
};
于 2013-10-22T09:58:29.220 回答