3

I have done a bit of research but cannot seem to find what I am looking for.

What I want to do is make a "custom" button in a windows form. This would basically just be a matter of changing the default "grey" background to a custom image. A custom image would also be used for when hovering over and clicking the button.

This is not just a matter of changing the background image as the image I want to use has rounded edges with a transparent background and I want custom image for hovering / clicked. I want everything else about the button to behave in the same manner as a normal button.

Is this possible?

5
  • 1
    Sounds more like you want to use WPF. However, do you have the option to move to WPF or are you stuck in Windows Forms? Commented Feb 16, 2012 at 23:43
  • I am pretty much restricted to Windows Forms for this. Commented Feb 16, 2012 at 23:47
  • The fastest route from here to there is likely to be to license the control you need, not write it from scratch. Commented Feb 17, 2012 at 0:21
  • 4
    The Button class is a bit unusual in Winforms, it has very few options to alter its appearance with code. There a four distinct button renderer classes but they are all private. You'll need to derive your own class from ButtonBase. That base class has enough plumbing to make your class behave like a button, but not enough to make it look like a button. Well, that's what you want to change. Commented Feb 17, 2012 at 0:26
  • @BradSemrad This has nothing to do with WPF. Commented Jul 13, 2015 at 10:09

2 Answers 2

4

The solution I found was to set the FlatStyle of the button to Flat and set all the borders to 0. I then had a problem with the focus of the button (it displayed a little border). To solve this I followed this tutorial:

http://dotnetstep.blogspot.com/2009/06/remove-focus-rectangle-from-button.html

With this in place all I had to do was add events to the button so that the image was changed when a certain action was carried out on it:

    private void button1_MouseLeave(object sender, EventArgs e)
    {
        this.button1.Image = Properties.Resources._default;
    }

    private void button1_MouseEnter(object sender, EventArgs e)
    {
        this.button1.Image = Properties.Resources._hover;
    }        

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        this.button1.Image = Properties.Resources._clicked;
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        this.button1.Image = Properties.Resources._default;
    }

Hope this will help someone else!

Sign up to request clarification or add additional context in comments.

Comments

3

It is called owner-drawn button

refer to:
Mick Dohertys' .net Tips and Tricks - Tips / Button
GlowButton - A Glowing Button Control
A shiny orb button in GDI+

1 Comment

thanks for the reply. Hmmm this looks like it will be a long task! i'll take a look into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.