Owner-Drawn Buttons in C#

J

Jeff

Hello,

I need a button control that will allow me to display mutiple icons/bitmaps
simultaneously.
Say for example you had a standard Forms.Button with text "Device 1" and you
need to display
small icons in each corner of the button to show state/status of Device 1.

Is an owner-drawn button my only option? This sounds like a daunting task.
I am unable to find any code examples or 3rd party products that even come
close.
Its hard to believe that I am the only one that needs such an animal.

Please help if you can.

Thanks, Jeff
 
A

Andy Becker

Jeff said:
Hello,

I need a button control that will allow me to display mutiple icons/bitmaps
simultaneously.
Say for example you had a standard Forms.Button with text "Device 1" and you
need to display
small icons in each corner of the button to show state/status of Device 1.

Is an owner-drawn button my only option? This sounds like a daunting task.
I am unable to find any code examples or 3rd party products that even come
close.
Its hard to believe that I am the only one that needs such an animal.

Please help if you can.

Thanks, Jeff

I needed such an animal, and found the basis for it here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbpowerpack.asp

However, that is VB, which happened to work for me. It looks like a
sizeable effort to port to C#. But perhaps it can be enought to steer you
in the right direction?

Best Regards,

Andy
 
M

Mick Doherty

You'll find source for an OwnerDraw button on my site.
http://dotnetrix.co.uk/buttons.html

An alternative is to create a new bitmap of appropriate size and draw an
icon in each corner of the bitmap. Then assign this new bitmap to the
buttons image property.
 
J

Jeff

Mick,

Thanks for the help. I'm looking at your code now.
Wow, you worked hard on this one!

I have been trying to get the bitmap approach to work, but have run into a
Bitmap.MakeTransparent roadblock. This approach should be simple...

If you have a moment, please look at my "Frustration..." post that discusses
the
MakeTransparent bug.

Regards, Jeff
 
M

Mick Doherty

What's the problem with Bitmap.MakeTransparent()?

Try this code out.

Rectangle BitmapRect = Rectangle.FromLTRB(0, 0, button1.Width,
button1.Height);
BitmapRect.Inflate(-6, -6);
Bitmap srcImage = new Bitmap(@"c:\test.bmp");
srcImage.MakeTransparent();
Bitmap bmp = new Bitmap(BitmapRect.Width, BitmapRect.Height);
Graphics g = Graphics.FromImage(bmp);
//TopLeft corner
g.DrawImage(srcImage, Point.Empty);
//Topright corner
g.DrawImage(srcImage, BitmapRect.Width - srcImage.Width, 0, srcImage.Width,
srcImage.Height);
//BottomLeft corner
g.DrawImage(srcImage, 0, BitmapRect.Height - srcImage.Height,
srcImage.Width, srcImage.Height);
//BottomRight corner
g.DrawImage(srcImage, BitmapRect.Width - srcImage.Width, BitmapRect.Height -
srcImage.Height, srcImage.Width, srcImage.Height);
button1.Image = new Bitmap(bmp);
g.Dispose();
bmp.Dispose();
 
J

Jeff

Mick,

I owe you one!
Your 4-corners icon code works great.
I'm not sure why this call to MakeTransparent( ) works
and the other doesn't (see Frustration post).

Also, your Owner-Drawn button code on your site rocks!
Strategically setting the corner radius even yields a great round button!

For now, I will stick to the simpler "4-corners" code.

Thank you, thank you, thank you!

Regards, Jeff
 
J

Jeff

Mick,

I was focusing on the wrong class.
The Bitmap::MakeTransparent( ) looks fine.
However, Control::BackgroundImage appears to be the culprit.
Using ButtonBase::Image appears to work fine with the bitmap that
was manipulated by MakeTransparent( ).

Lesson learned: avoid BackgroundImage, and use Image instead!

Thanks again.

Regards, Jeff
 
M

Mick Doherty

I was about to ask why you were using BackgroundImage instead of Image.
Although, even with BackgroundImage I'm not seeing any problem. Perhaps it's
something to do with the image you're using.
 
J

Jeff

Mick,

I used BackgroundImage because I didn't know any better.
Prior to getting your solution, I really investigated the
MakeTransparent & BackgroundImage issue. It appears there
may be some platform dependance with BackgroundImage. Some
people see it, others don't. I have tried all types of images
and they all result in a black background with other buttons drawn
on top. (Its pretty wacky). Maybe its a video card/driver
thing?

Thanks for all of your help.

Cheers, Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top