Capture mouse coords

This is a discussion on Capture mouse coords within the Windows Programming forums, part of the Platform Specific Boards category; How can I get the coords of the mouse when a control is clicked? I want to be able to ...

  1. #1
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293

    Capture mouse coords

    How can I get the coords of the mouse when a control is clicked? I want to be able to drag one of my custom controls around however WM_LBUTTONDOWN never seems to do anything if I use it in the control callback, it does in the main window callback though..

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Handle the WM_NCHITTEST message for your custom control and return HTCAPTION.

    or

    Handle the WM_LBUTTONDOWN and
    Code:
    SendMessage(hwnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
    which is more or less the same thing.

    Either of these methods is better (smoother movement) than using something like SetWindowPos or MoveWindow to reposition the control.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Ok, I have it moveable. But now I have another problem.

    If the mouse is clicked within the left and right sides and within 25 from the top (ie: the title bar!) it will be moveable when you drag until you let go, just like a normal app window.

    Now I'm using GetCursorPos() to get the coords (obviously ) on WM_MOUSEMOVE so that it'll move roughly the right direction. My problem is that it has an offset to the actual mouse position and I'm not sure how to make it correct.

    http://mhtml.ithium.net/offsite/cursorMove.jpg - The blue dot is where my curosr would be about when draging, as you can see the tool window has a large offset on the x and y.
    Code:
    case WM_LBUTTONDOWN:
      {
        GetWindowRect(hwnd,&rc);
        if(pos.x < rc.right && pos.x > rc.left && pos.y < rc.top+25 && pos.y > rc.top)
        {
            fDragMode = true;
            SetCapture(hwnd);
            return 0;
        }
        break;
      }break;
      case WM_MOUSEMOVE:
      {
        if(fDragMode == false) break;
        GetCursorPos(&pos);
        MoveWindow(hwnd,pos.x,pos.y,rc.right-rc.left,rc.bottom-rc.top,TRUE);
        UpdateWindow(hwnd);
        return 0;
      }
      case WM_LBUTTONUP:
      {
        if(fDragMode == false) break;
        fDragMode = false;   
        ReleaseCapture();
        return 0;
      }
    fDragMode, rc and pos are declared earlier as bool, RECT and POINT respectively.

    Cheers,
    Michael.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    GetCursorPos returns the mouse position in screen coordinates. Use ScreenToClient to convert to client coordinates.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    [edit]: Ok, it works now that I replaced the ScreenToClient(hwnd,&pos) to ScreenToClient(GetParent(hwnd),&pos) ... but the mouse is getting snapped to the 0,0 on the control. The control does move and redraw nicely now however.
    Last edited by Mithoric; 12-17-2003 at 10:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 08:07 AM
  2. mouse click coords and moving a rect
    By techrolla in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2004, 09:49 PM
  3. Direct Input and Mouse Coords
    By Khelder in forum Game Programming
    Replies: 5
    Last Post: 12-30-2003, 02:29 PM
  4. getting mouse coords?
    By elfjuice in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2003, 09:03 PM
  5. Middle button mouse capture in another program
    By Gayak in forum Windows Programming
    Replies: 5
    Last Post: 06-13-2002, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21