User32 WindowFromPoint issue/question

759 views
Skip to first unread message

Edward Jakubowski

unread,
Jan 13, 2014, 4:53:50 PM1/13/14
to jna-...@googlegroups.com
I'm attempting to write an application that can get a Window HWND pointer based on the mouse location.  I've seen working apps and source code in C++ but for some reason this isn't working for me.  The results I get from "WindowFromPoint" always return the same 2 HWND's and they are usually my own Java app's HWND.  I am doing this while I try to drag a JLabel, I've even attempted to add an offset in case it is simply finding my cursor or JLabel item I'm dragging.  Anyone know if I'm doing something wrong here?  Thanks!  Below is an example...

public interface User32 extends W32APIOptions {  
User32 instance = (User32) Native.loadLibrary("user32", User32.class, DEFAULT_OPTIONS);  
boolean ShowWindow(HWND hWnd, int nCmdShow);  
boolean SetForegroundWindow(HWND hWnd);  
HWND FindWindow(String winClass, String title);
boolean GetCursorPos(int[] ascoor);
HWND WindowFromPoint(int xPoint, int yPoint);
}  

...

int[] getPos = new int [2];
    User32.instance.GetCursorPos(getPos);
    HWND hwnd = User32.instance.WindowFromPoint(getPos[0], getPos[1]);
System.out.println(getPos[0] + "," + getPos[1]);

// hwnd is always my own window...

Daniel Doubrovkine

unread,
Jan 13, 2014, 5:08:55 PM1/13/14
to jna-...@googlegroups.com
WindowFromPoint takes a POINT, which is a struct of 2 longs. I don't think it's the same as two int arguments.


--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Edward Jakubowski

unread,
Jan 13, 2014, 5:21:55 PM1/13/14
to jna-...@googlegroups.com
I actually tried that first, but WindowFromPoint will always return NULL when I used the POINT... Here is an example I tried with POINT...
public interface User32 extends W32APIOptions {  
User32 instance = (User32) Native.loadLibrary("user32", User32.class, DEFAULT_OPTIONS);  
boolean ShowWindow(HWND hWnd, int nCmdShow);  
boolean SetForegroundWindow(HWND hWnd);  
HWND FindWindow(String winClass, String title);
boolean GetCursorPos(int[] ascoor);
//HWND WindowFromPoint(int xPoint, int yPoint);
HWND WindowFromPoint(POINT point);
}  


int[] getPos = new int [2];
    User32.instance.GetCursorPos(getPos);
    POINT pp = new POINT();
    pp.x = getPos[0];
    pp.y = getPos[1];
    HWND hwnd = User32.instance.WindowFromPoint(pp);
        // this hwnd will always be null for some reason

    //HWND hwnd = User32.instance.WindowFromPoint(getPos[0], getPos[1]);
System.out.println(getPos[0] + "," + getPos[1]);

Daniel Doubrovkine

unread,
Jan 13, 2014, 6:20:08 PM1/13/14
to jna-...@googlegroups.com
I can confirm that. My C program properly returns values, but the JNA equivalent is returning NULL in all cases. And you're right that with two int arguments it seems to work (better). This is also something that has been reported on the PInvoke side of things: http://www.pinvoke.net/default.aspx/user32.windowfrompoint, see Known Issues.

I have no idea why.

Edward Jakubowski

unread,
Jun 5, 2014, 10:38:38 AM6/5/14
to jna-...@googlegroups.com
In case anyone tries to do this in the future... I was able to get something consistently working for getting the HWND from the mouse position.  Not sure if this is the best method, but it seems to work well.

    public static final int POINT_Y(long i)
    {
        return (int) (i  >> 32);
    }
   
    public static final int POINT_X(long i)
    {
        return (int) (i & 0xFFFF);
    }

      public interface User32Ex extends W32APIOptions { 
        User32Ex instance = (User32Ex) Native.loadLibrary("user32", User32Ex.class, DEFAULT_OPTIONS); 

      boolean GetCursorPos(long[] lpPoint); //use macros POINT_X() and POINT_Y() on long lpPoint[0]
        HWND WindowFromPoint(long point);
      }

        long[] getPos = new long [1];
        User32Ex.instance.GetCursorPos(getPos);
        HWND hwnd = User32Ex.instance.WindowFromPoint(getPos[0]);
Reply all
Reply to author
Forward
0 new messages