HOME
Login
Change Info
Logout


TUTORIALS

C, C++
Win32
Java
Visual Basic
MFC
DCOM
Networking
C#
Perl
HTML
XML
ASP
PHP
Javascript
Other

DOWNLOADS
ITCLib
SourceVizor meets the notification, reporting, and admin needs of teams using Microsoft Visual SourceSafe.
Free 30-day trial!


Creating a Simple MFC Program

by Alan Oursland

Moving the Window

Windows generally lets you move a window by dragging it around by the title bar. We don't have a title bar, so we need another way to move the window. We could just add a "Move" item to our popup menu and post a WM_ENTERSIZEMOVE event in the same way we posted WM_CLOSE, but I think I'd like the user to be able to drag the window around the screen.

The first part is easy. Add another menu item called "Move" to our popup menu under the "Close" menu item. Create a function to handle it called OnPopupMove. Edit OnPopupMove to look like this:


void CMainFrame::OnPopupMove()
{
   PostMessage(WM_SYSCOMMAND, SC_MOVE);
}

You can use WM_SYSCOMMAND to trigger events from the system menu. In fact, why don't we change OnPopupClose to use the WM_SYSCOMMAND as well. Change "PostMessage(WM_CLOSE)" to "PostMessage(WM_SYSCOMMAND, SC_CLOSE)". There isn't any change in functionality, but it helps keep things consistent.

Notice that when we select Move, initially we can move the window with the arrow keys, but not with the mouse. The same thing would happen if we tried to post an SC_MOVE event on mouse down. Windows doesn't provide us access to the code to move things, so we will have to write it ourselves. We want to start moving on a left mouse button down, move on a mouse move, and stop moving on a left mouse button up. First, add functions to handle ON_WM_LBUTTONDOWN, ON_WM_MOUSEMOVE, and ON_WM_LBUTTONUP.

To move the window, we first need to know when the mouse was pressed inside of it. Add the following member variable to CMainFrame:


BOOL m_bMouseDown;

Initialize it to FALSE in the constructor, set it to TRUE in OnLButtonDown, and set it to FALSE in OnLButtonUp. Add an if clause on it in OnMouseMove. This isn't good enough to tell when the mouse is pressed. It is possible for the button to be pressed while the cursor is inside of the window and released while the cursor is outside of the window. In that case we would never receive the mouse release and would continue moving the window. We need to capture the mouse so that we always get mouse events - even if the mouse is not in the window.

Add "SetCapture()" to OnLButtonDown and "ReleaseCapture()" to OnLButtonUp. During OnMouseMove, we want to move the window. We will need to know where in the window the mouse was pressed, and move the window by the distance between it and the current position. Add The following member variable to CMainFrame:


CPoint m_mouseDownPoint;

Set the variable to the point parameter in OnLButtonDown. In OnMouseMove, check that the mouse is down. Then find the size difference between the "mouse down point" and the new point. Get the window rectangle. Subtract the rectangle from the size and move the window to the new position.

That's it. We now have a window that we can drag around with the mouse or move via the menu using the arrow keys. The final code for OnLButtonDown, OnMouseMove, and OnLButtonUp follows:


void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_bMouseDown = TRUE;
    SetCapture();
    m_mouseDownPoint = point;

    CFrameWnd::OnLButtonDown(nFlags, point);
}

void CMainFrame::OnMouseMove(UINT nFlags, CPoint point)
{
    if( m_bMouseDown )
    {
        CSize s = m_mouseDownPoint - point;
        CRect r;
        GetWindowRect(&r);
        r = s - &r;
        MoveWindow(r);
    }

    CFrameWnd::OnMouseMove(nFlags, point);
}

void CMainFrame::OnLButtonUp(UINT nFlags, CPoint point)
{
    m_bMouseDown = FALSE;
    ReleaseCapture();

    CFrameWnd::OnLButtonUp(nFlags, point);
}

Previous Page
Return to beginning of article

Next Page


Featured Article

An Introduction to C#
By Joey Mingrone

Register Today!


100% FREE

Members enjoy these benefits:
Access to ITI Downloads
Access to more articles and tutorials
Optional weekly newsletter
And more...

Click here to register
Or
Click here to log in



© 2010 Interface Technologies, Inc. All Rights Reserved
Questions or Comments? devcentral AT iticentral DOT com
PRIVACY POLICY