Transforming mouse coordinates

G

Guest

Hi

Thanks for taking the time to check this post. Any help/suggestions are
appreciated.

I have an owner drawn (GDI+) document generation application that is made up
of embedded objects. The document acts simply as a layout container and
delegates painting to the embedded items.

The document supports scaling and rotation of these items. This is handled
using transformations to hide to implementation of this functionality from
the embedded items.

The problem I am struggling to resolve is that some of the embedded items
give user feedback based on the position of the cursor (this is passed to the
paint method of the items). If the item has been rotated, it still stores
any internal hotspot coordinates in the none rotated form and the cursor
position no longer matches (it has no knowledge of the transformation).

ie ... the cursor triggers the hotspot feedback when it is placed on the
document in the place the hotspot would be without any rotation.

Is there a simple way to transpose the coordinates. I have the angle of
rotation, the origin and the start point .... I need the mouse position
relative to the item as if it had not been rotated.

Any ideas. Ideally, I would prefer not to have to do the maths the old
fashioned way, as I would like to support more transformations at a later
time.

Thanks for leaving your thoughts.
 
B

Bob Powell [MVP]

Just use the inverse of the Matrix used to draw the graphics. See
Matrix.Invert.

This technique is called "backtracking" and is shown in the Windows Forms
Tips and Tricks article on how to backtrack the mouse.

--
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.
 

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