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

More Menu Fun

We've added a few of the items from the system menu. Let's go ahead and add the rest: Restore, Size, Minimize, and Maximize. First, add menu options for the four items. Second, in the ClassWizard, add member functions to handle each option. Third, post the appropriate WM_SYSCOMMAND event for each function (here's a hint: SC_RESTORE, SC_SIZE, SC_MINIMIZE, SC_MAXIMIZE). Run the program. Try out the menu items.

Notice that all of the options are always enabled. When you are maximized, you can still select maximize even though it doesn't do anything. We want to disable some of the menu items. Use the UPDATE_COMMAND_UI message map to do this. Create an UPDATE_COMMAND_UI handler for Move, Maximize, Minimize, Restore and Size. These functions pass in a CCmdUI object. Call Enable() to enable or disable the menu option for that function (you can also use CCmdUI to set checks and text).

We want to disable Move and Size when the application is not maximized:


pCmdUI->Enable(!IsIconic()&&!IsZoomed());

We want to disable Maximize when the application is maximized:


pCmdUI->Enable(!IsZoomed());

We want to disable Minimize when the application is minimized:


pCmdUI->Enable(!IsIconic());

We want to disable Restore when the application is not maximized or minimized:


pCmdUI->Enable(IsIconic()||IsZoomed());

Now when we use the menu, the options should be enabled appropriately.

We have one more thing to do. Notice that you can still move the window when the application is maximized. We want to disable this. Modify OnMouseMove to look like the following:

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

   CFrameWnd::OnMouseMove(nFlags, point);
}

This will prevent us from moving the window manually while it is maximized. Following is the code added for this section:


void CMainFrame::OnPopuptopRestore()
{
   PostMessage(WM_SYSCOMMAND, SC_RESTORE);
}

void CMainFrame::OnPopuptopSize()
{
   PostMessage(WM_SYSCOMMAND, SC_SIZE);
}

void CMainFrame::OnPopuptopMinimize()
{
   PostMessage(WM_SYSCOMMAND, SC_MINIMIZE);
}

void CMainFrame::OnPopuptopMaximize()
{
   PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE);
}

void CMainFrame::OnUpdatePopupMove(CCmdUI* pCmdUI)
{
   pCmdUI->Enable(!IsIconic()&&!IsZoomed());
}

void CMainFrame::OnUpdatePopuptopMaximize(CCmdUI* pCmdUI)
{
   pCmdUI->Enable(!IsZoomed());
}

void CMainFrame::OnUpdatePopuptopMinimize(CCmdUI* pCmdUI)
{
   pCmdUI->Enable(!IsIconic());
}

void CMainFrame::OnUpdatePopuptopRestore(CCmdUI* pCmdUI)
{
   pCmdUI->Enable(IsIconic()||IsZoomed());
}

void CMainFrame::OnUpdatePopuptopSize(CCmdUI* pCmdUI)
{
   pCmdUI->Enable(!IsIconic()&&!IsZoomed());
}

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



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