.NETGURU
Best approach
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-drawing' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Chris Goldfarb
-- Copied from [winforms-cs] to [ngfx-drawing] by Click here to reveal e-mail address --Greetings,I know there is a more elegant way to handle this than using a globalreference, so I'm asking the experts.Assume I have a control "Map" that inheritsSystem.Windows.Forms.PictureBox. It contains a 2-dimensional array of"Hex" objects. These Hex objects overlay the Map in a traditional hexgrid.Each Hex knows where it's place is on the Map as far as x,y Pixelcoordinates. For the sake of OO, I'd *really* like to be able to exposemethods such as:Hex.DrawDot();How do I obtain a handle to Map's PictureBox from a given Hex?Thanks in advance,Chris
Reply to this message...
 
    
Mitch Denny (VIP)
Chris,

If you look at the PictureBox class you will see that
it inherits a Handle property from the class. This IntPtr
property represents the Hwnd (Window Handle) for that
particular control. What you need to do is attach a
drawing surface to that control. You can do that with
the Graphics class.

    Graphics g = Graphics.FromHwnd(control.Handle);

Once you have the graphics object you can use its
methods to draw whatever you like. I'll leave it to
you to decide how you actually implement the solution
and get the Hex's to actually draw on the surface.

I'd recommend having some sort of view class which
you pass the data to and it handles drawing them on. Perhaps
model it after MVC?

----------------------------------------
- Mitch Denny
- http://www.warbyte.com
- Click here to reveal e-mail address
- +61 (414) 610-141
-

[Original message clipped]

Reply to this message...
 
    
Chris Goldfarb
Mitch,

This is exactly what I was looking for - thanks.

This approach is great in that the Hex object doesn't need to know who
it is that it belongs to (I pass in the hwnd via constructor). This
makes it nice and portable, as well as the bigger advantage of being
able to say "Hex, draw yourself".

To speed up rendering routines, I've kept an object-wide Graphic
instance created during the constructor, rather than re-creating the
Graphic each time on the fly. I'm a little concerned about the memory
stamp though, given that any given map may have a couple thousand
hexes...

Thanks again Mitch!

Chris

[Original message clipped]

Reply to this message...
 
    
Mitch Denny (VIP)
Chris,

Glad to see this was a solution for you. If you have a
bit of time and the inclination, I recommend that you take
a squizz at the following URL on the GotDotNet site:

http://www.gotdotnet.com/team/windowsforms/windowsformspainting.aspx

It discusses some interesting concepts and serves as a
good backgrounder to working with GDI.NET (GDI+???) and
Windows Forms.

In your specific case, I'd avoid having a Graphics object
which each Hex has a constant reference to. I'd probably
implement an abstract Shape class (or IShape interface)
which has a Draw method that takes a Graphics object as
a parameter. That way you could queue up your Hex's and
draw then sequentially, and help avoid any nasty threading
issues down the track.

----------------------------------------
- Mitch Denny
- http://www.warbyte.com
- Click here to reveal e-mail address
- +61 (414) 610-141
-

[Original message clipped]

Reply to this message...
 
    
Chris Goldfarb
Thanks again, Mitch. BTW, if anyone is curious as to what this is
about:

http://www.warplanner.com

The app is currently written in VB6 and I'm a-itchin' to change it.

[Original message clipped]

Reply to this message...
 
 
System.Drawing.Graphics
System.IntPtr
System.Windows.Forms.PictureBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification