A trap that novices often fall into is to take zooming and panning into
account when doing drawing. This is entirely the wrong approach and causes
lots of problems because wherever you zoom or pan you must also unzoom and
pan back to deal with mouse interactions and so-on.
The correct technique is to set up a transform that takes care of your
zooming and / or panning and draw everything as if it were at 1:1 scale
allowing the display pipeline to put the pixels in the right place for you.
First apply a transform that copes with whatever level of zoom and pan you
need. Second, save the Graphics state either using Graphics.Save
(BeginContainer/EndContainer is broken) or using a home-grown matrix stack.
Third apply any other transform such as scale or rotation. Fourth, draw all
objects at 1:1 scale. Fifth, restore the graphics state back to the original
setting. Nesting states can give complex yet consistent drawing object
models.
The inverse of the matrix used to draw any specific object can be used to
"backtrack" the mouse from the current client coordinates to real-world
measurements in the graphics system.
See the GDI+ FAQ for more detail on all these subjects.
--
Bob Powell [MVP]
Visual C#, System.Drawing
Ramuseco Limited .NET consulting
http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
<jo*********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I actually do set the page units to millimeters. You mention:
"This gives each unit in world space the value of 1 mm"
What I discovered however is that this results in a line being
physically drawn on the screen in millimeters. This is not "world"
space, at least not the way most programmers envision world space.
World space is whatever a line length represents in the real world and
not how physically long it appears on a screen, which can be modified
with zooming factors.
What I am trying to avoid of course is any relationship between
physical lengths on the screen and the real world lengths that a line
length represents.
So if I want to draw a line that is a meter long but want it to appear
on the screen as 10 mm, I need to set ScaleTransform to some value that
scales from meters down to millimeters. The problem is, that a straight
meter-to-mm conversion isn't good enough. You need to take into account
the zooming factor. Why is that? Imagine displaying a single rectangle
on a screen. The rectangle is 10 meters wide by 20 meters high. If I
want the rectangle to fit exactly the height of the screen (such as in
Word with "fit to screen"), you need to apply a secondary amount of
scaling to the ScaleTransform and this can only be done by taking the
height of pixels of the client area into account. The client size
property returned in an OnPaint event gives its size in pixels and not
in millimeters (or whatever units you have set).