puklaus's picture

PointToClient()

I need to convert screen coordinates to the window and PointToClient() method isnt
implemented but only on windows.

So, here's linux code (mono's windows.forms sources was useful http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/Managed.Windows.F...):

To X11GLNative.cs

#region PointToClient
 
static object XlibLock=new object(); // ?
public void PointToClient(ref System.Drawing.Point p)
{
   int	ox, oy;
   IntPtr child;
 
   lock (XlibLock) { // dont know what that means
	Functions.XTranslateCoordinates(window.Display, window.RootWindow, window.WindowHandle, p.X, p.Y, out ox, out oy, out child);
   }
   p.X = ox;
   p.Y = oy;
}
#endregion

And here mac code, cant test it and it is most likely not yet right
but if anyone can test/fix it, great:

To CarbonGLNative.cs:

        #region PointToClient
        public void PointToClient(ref System.Drawing.Point p)
        {
            // not sure. this should be the same as linux  window.WindowHandle  (if i understand that correctly) ...
            IntPtr handle = window.WindowRef;
 
            System.Drawing.Point t = new System.Drawing.Point(p.X, p.Y);
            System.Drawing.Point point = Carbon.API.ConvertScreenPointToClient(handle, t);
            p.X = point.X;
            p.Y = point.Y;
        }
        #endregion

And in CarbonAPI.cs, in API class:

        public static System.Drawing.Point ConvertScreenPointToClient(IntPtr handle, System.Drawing.Point point)
        {
            System.Drawing.Point converted_point = new System.Drawing.Point();
            Carbon.Rect window_bounds = new Carbon.Rect();
            Point native_point = new Point();
 
            Carbon.API.GetWindowBounds(handle, WindowRegionCode.StructureRegion /*32*/, out window_bounds);
 
            native_point.H = (short)(point.X - window_bounds.X);
            native_point.V = (short)(point.Y - window_bounds.Y);
 
            HIViewConvertPoint(ref native_point, IntPtr.Zero, handle);
 
            converted_point.X = (int)native_point.H;
            converted_point.Y = (int)native_point.V;
 
            return converted_point;
        }
        [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
        extern static int HIViewConvertPoint(ref Point point, IntPtr pView, IntPtr cView);

Yup and it compiles but not tested otherwise.

Here http://dhost.info/haima/pointtoclient.7z you can find little test and compiled exe (and modificated opentk), it
works on win and linux..
In this test, it opens opentk window and when pressing space, it puts mouse pointer center of it.

And one more thing:

            // pointer to middle of the window
            if (Keyboard[Key.Space])
            {
                Point p=new Point(300, 200);
 
                Point pos = PointToClient(p);
                pos.X = Width - pos.X;    // that I down get, why i must decrase from width(and height) these positions..
                pos.Y = Height - pos.Y;
 
                System.Windows.Forms.Cursor.Position = pos;
            }

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
puklaus's picture

Thank you very much,now I can continue coding with using this method (I wasnt on home these days so this comes late :)).
I will even check your mods if i learn something :)

"PointToScreen"
Not yet know whenever this is needed but if its sometime, its great you made this too.