graphics object of Button in CF 3.5

C

Chris

I want to extend a standard Button class (VS 2008, Compact Framework 3.5) - I
need to change background, draw lines etc. Unfortunately it is impossible to
override OnPaint method (I mean, I can override it, but this method never
fires). I need graphics object which is available in OnPaint method. Anyway -
how to create graphics object related to Button? There is no CreateGraphics
method in Button class. When I cast Button to Control and call
CreateGraphics() (it is available in Cotnrol class) UnSupportedException is
thrown.
Any ideas how to draw on a button? Or how to get Graphics object related to
the button?
 
P

Peter Foot

You can't do this with the standard Button class which is a simple wrapper
over the native button control. Instead create your own custom control where
you can draw what you like from scratch and make use of the Click event.
There is an example here:-
http://msdn.microsoft.com/en-us/library/aa446518.aspx

Peter

--
Peter Foot
Microsoft Device Application Development MVP
peterfoot.net | appamundi.com | inthehand.com
APPA Mundi Ltd - Software Solutions for a Mobile World
In The Hand Ltd - .NET Components for Mobility
 
C

Christian Resma Helle

As Peter mentioned you could just create an owner drawn one button control
http://christian-helle.blogspot.com/2007/09/buttonex-owner-drawn-button-control.html

You could also subclass the button control and do your own painting on
WM_PAINT and WM_ERASEBKGND. Not a simple solution but there a few articles
out there explaining the procedure.

Here's a few MSDN links:

Subclassing Controls with a Managed WndProc
http://msdn.microsoft.com/en-us/library/ms229681.aspx

How to: Subclass a TreeView by Using Native Callbacks
http://msdn.microsoft.com/en-us/library/ms229669.aspx

How to: Subclass a Button by Using Native Callbacks
http://msdn.microsoft.com/en-us/library/ms229661.aspx
 

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