我是新来的,我正试图找到一种方法来创建我自己的自定义形状按钮。
我应该为它创建一个类吗?还是一个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);
答案 0 :(得分:0)
如果您正在尝试为C#WinForm应用程序创建一个按钮,那么这里是一个使用Panel控件的椭圆形按钮的示例。如果要提供自定义形状,请在OnPain事件中执行此操作。尝试使用以下代码,您将弄清楚该怎么做。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
public class AdonaiOvalButton : Panel
{
bool isControlActive = false;
#region Text
private string text = "Button";
[NotifyParentProperty(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
[Description("Sets the Text"), Category("Adonai")]
public override string Text
{
get { return text; }
set
{
if (value != text)
{
if (value == string.Empty)
{ value = " "; }
text = value;
this.Invalidate();
}
}
}
#endregion Text
#region ForeColor
private Color foreColor = Color.White;
[Description("Sets the Forecolor"), Category("Adonai")]
public override Color ForeColor
{
get { return foreColor; }
set
{
if (foreColor != value)
{
foreColor = value;
this.Invalidate();
}
}
}
#endregion ForeColor
#region Outline Color
private Color outLineColor = Color.DarkGray;
[Description("Sets the Buttons outline color"), Category("Adonai")]
public Color OutLineColor
{
get { return outLineColor; }
set
{
if (outLineColor != value)
{
outLineColor = value;
this.Invalidate();
}
}
}
#endregion Outline Color
#region Outline Width
private float outlineWidth = 0.4f;
[Description("Sets the Buttons outline width"), Category("Adonai")]
public float OutlineWidth
{
get { return outlineWidth; }
set
{
if (outlineWidth != value)
{
outlineWidth = value;
this.Invalidate();
}
}
}
#endregion Outline Width
#region Default Back Color
//--Default Button Color--//
private Color inactiveColor = ControlPaint.Dark(SystemColors.Grad
答案 1 :(得分:0)
我同意@Sinatr,WPF会更容易。如果您决定使用WPF,您可以执行以下操作:
<Button>
<Button.Template>
<ControlTemplate>
<Canvas Height="80" Width="100">
<Rectangle Height="80" Width="100" Stroke="Blue" StrokeThickness="2"/>
<Line X1="50" Y1="0" X2="50" Y2="80" Stroke="Blue" StrokeThickness="2"/>
<Line X1="0" Y1="20" X2="100" Y2="20" Stroke="Blue" StrokeThickness="2"/>
<Line X1="0" Y1="40" X2="100" Y2="40" Stroke="Blue" StrokeThickness="1"/>
<Line X1="0" Y1="60" X2="100" Y2="60" Stroke="Blue" StrokeThickness="1"/>
</Canvas>
</ControlTemplate>
</Button.Template>
</Button>