Thread: moving a window

  1. #1
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    moving a window

    This turned out to be way more difficult than I thought. I just want to be able to move a window by dragging it from any point within the window. The obvious problem is that I don't know where the mouse is when it leaves the window. I was thinking that I could make an invisible window that spans the entire screen and use its WM_MOUSEMOVE to determine the mouse position, but that seems a little stupid. Maybe I'm going about this the wrong way.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Try this in your wndproc:

    Code:
          if(uMsg == WM_LBUTTONDOWN)
          {
             SendMessage(hWnd, WM_SYSCOMMAND, 0xF012, 0);
             break;
          }
    hope that helps!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    What you are looking for is SetCapture.
    Code:
    case WM_LBUTTONDOWN:
         SetCapture(hWnd);
         break;
    
    case WM_MOUSEMOVE:
         //Appropriate SetWindowPos calls
         break;
    
    case WM_LBUTTONUP:
         ReleaseCapture();
         break;
    SetCapture makes it so YOUR WINDOW handles all WM_MOUSEMOVE messages for the time it has the capture set.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    If you are using MFC, it's quite simple really.

    Override the OnNcHitTest() function of your dialog (if that's what you're writing) and just have it return HTCAPTION. That tells windows that the mouse is on the caption bar of the window no matter where it actually is over the dialog, so it moves the entire dialog when it's dragged.

    Code:
    UINT CSkinTestDlg::OnNcHitTest(CPoint point) 
    {
    	return HTCAPTION;//always say our mouse is on the caption (title bar) area
    }

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Hershlag, using setcapture is really unnecessary. And if you're not using MFC, then Lucky's suggestion wont work for you.

    using the small piece of code i posted will work for you with anything. Just do

    SendMessage(hWnd, WM_SYSCOMMAND, 0xF012, 0);

    in your mouse click even handler. It's as simple as that!

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well Lucky i'm not using MFC (as I am using mingw and can't) but I really don't see how that could possibly be easier than Uraldor's solution. Hershlag's approach is good for how I was doing things. But my way was a bit messy and it was error prone. Uraldor's way is perfect.

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Well to be clear, master5001, I didn't say it was easier than Uraldor's solution.
    Perhaps there are other MFC programmers who are looking for an alternate way to accomplish the task.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Uraldor,
    I looked in winuser.h for the SC_ command defined as 0xF012 (as I would rather use the define than a value, should the define change) but could not find one.
    I tried a search but could not find the define.

    Could you point me to the #define for this command?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Nova,

    I wish I could mate, but there is no #define for it. The logic behind it is that:

    SC_MOVE + HTCAPTION = 0xF012 = 61458

    I think it's just another one of MS's undocumented features.

    That's the best I can do I'm afraid!

    Cheers
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    the way it probably should be done then is this
    Code:
    SendMessage(hWnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, 0);
    just to be more readable. I was not familiar with this one but I suspect that they do the SetCapture stuff internally. Pretty slick though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM