Get HWND from a mouse release

Posted on 2004-11-28
Last Modified: 2008-02-01

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?

Question by:Mad_Angel
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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,
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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:

    Author Comment

    Too there a code example?

    LVL 55

    Accepted Solution

    >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


    Expert Comment

    When I try to put this in, i get a case error...don't i need to switch anything?
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Of course you need a switch, is just a portion of your default window procedure.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now