Get HWND from a mouse release


Is there a way for a user to click on a button, then drag the mouse to a child window and release the mouse while over the window and have that child window be recordered for sending keys to it?

If yes then how?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
Hi Mad_Angel,

You can implement your own drag and drop functionaly with SetCapture API:

You have to do this:
-Detect WM_LBUTTONDOWN event over your button
-Activate SetCapture to the button's window
-Change your icon to indicate you are dragging
-Detect WM_LBUTTONUP event, at this time call ReleaseCapture, and call GetWindowFromPoint to know which window is under the mouse cursor.
-Now you can take any action to that window

Good luck,
Jaime OlivaresSoftware ArchitectCommented:
Dont be confused with screen and client coordinates.
When SetCapture is active, WM_LBUTTONUP event will tell you release position in "client coordinates" convert them to "screen coordinates" using:

then obtain HWND of pointed window with:
Mad_AngelAuthor Commented:
Too there a code example?

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Jaime OlivaresSoftware ArchitectCommented:
>Too there a code example?
No there is not an specific code sample, but drag and drop is always an annoying task, in my opinion.

Just have to generate WM_LBUTTONDOWN and WM_LBUTTONUP events in your buttons's window procedure, something like:
      // some initial variables
     static BOOL capturing = FALSE;
     HCURSOR hCursor;
     POINT point;
     HWND target;

        case WM_LBUTTONDOWN:
               SetCapture(hWnd);    // your own hWnd
               capturing = TRUE;       // some flag to specify you are capturing, could be a static or global variable
               SetCursor(hCursor);   // some cursor you have loaded previously with LoadCursor or you can try with SetCursor(LoadCursor(NULL,IDC_HAND));
        case WM_LBUTTONUP:
               if (capturing) {
                         point.x = LOWORD(lParam);
                         point.y = HIWORD(lParam);
                         ClientToScreen(hWnd, &point);
                         target = WindowFromPoint(point);
                         // SOME ACTION HERE WITH target


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
When I try to put this in, i get a case error...don't i need to switch anything?
Jaime OlivaresSoftware ArchitectCommented:
Of course you need a switch, is just a portion of your default window procedure.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.