Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to design an irregular shape button??
 

How to design an irregular shape button??

Hi all,

I wanna design a button (or any other component with mouseClick event; for example, pictureBox) with irregular shape, which means users will only be able click inside the image area of the button. (The image of the button could be a gif picuture with transparent background if necessary) Then the mouseClick event happened and do corresponding jobs.
On the other words, how to design such a button that only its image area will detect the mouseClick event??

Any help would be really appreciated!! Thanks in advance!!

Sincerely,
Joseph
josephneutron  Thursday, March 19, 2009 9:11 AM
There are a couple of examples on my site which you may find useful.

This example shows how to create a control which will setit's region based upon the background image set:

Create a Shaped Control.
http://dotnetrix.co.uk/custom.htm


This example shows how to create a custom control which will act as a Button.

Creating Custom Buttons by Implementing IButtonControl.
http://dotnetrix.co.uk/button.htm


You can extract and combine the relevant code from each example to create a button which will shape itself according to it's background image.
Mick Doherty
http://dotnetrix.co.uk
Mick Doherty  Saturday, March 21, 2009 9:57 AM
On suggestion.

On button click make sure the mouse pointer is inside the image and if its inside then only execute the code.

Thanks, A.m.a.L
A.m.a.L - aditi.com - Think Product  Thursday, March 19, 2009 1:09 PM
There are a couple of examples on my site which you may find useful.

This example shows how to create a control which will setit's region based upon the background image set:

Create a Shaped Control.
http://dotnetrix.co.uk/custom.htm


This example shows how to create a custom control which will act as a Button.

Creating Custom Buttons by Implementing IButtonControl.
http://dotnetrix.co.uk/button.htm


You can extract and combine the relevant code from each example to create a button which will shape itself according to it's background image.
Mick Doherty
http://dotnetrix.co.uk
Mick Doherty  Saturday, March 21, 2009 9:57 AM
Hi Mick,

Thanks a lot!! You gave great samples.
If I didn't get it wrong, your idea is to mask all the possible background pixels to be color transparent. So that it can be useful for any background image loaded to the control. This idea is really flexible for any cases; however what I need for my project is not necessary to be this flexible. The background image could be fixed and never changed once my program is on-line. So, Is there any other methods that is more rough but less computation in real-time?

best regards,

Joseph

josephneutron  Thursday, March 26, 2009 1:50 AM
Hi Joseph,

The basic principle is going to be the same. You define a GraphicsPath and set the controls region to use that path.

I believe there is a shaped button example on codeproject which may help, but I don't have a link to hand.

Depending upon your exact requirements, it may be that you don't need a control at all. Simply draw the GraphicsPath to the form and HitTest the path in the forms OnClick method.

Regards,

Mick.
Mick Doherty
http://dotnetrix.co.uk
Mick Doherty  Thursday, March 26, 2009 6:34 PM

You can use google to search for other answers

Custom Search

More Threads

• Extending Treeview
• Two Name property of UserControl
• Code generation for property <xxxx> failed
• what the heck? Clicked on the background image property of a form, and everything went haywire!
• Deserialization
• Nullreference exception in VS2008 designer
• lost with glyph, adorner and behaviorservice,...
• Deriving from abstract UserControls
• How to detect any mouse click or key press anywhere, either inside or outside the application
• How to Print a form in C#?