Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals


compiler can find out and position it accordingly. For this reason, you can omit the
pAlternateOwner argument.

You should call the CenterWindow() method in the event that creates the window. For a
frame this would be the OnCreate event. Here is an example:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//...

CenterWindow();

return 0;
}

If you want the caller to be centrally positioned relative to another window, obtain a
handle to that window and pass it as the pAlternateOwner argument.

Once a window is displaying on the screen, the user can change its location by dragging
its title bar. This would change its CREATESTRUCT::x or its rect::left argument. If you
do not want the user to change the location of a window, one thing you can do is to
prevent the mouse from capturing, that is, taking ownership, of the title bar. This can be
done calling the Win32's ReleaseCapture() function. Its syntax is:

BOOL ReleaseCapture(VOID);

When this function is called, the event in which it is accessed prevents the mouse from
capturing the object on which the mouse is positioned. Nevertheless, if the function
succeeds, it returns TRUE. If for some reason it fails, it returns FALSE. Because a
WM_MOVE message is sent when a window is moved, you can use it to call this
function. Here is an example:

void CMainFrame::OnMove(int x, int y)
{
CFrameWnd::OnMove(x, y);

ReleaseCapture();
// TODO: Add your message handler code here
}

To change the dimensions of a window, the user can click and drag one of its borders.
Imagine you do not want the user to change the size of the window. If you remove the
system buttons and/or the system menu(

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style &= ~WS_SYSMENU;

return TRUE;
}
Free download pdf