How to configure Button Control, like this one?

I want to create a custom button control (the image button is ok), like this one.

I am a new user, so I can not post the image here. So I uploaded the picture here

I'm desperate now, having tried some lessons

Any suggestion is much appreciated.

thank

+3
source share
3 answers

You can create a class that inherits from Button to save all your styles in one place. To make the hang and pressed states you can redefine the mouse, enter / leave button events and change the style.

( , ). , .

public class MossieButton : Button
{
    private static Font _normalFont = new Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

    private static Color _back = System.Drawing.Color.Grey;
    private static Color _border = System.Drawing.Color.Black;
    private static Color _activeBorder = System.Drawing.Color.Red;
    private static Color _fore = System.Drawing.Color.White;

    private static Padding _margin = new System.Windows.Forms.Padding(5, 0, 5, 0);
    private static Padding _padding = new System.Windows.Forms.Padding(3, 3, 3, 3);

    private static Size _minSize = new System.Drawing.Size(100, 30);

    private bool _active;

    public MossieButton()
        : base()
    {
        base.Font = _normalFont;
        base.BackColor = _border;
        base.ForeColor = _fore;
        base.FlatAppearance.BorderColor = _back;
        base.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        base.Margin = _margin;
        base.Padding = _padding;
        base.MinimumSize = _minSize;
    }

    protected override void OnControlAdded(ControlEventArgs e)
    {
        base.OnControlAdded(e);
        UseVisualStyleBackColor = false;
    }

    protected override void OnMouseEnter(System.EventArgs e)
    {
        base.OnMouseEnter(e);
        if (!_active)
            base.FlatAppearance.BorderColor = _activeBorder;
    }

    protected override void OnMouseLeave(System.EventArgs e)
    {
        base.OnMouseLeave(e);
        if (!_active)
            base.FlatAppearance.BorderColor = _border;
    }

    public void SetStateActive()
    {
        _active = true;
        base.FlatAppearance.BorderColor = _activeBorder;
    }

    public void SetStateNormal()
    {
        _active = false;
        base.FlatAppearance.BorderColor = _border;
    }
}
+10

, , .

button1.FlatStyle = FlatStyle.Flat;
button1.BackgroundImage = Bitmap.FromFile("image.jpg");
+1

, - , ,

this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Image = "Any Image"
this.button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.button1.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;

  private void button1_Click(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "OnClick";
    }

    private void button1_MouseEnter(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "Enter";
    }

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        //Code for Image Appearance.
        button1.Text = "Normal";
    }

Update:

I don’t know if I will be corrected or not, but I think that you can also achieve your goal by putting a button and a shortcut inside the panel and arranging them according to your choice. Do button1.FlatStyle = System.Windows.Forms.FlatStyle.Flatat the beginning with Label.Text="Normal". Then, on Mouse, enter into the Panel a rectangle with a border around the button and change the label text to " Hover". As when you click on the panel, you also change the border of the rectangle according to you and do label.Text="OnClick".

+1
source

All Articles