GDI problem

M

Michele Santucci

Hallo,

I have a small (hopefully) problem... I defined an user control that in the
Paint overriden method does the following things:

protected override void OnPaint( PaintEventArgs e )
{
e.Graphics.Clear( BackColor );

e.Graphics.SmoothingMode = _SmoothingMode;
e.Graphics.CompositingQuality = _CompositingQuality;
e.Graphics.InterpolationMode = _InterpolationMode;
e.Graphics.TextRenderingHint = _TextRenderingHint;

e.Graphics.TranslateTransform( Width / 2,Height / 2 );
e.Graphics.RotateTransform( -180 );

// Apply global transform paramenters
e.Graphics.RotateTransform( _Rotation );
e.Graphics.TranslateTransform( _XOffset,_YOffset );
e.Graphics.ScaleTransform( _Zoom,_Zoom );
//

_PaintBackground( e.Graphics );
_PaintForeground( e.Graphics );

e.Graphics.ResetTransform();

base.OnPaint( e );
}

This allow me to draw foreground and background graphic elements using just
_Rotation, _XOffset, _YOffset, _Zoom parameters to change the
'point-of-view' on the global scene.
Everything works fine but now I'm stuck with a problem ... I have to
retrieve the 'virtual' coordinate of a point on the viewport
corresponding to the actual mouse position.
The very first question is 'how do I do that?' ... the second one is
'where?'

I figured that the answer to the first question is to apply the same
transformations applied to the Graphic object
just to the location point defined by mouse position but I don't know how to
reply that...

The answer to the second is more difficult for me to figure out since, for
my knowledge the only place where the e.Graphics state is available is
inside the OnPaint method while the mouse position update is available
trough the OnMouseMove method ...

I think there's a pretty simple solution to this...
 
P

Peter Duniho

Michele said:
[...]
This allow me to draw foreground and background graphic elements using just
_Rotation, _XOffset, _YOffset, _Zoom parameters to change the
'point-of-view' on the global scene.
Everything works fine but now I'm stuck with a problem ... I have to
retrieve the 'virtual' coordinate of a point on the viewport
corresponding to the actual mouse position.
The very first question is 'how do I do that?' ... the second one is
'where?'

I figured that the answer to the first question is to apply the same
transformations applied to the Graphic object
just to the location point defined by mouse position but I don't know
how to
reply that...

The answer to the second is more difficult for me to figure out since, for
my knowledge the only place where the e.Graphics state is available is
inside the OnPaint method while the mouse position update is available
trough the OnMouseMove method ...

I think there's a pretty simple solution to this...

Me too. :)

The transformation you apply in the OnPaint() method is based on state
within your class (it appears…you didn't bother to post a complete code
example, so I can only guess). But, you could instead just maintain a
System.Drawing.Drawing2D.Matrix instance itself for those values as your
state. Then you just apply the inverse of the transform to your mouse
coordinates to map back to the "world" coordinates (see the
Matrix.Invert() method).

Alternatively, you can build a new transformation Matrix from scratch
using the existing state, any time you want to map back (see the
Matrix.TransformPoints() method).

Pete
 
P

Peter Duniho

Michele said:
[...]
I think there's a pretty simple solution to this...

I almost forgot…

Please do not multi-post. If you feel you must post a given question to
multiple newsgroups, please learn to cross-post properly.

Thanks,
Pete
 
M

Michele Santucci

Thank you Peter, re all,

Glad to hear that ;-)
The transformation you apply in the OnPaint() method is based on state
within your class (it appears…you didn't bother to post a complete code
example, so I can only guess).

There was not much more code besides what I posted here.
Just routines drawing foreground and background elements and updating global
parameters.
But, you could instead just maintain a System.Drawing.Drawing2D.Matrix
instance itself for those values as your state. Then you just apply the
inverse of the transform to your mouse coordinates to map back to the
"world" coordinates (see the Matrix.Invert() method).

It works! For some reason I was trying to use just the transformation matrix
without
inverting it (and to be honest I just didn't fully understand why I do need
to do that)
but with this update everything works fine and I got my component fully
working.

P.S. please excuse me for cross posting but I had some problem with
newsgroups
handling and I was not sure of posting previous message correctly.

Thank you !

Michele Santucci
 

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