Ownderdraw vs Creating Control from scratch?

T

Try Guy

I'm not sure if I really understand this whole ownderdraw thing...

i want to create my own Windows Forms control (NOT USERCONTROL)...is it
ownerdrawing I should start learning or something else?
And where do you learn this stuff? I've been looking for tutorials but all
are for C # or how to use prebuild stuff... I wanna learn how to really do
it.

If I learn to use Ownerdraw on Menuitems... can I then also use the same
knowledge for modifying a combobox or is it very different from control to
control?

/Try Guy
 
B

Bob Powell [MVP]

Objects derived from Control are easy to create. To paint the control,
override the OnPaint method and paint on the Graphics object provided in the
PaintEventArgs parameter. You may also wish to override the
OnPaintBackground to prevent the default clearing of the graphics or to add
some custom background.

This simple control draws a circle shape..

Public Class CircleControl
Inherits Control

Public Overrides Sub OnPaint(ByVal e as PaintEventArgs)
Dim b As New SolidBrush(Me.ForeColor)
e.Graphics.DrawEllipse(b,0,0,Me.ClientSize.Width, Me.ClientSize.Height)
b.Dispose()
End Sub

End Class

Existing controls such as the combobox and textbox are based on the old
Win32 controls and are often more difficult to customize. Some controls do
provide owner-draw options which you can take advantage of. The mechanism
for OwnerDraw is more or less the same as that for MenuItem inasmuch as
there is generally a MeasureItem event in which you declare the size of the
object you're drawing and a DrawItem event where you actually paint the
bits. For a custom control, OnPaint is all you really need.

As for tutorials, this one on DevCity might be useful to you but I think
it's on the slowest server in the known universe.

http://www.devcity.net/net/article.aspx?alias=ssteps_customcontrol

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
T

Try Guy

HeY!

Thank you! That looks like what I'm loking for and thanx for straiting
things out for me... been playing around with the DrawItem even now in a
listbox (while I was waiting for an answer) and it gives me a lot more ideas
on what I can actually do.. and off course raises many many questions:)

Will read the tutorial now:)

Thanx again!

Best Regards
/Try Guy
 

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