【发布时间】:2016-12-22 13:31:57
【问题描述】:
我是新来的,我正在尝试找到一种在按钮中创建自己的自定义形状的方法。
我应该为它创建一个类吗?还是一个xml文件?我需要创建一个看起来像表格的按钮。我找到了这段代码,但很难创建它。
Button dynamicButton = new Button();
// 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.
dynamicButton.Region = polygon_region;
// Make the button big enough to hold the whole region.
dynamicButton.SetBounds(
dynamicButton.Location.X,
dynamicButton.Location.Y,
pts[3].X + 5, pts[4].Y + 5);
Controls.Add(dynamicButton);
【问题讨论】:
-
这个问题的答案很大程度上取决于您使用的 GUI 工具包(例如 Windows 窗体、WPF、Gtk#...),因此请添加适当的标签。
-
我不知道 GUI 工具包。这是一个可以切割和绘制一个完整按钮的外部软件吗?有没有办法在VS Blend中创建一个按钮并将其导入winform?
-
"我不知道 GUI 工具包。" - 如果您在窗口中添加视觉元素(例如按钮),则您使用的是 GUI 工具包。 必须在对它进行任何操作之前找出你正在使用的那个。 “这是一个外部软件” - 不。 “GUI 工具包”是可用于创建图形用户界面(即 GUI)的任何可视化组件集的通用术语。 “在 VS Blend 中创建一个按钮并将其导入 winform” - VS Blend 以 WPF 为目标,这是与 Windows 窗体不同的 GUI 工具包。他们可以互动,但坦率地说,你应该先学习其他东西。
-
当你创建项目时,你通常会坚持它的类型:winforms、wpf、uwp...你有哪一种?
Controls.Add更可能意味着winforms。 -
我猜你是从 winforms 开始的,因为它更容易,但在 WPF 中解决此类问题要容易得多(任何
Control都有可以更改的模板)。也许现在改变还为时不晚?如果你坚持,谷歌“自定义按钮winforms”,例如here是一些链接的答案。至于你的问题,目前还不清楚,提供的代码与它无关。您能否向我们展示此类按钮的草图或更好地描述您正在尝试做什么?