Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls


If you create a certain type of window and you do not want the user to move it around,
you can write code for the WM_MOVING message. In the following example, the user
cannot move the window as its location and dimensions are restored with any attempt to
move it (if you want to write the OnMoving event for a dialog box in MSVC 6, you may
have to manually declare and define the event as follows):

class CTabDlg : public CDialog
{
// Construction
public:
CTabDlg(CWnd* pParent = NULL); // standard constructor

...


// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CTabDlg)
virtual BOOL OnInitDialog();
afx_msg void OnMoving(UINT nSide, LPRECT lpRect);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

...


BEGIN_MESSAGE_MAP(CTabDlg, CDialog)
//{{AFX_MSG_MAP(CTabDlg)
ON_WM_MOVING()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

...
void CTabDlg::OnMoving(UINT nSide, LPRECT lpRect)
{
CRect CurRect;


// Find out the location and the dimensions of the window
GetWindowRect(&CurRect);

// You ain't moving nothin'
lpRect->left = CurRect.left;
lpRect->top = CurRect.top;
lpRect->right = CurRect.right;
lpRect->bottom = CurRect.bottom;
}

To programmatically move a window, call the CWnd::SetWindowPos() method. Its
syntax is:

BOOL SetWindowPos(const CWnd* pWndInsertAfter,
int x, int y, int cx, int cy, UINT nFlags);

The pWndInsertAfter argument is used to specify the window that will positioned in the Z
coordinate on top of the window that called this method. If you have the class name or
the CWnd name of the other window, pass it as the pWndInsertAfter argument.
Otherwise, this argument can have one of the following values:
Free download pdf