Move a window in MFC - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Move a window in MFC

CocaColaCocaCola Posts: 48Member
I want to have a window that has no title bar so you can't move it around. I've tried this but something is wrong because it disappears when using it:

[blue]void[/blue] CMoveagainDlg::OnMouseMove(UINT nFlags, CPoint point)
{
if((nFlags & MK_LBUTTON) == MK_LBUTTON)
{
cpPos.x = point.x;
cpPos.y = point.y;

MoveWindow(cpPos.x,cpPos.y,0,0,TRUE);
}

CDialog::OnMouseMove(nFlags, point);
}

Do I have to handle a WM_LBUTTONDON message to? if so, for what!



[b][size=3][red]CocaCola[/red][/size][/b]
[italic]ICQ:[/italic] [blue]50302279[/blue]
[italic]E-mail:[/italic] [email protected]

Comments

  • Eric TetzEric Tetz Posts: 2,141Member
    : I want to have a window that has no title bar so you can't move it around. I've tried this but something is wrong because it disappears when using it:
    :
    : [blue]void[/blue] CMoveagainDlg::OnMouseMove(UINT nFlags, CPoint point)
    : {
    : if((nFlags & MK_LBUTTON) == MK_LBUTTON)
    : {
    : cpPos.x = point.x;
    : cpPos.y = point.y;
    :
    : MoveWindow(cpPos.x,cpPos.y,0,0,TRUE);
    : }
    :
    : CDialog::OnMouseMove(nFlags, point);
    : }
    :
    : Do I have to handle a WM_LBUTTONDON message to? if so, for what!

    I wrote a little routine for that a long time ago...

    [code] //------------------------------------------------------------------------------
    // HandleClientAreaDragMsg() - Eric Tetz 7/13/99
    //
    // If you pass WM_LBUTTONDOWN, WM_LBUTTONUP, and WM_MOUSEMOVE messages to this
    // function, it will enable "client area drag" for the window.
    //------------------------------------------------------------------------------
    static void HandleClientAreaDragMsg (HWND hwnd, UINT msg, int mouseX, int mouseY)
    {
    static int captureX = 0;
    static int captureY = 0;

    switch (msg)
    {
    case WM_LBUTTONDOWN:
    captureX = mouseX;
    captureY = mouseY;
    SetCapture (hwnd);
    break;

    case WM_LBUTTONUP:
    ReleaseCapture();
    break;

    case WM_MOUSEMOVE:
    if (GetCapture() == hwnd)
    {
    RECT rc;
    GetWindowRect (hwnd, &rc);
    int newX = rc.left + mouseX - captureX;
    int newY = rc.top + mouseY - captureY;
    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;
    UINT flags = SWP_NOZORDER | SWP_NOACTIVATE;
    SetWindowPos (hwnd, NULL, newX, newY, width, height, flags);
    }
    break;
    }
    }[/code]It's written using the raw API, but you can easily use it in your MFC app like this:

    [code] void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point)
    {
    HandleClientAreaDrag(m_hWnd, WM_LBUTTONDOWN, point.x, point.y);
    CDialog::OnLButtonDown(nFlags, point);
    }

    void CMoveagainDlg::OnLButtonUp(UINT nFlags, CPoint point)
    {
    HandleClientAreaDrag(m_hWnd, WM_LBUTTONUP, point.x, point.y);
    CDialog::OnLButtonUp(nFlags, point);
    }

    void CMoveagainDlg::OnMouseMove(UINT nFlags, CPoint point)
    {
    HandleClientAreaDrag(m_hWnd, WM_MOUSEMOVE, point.x, point.y);
    CDialog::OnMouseMove(nFlags, point);
    }[/code]Cheers,
    Eric

  • Sephiroth2Sephiroth2 Posts: 423Member
    If you just want to move the window using the mouse, this is all you have to do:
    if(msg==WM_LBUTTONDOWN) {
    DefWindowProc(hWnd,SC_SYSCOMMAND,SC_MOVE+1,lparam);
    }

    Using MFC, it would be something like this...
    void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point)
    {
    DefWindowProc(m_hWnd, SC_SYSCOMMAND, SC_MOVE+1,MAKELPARAM(point.x,point.y));
    }

  • Eric TetzEric Tetz Posts: 2,141Member
    : If you just want to move the window using the mouse, this is all you have to do:
    : if(msg==WM_LBUTTONDOWN) {
    : DefWindowProc(hWnd,SC_SYSCOMMAND,SC_MOVE+1,lparam);
    : }
    :
    : Using MFC, it would be something like this...
    : void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point)
    : {
    : DefWindowProc(m_hWnd, SC_SYSCOMMAND, SC_MOVE+1,MAKELPARAM(point.x,point.y));
    : }

    Ahhh!! Cool! Much easier than my method. For CocaCola's sake, I should point out a typo: it's WM_SYSCOMMAND, not SC_SYSCOMMAND.

    Anyway, I can still see a situation where my approach would be useful... ;) Newer version of Windows have a "show window contents while dragging" option. If you want your window to show it's contents while dragging regardless of the Windows version and/or the setting of that option, you can use my EnableClientAreaDrag routine.

    This can be really helpful if you have non-rectangular window. It looks cheesy if you have a window shaped like a pear, but when you drag it you see a rectangle - it kinda spoils the illusion.

    Cheers,
    Eric


  • Sephiroth2Sephiroth2 Posts: 423Member
    : : If you just want to move the window using the mouse, this is all you have to do:
    : : if(msg==WM_LBUTTONDOWN) {
    : : DefWindowProc(hWnd,SC_SYSCOMMAND,SC_MOVE+1,lparam);
    : : }
    : :
    : : Using MFC, it would be something like this...
    : : void CMoveagainDlg::OnLButtonDown(UINT nFlags, CPoint point)
    : : {
    : : DefWindowProc(m_hWnd, SC_SYSCOMMAND, SC_MOVE+1,MAKELPARAM(point.x,point.y));
    : : }
    :
    : Ahhh!! Cool! Much easier than my method. For CocaCola's sake, I should point out a typo: it's WM_SYSCOMMAND, not SC_SYSCOMMAND.
    :
    : Anyway, I can still see a situation where my approach would be useful... ;) Newer version of Windows have a "show window contents while dragging" option. If you want your window to show it's contents while dragging regardless of the Windows version and/or the setting of that option, you can use my EnableClientAreaDrag routine.
    :
    : This can be really helpful if you have non-rectangular window. It looks cheesy if you have a window shaped like a pear, but when you drag it you see a rectangle - it kinda spoils the illusion.
    :
    : Cheers,
    : Eric
    :
    :
    Right.
    I gotta stop typing these things at 1AM... =)

Sign In or Register to comment.