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 を更新する必要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); 
}
于 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 に答える