I had a ton of problems with this also. It appears that the scroll bars get
in the way. I modified the SDK samples to take the scroll bars into account
and this is what I came up with:
public static System.Drawing.Point MapVisioToWindows(
Window referenceWindow,
double visioX,
double visioY)
{
// The drawing control object must be valid.
if (referenceWindow == null)
{
// Throw a meaningful error.
throw new ArgumentNullException("window");
}
int windowsX = 0;
int windowsY = 0;
double visioLeft;
double visioTop;
double visioWidth;
double visioHeight;
int pixelLeft;
int pixelTop;
int pixelWidth;
int pixelHeight;
// Get the window coordinates in Visio units.
referenceWindow.GetViewRect(out visioLeft, out visioTop,
out visioWidth, out visioHeight);
// Get the window coordinates in pixels.
referenceWindow.GetWindowRect(out pixelLeft, out pixelTop,
out pixelWidth, out pixelHeight);
// GetWindowRect does not take the scrollbar sizes into account
pixelWidth -=
System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
pixelHeight -=
System.Windows.Forms.SystemInformation.HorizontalScrollBarHeight;
// Convert the X coordinate by using pixels per inch from the
// width values.
windowsX = (int)(pixelLeft +
((pixelWidth / visioWidth) * (visioX - visioLeft)));
// Convert the Y coordinate by using pixels per inch from the
// height values and transform from a top-left origin (windows
// coordinates) to a bottom-left origin (Visio coordinates).
windowsY = (int)(pixelTop +
((pixelHeight / visioHeight) * (visioTop - visioY)));
return new System.Drawing.Point(windowsX, windowsY);
}
public static void MapWindowsToVisio(
Window referenceWindow,
int windowsX,
int windowsY,
out double visioX,
out double visioY)
{
// The drawing control object must be valid.
if (referenceWindow == null)
{
// Throw a meaningful error.
throw new ArgumentNullException("referenceWindow");
}
visioX = 0;
visioY = 0;
double visioLeft;
double visioTop;
double visioWidth;
double visioHeight;
int pixelLeft;
int pixelTop;
int pixelWidth;
int pixelHeight;
// Get the window coordinates in Visio units.
referenceWindow.GetViewRect(out visioLeft, out visioTop,
out visioWidth, out visioHeight);
// Get the window coordinates in pixels.
referenceWindow.GetWindowRect(out pixelLeft, out pixelTop,
out pixelWidth, out pixelHeight);
// GetWindowRect does not take the scrollbar sizes into account
pixelWidth -=
System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;
pixelHeight -=
System.Windows.Forms.SystemInformation.HorizontalScrollBarHeight;
// Convert the X coordinate by using pixels per inch from the
// width values.
visioX = visioLeft +
((visioWidth / (double)pixelWidth) * (double)(windowsX - pixelLeft));
// Convert the Y coordinate by using pixels per inch from the
// height values and transform from a top-left origin (windows
// coordinates) to a bottom-left origin (Visio coordinates).
visioY = -(((visioHeight / (double)pixelHeight) * (double)(windowsY -
pixelTop)) - visioTop);
}
Hope this helps. By the way, the windows coordinates should be screen
coordinates.
- Chris