Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
For some reason, I'm not getting the correct class of the component under the cursor with the following code. I get the parentWindow class instead. And I get exactly the same result if I try with ChildWindowFromPoint. When I retrieve a list of childWindows under the parent, I get Buttons and Edit fields, so why cant I catch them with this code ?
VB
[DllImport("user32.dll")]
        public static extern IntPtr WindowFromPoint(POINTAPI pt);


        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);


	    Point p = Cursor.Position;
            POINTAPI papi = new POINTAPI();
            papi.x = p.X;
            papi.y = p.Y;
            
            IntPtr window = WindowFromPoint;

            int nRet;
            System.Text.StringBuilder ClassName = new System.Text.StringBuilder(100);
            nRet = GetClassName(window, ClassName, ClassName.Capacity);

            string userInfo = "";

            if (nRet != 0)
                userInfo = ClassName.ToString();
            else
                userInfo = "n/a";
Posted
Updated 18-Dec-10 20:56pm
v2
Comments
TweakBird 19-Dec-10 2:56am    
<pre> tag added.

Hello,
So your got parent instead of child, that's right ?
Reading this ChildWindowFromPoint [^] doc, it appears that RealChildWindowFromPoint[^] say "Retrieves a handle to the child window at the specified point", this should solve your problem...

HWND WINAPI RealChildWindowFromPoint(
  __in  HWND hwndParent, // window in your code
  __in  POINT ptParentClientCoords // p in your code
);


Be care that as p is in screen coordinate, you have to convert in client coordinate of window ( ScreenToClient function ).

I didn't test my solution, this is just the way I think to do it...

Regards
 
Share this answer
 
v2
Your theory seems to be OK and the simple WindowFromPoint() should do the job well for you. WindowFromPoint() usually does not work when the WindowProc of specified HWND returns HTTRANSPARENT in response to WM_NCHITTEST, but this is very-very rare. In that case WindowFromPoint() returns the window behind the specified HWND but in our scenario this isn't the problem. I think something must be wrong in your function declaration, and I don't really understand where do you pass the point to the WindowFromPoint() function call. Check the correct function declaration here[^], and then pass the current cursor position to the function. Let's try it again! :D
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900