【问题标题】:Create your own button shape [duplicate]创建自己的按钮形状[重复]
【发布时间】: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是一些链接的答案。至于你的问题,目前还不清楚,提供的代码与它无关。您能否向我们展示此类按钮的草图或更好地描述您正在尝试做什么?

标签: c# winforms button


【解决方案1】:

我同意@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>

【讨论】:

  • 如何设置一个按钮来读取这个 xml 文件?
  • 当你创建一个项目时,你需要创建一个 WPF 应用程序。这将创建一个 mainwindow.xml 文件,您可以将其添加到该文件中。网上有很多关于 WPF 和 XAML 的教程,但你也可以试试这个,从 wpf-tutorial.com/xaml/what-is-xaml
【解决方案2】:

如果您尝试为 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

【讨论】:

    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    相关资源
    最近更新 更多