C# 绘制六角形按钮需要使用哪些点
使用下面的代码,我能够绘制箭头形状的按钮(如下所示),但我想绘制hexagone(如下所示为结果图像),这样我就可以使用175x154大小的png图像作为按钮图像,我需要使用哪些点来绘制此图?我需要画6个这样的按钮,我该如何实现C# 绘制六角形按钮需要使用哪些点,c#,winforms,C#,Winforms,使用下面的代码,我能够绘制箭头形状的按钮(如下所示),但我想绘制hexagone(如下所示为结果图像),这样我就可以使用175x154大小的png图像作为按钮图像,我需要使用哪些点来绘制此图?我需要画6个这样的按钮,我该如何实现 private void Parent_Load(object sender, EventArgs e) { // Define the points in the polygonal path. Point[] pts = { new
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);
}
输入应该是一个
矩形
,其中包含六边形
,根据该输入,我们将为您的六边形
计算点
,如下所示:
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);
}
输入应该是一个
矩形
,其中包含六边形
,根据该输入,我们将为您的六边形
计算点
,如下所示:
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);
}
输入应该是一个
矩形
,其中包含六边形
,根据该输入,我们将为您的六边形
计算点
,如下所示:
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);
}
输入应该是一个
矩形
,其中包含六边形
,根据该输入,我们将为您的六边形
计算点
,如下所示:
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);
}
这就是你的意思吗
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)
};
这就是你的意思吗
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)
};
这就是你的意思吗
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)
};
这就是你的意思吗
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)
};
@Durga查看我的更新。请注意,我的代码是一个
方形按钮
,如果您想要一个任意矩形的按钮,请告诉我,我将在获取点
方法中稍微修改代码。通过使用您的解决方案,我得到了上面的图像,但是上面两个滑动年龄没有正确出现,这些可以被纠正吗,背景空白,我不明白,为什么btnexam的大小会改变,你的意思是我是否需要正确地改变大小?@Durga不清楚你所说的两个滑动年龄是什么意思…,对于按钮的大小改变,当然向SizeChanged
事件处理程序添加代码更好,因为如果你想改变按钮的大小,区域将相应地为您更新。在某些情况下,更改大小可能会意外完成,在这种情况下,您的代码不会被破坏。好的,我现在得到了它,但正如您所说的//按钮应该是方形的
我使用的是这个175 x 154
大小与我的图像大小一致,是吗fine@Durga在GetPoints
中稍加修改即可,将half
更新为container.Height/2
@Durga查看我的更新。请注意,我的代码是一个方形按钮
,如果您想要一个任意矩形的按钮,请告诉我,我将在获取点
方法中稍微修改代码。通过使用您的解决方案,我得到了上面的图像,但是上面两个滑动年龄没有正确出现,这些可以被纠正吗,背景空白,我不明白,为什么btnexam的大小会改变,你的意思是我是否需要正确地改变大小?@Durga不清楚你所说的两个滑动年龄是什么意思…
,对于按钮的大小改变,当然向SizeChanged
事件处理程序添加代码更好,因为如果你想改变按钮的大小,区域将相应地为您更新。在某些情况下,更改大小可能会意外完成,在这种情况下,您的代码不会被破坏。好的,我现在得到了它,但正如您所说的//按钮应该是方形的
我使用的是这个175 x 154
大小与我的图像大小一致,是吗fine@Durga在GetPoints
中稍加修改即可,将half
更新为container.Height/2
@Durga查看我的更新。请注意,我的代码是一个方形按钮
,如果您想要一个任意矩形的按钮,请告诉我,我将在获取点
方法中稍微修改代码。通过使用您的解决方案,我得到了上面的图像,但是上面两个滑动年龄没有正确出现,这些可以被纠正吗,背景空白,我不明白,为什么btnexam的大小会改变,你的意思是我是否需要正确地改变大小?@Durga不清楚你所说的两个滑动年龄是什么意思…
,对于按钮的大小改变,当然向SizeChanged
事件处理程序添加代码更好,因为如果你想改变按钮的大小,区域将相应地为您更新。在某些情况下,更改大小可能会意外完成,在这种情况下,您的代码不会被破坏。好的,我现在得到了它,但正如您所说的//按钮应该是方形的
我使用的是这个175 x 154
大小与我的图像大小一致,是吗fine@Durga在GetPoints
中稍加修改即可,将half
更新为container.Height/2
@Durga查看我的更新。请注意,我的代码是用于方形按钮的
,如果您想要任何矩形的按钮,请让我知道,我将在获取点
方法中稍微修改代码。使用您的解决方案,我将获得上面的图像,但上面两个滑动年龄不是