2016-12-22 3 views
0

Ich bin neu hier und ich versuche, eine Möglichkeit zu finden, meine eigene benutzerdefinierte Form in Button zu erstellen.Erstellen Sie Ihre eigene Knopfform

Soll ich eine Klasse dafür erstellen? Oder eine XML-Datei? Ich muss eine Schaltfläche erstellen, die wie eine Tabelle aussieht. Ich habe diesen Code gefunden, aber es ist schwierig ihn zu erstellen.

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); 
+3

Die Antwort auf diese Frage stark auf dem GUI-Toolkit hängt (zB Windows Forms, WPF, Gtk #, ...) Sie verwenden, so Bitte fügen Sie ein passendes Tag hinzu. –

+0

Ich weiß nicht GUI-Toolkit. Ist das eine externe Software, die einen kompletten Knopf knöpfen und zeichnen kann? Gibt es eine Möglichkeit, eine Schaltfläche in VS Blend zu erstellen und sie in Winform zu importieren? – drs

+1

"Ich kenne kein GUI Toolkit." - Wenn Sie visuelle Elemente wie Schaltflächen in einem Fenster hinzufügen, verwenden Sie ein GUI-Toolkit. Es ist * wichtig *, dass Sie herausfinden, welche Sie verwenden, bevor Sie etwas damit machen. "Ist das eine externe Software" - nein. "GUI-Toolkit" ist ein allgemeiner Begriff für jeden Satz visueller Komponenten, die zum Erstellen einer grafischen Benutzerschnittstelle (d. H. Einer GUI) verwendet werden können. "Erstellen Sie eine Schaltfläche in VS Blend und importieren Sie sie in winform" - VS Blend zielt auf WPF ab, bei dem es sich um ein anderes GUI-Toolkit von Windows Forms handelt. Sie können interagieren, aber ehrlich gesagt, es gibt andere Dinge, die Sie zuerst lernen sollten. –

Antwort

0

Wenn Sie eine Taste für eine C# WinForm Anwendung erstellen möchten, ist dann hier eine Probe von einem ovalen Knopf ein Panel-Steuerung. Wenn Sie eine benutzerdefinierte Form bereitstellen möchten, tun Sie dies im OnPain-Ereignis. Versuchen Sie es mit dem folgenden Code, und Sie werden herausfinden, was zu tun ist.

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 
0

ich mit @Sinatr darüber einig, dass WPF einfacher wäre. Wenn Sie mit WPF entscheiden, gehen Sie so etwas tun könnten:

<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> 
+0

Wie kann ich eine Schaltfläche zum Lesen aus dieser XML-Datei festlegen? – drs

+0

Wenn Sie ein Projekt erstellen, müssen Sie eine WPF-Anwendung erstellen. Dadurch wird eine Datei "mainwindow.xml" erstellt, der Sie hinzufügen können. Es gibt viele Tutorials online über WPF und XAML, aber du könntest auch dieses hier ausprobieren: http://www.wpf-tutorial.com/xaml/what-is-xaml/ – sclarke81

Verwandte Themen