Menu won't check-uncheck! 
Author Message
 Menu won't check-uncheck!

This is a piece of code that I did long time ago which allows toolbars
and status bars for dialog bassed applications.

Ken

##################begin###################

//#define MYTITLE       {dlgwnd.cpp}
/******************************************************************************
   MODULE               :       dlgwnd.cpp
   Version              :       2.0
   Description  :       The base class for the main window.  This class is
                                        derived from CDialog and is intended to be used as
                                        a main window in an application using a dialog
                                        as it's main window.
******************************************************************************/

#include "stdafx.h"
#include "dlgwnd.h"

#ifdef _DEBUG
        #undef THIS_FILE
        static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

#define new DEBUG_NEW

/////////////////////////////////////////////////////////////////////////////
// Message Map -- CDialogWinApp

BEGIN_MESSAGE_MAP(CDialogWinApp, CDialog)
        //{{AFX_MSG_MAP(CDialogWinApp)
#if defined (_STATBAR) || defined (_ALLOPTIONS)
        ON_COMMAND(ID_VIEW_STATUS_BAR, OnViewStatusBar)
        ON_UPDATE_COMMAND_UI(ID_VIEW_STATUS_BAR, OnUpdateViewStatusBar)

        ON_WM_ENTERIDLE()
        ON_MESSAGE(WM_SETMESSAGESTRING, OnSetMessageString)
        ON_WM_MENUSELECT()

        // turning on and off standard mode indicators
        ON_UPDATE_COMMAND_UI(ID_INDICATOR_CAPS, OnUpdateKeyIndicator)
        ON_UPDATE_COMMAND_UI(ID_INDICATOR_NUM, OnUpdateKeyIndicator)
        ON_UPDATE_COMMAND_UI(ID_INDICATOR_SCRL, OnUpdateKeyIndicator)
#endif  //_STATBAR
#if defined (_TOOLBAR) || defined (_ALLOPTIONS)
        ON_COMMAND(ID_VIEW_TOOLBAR, OnViewToolbar)
        ON_UPDATE_COMMAND_UI(ID_VIEW_TOOLBAR, OnUpdateViewToolbar)
#endif  //_TOOLBAR
#if defined (_CTL3D) || defined (_ALLOPTIONS)
        ON_WM_SYSCOLORCHANGE()
#endif  //_CTL3D
        ON_WM_INITMENUPOPUP()
#if defined (_HELP) || defined (_ALLOPTIONS)
        ON_MESSAGE(WM_COMMANDHELP, OnCommandHelp)
        ON_WM_DESTROY()
#endif //_HELP
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::CDialogWinApp
//      This is the constructor for the CDialogWinApp class.  

CDialogWinApp::CDialogWinApp()
{
#if defined (_STATBAR) || defined (_ALLOPTIONS)
        m_nIDTracking    = 0;
        m_nIDLastMessage = 0;

        m_lpaIDStatusBar = NULL;
        m_cIDStatusBar   = 0;
#endif  //_STATBAR
#if defined (_TOOLBAR) || defined (_ALLOPTIONS)
        m_lpaIDToolBar   = NULL;
        m_cIDToolBar     = 0;
        m_nIDBitmap      = 0;
#endif  //_TOOLBAR

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::Create
//      Create actually creates the modeless dialog.
#if defined (_STATBAR) || defined (_ALLOPTIONS)
//      Statusbar information is saved for processesing during
CDialogWinApp::OnInitDialog.
#endif  //_STATBAR
#if defined (_TOOLBAR) || defined (_ALLOPTIONS)
//      Toolbar information is saved for processesing during
CDialogWinApp::OnInitDialog.
#endif  //_TOOLBAR

BOOL CDialogWinApp::Create(UINT nIDTemplate
#if defined (_STATBAR) || defined (_ALLOPTIONS)
        , UINT FAR *lpaIDStatus, int cIDStatus
#endif  //_STATBAR
#if defined (_TOOLBAR) || defined (_ALLOPTIONS)
        , UINT FAR *lpaIDToolbar, int cIDToolbar, UINT nIDBitmap
#endif  //_TOOLBAR
        , CWnd *pParent)
{
#if defined (_STATBAR) || defined (_ALLOPTIONS)
        m_lpaIDStatusBar = lpaIDStatus;
        m_cIDStatusBar   = cIDStatus;
#endif  //_STATBAR
#if defined (_TOOLBAR) || defined (_ALLOPTIONS)
        m_lpaIDToolBar   = lpaIDToolbar;
        m_cIDToolBar     = cIDToolbar;
        m_nIDBitmap      = nIDBitmap;
#endif  //_TOOLBAR
        return CDialog::Create(nIDTemplate, pParent);

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnOK
//      Overload this function so the main window gets destroyed when the
//      user presses <ENTER>

void CDialogWinApp::OnOK()
{
        CDialog::OnOK();                // Call baseclass so usercode gets called properly
        DestroyWindow();
        PostQuitMessage(0);

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnCancel
//      Overload this function so the main window gets destroyed when the
//      user presses <ESCAPE>

void CDialogWinApp::OnCancel()
{
        CDialog::OnCancel();    // Call baseclass so usercode gets called properly
        DestroyWindow();
        PostQuitMessage(0);

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::PostNcDestroy

void CDialogWinApp::PostNcDestroy()
{
        delete this;

Quote:
}

#if defined (_HELP) || defined (_ALLOPTIONS)

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp support for context sensitive help.  This code is based
//  on CFrameWnd::IsTracking and CFrameWnd::OnCommandHelp.

BOOL CDialogWinApp::IsTracking() const
{
        return m_nIDTracking != 0 &&
                m_nIDTracking != AFX_IDS_HELPMODEMESSAGE &&
                m_nIDTracking != AFX_IDS_IDLEMESSAGE;

Quote:
}

LRESULT CDialogWinApp::OnCommandHelp(WPARAM, LPARAM lParam)
{
        if (lParam == 0)
        {
                if (IsTracking())
                        lParam = HID_BASE_COMMAND + m_nIDTracking;
                else
                        lParam = HID_BASE_RESOURCE + m_nIDHelp;
        }

        if (lParam != 0)
        {
                AfxGetApp()->WinHelp(lParam);
                return TRUE;
        }
        return FALSE;

Quote:
}

void CDialogWinApp::OnDestroy()
{
        // If the main window is closing, tell WinHelp to close as well
        if (AfxGetApp()->m_pMainWnd == this)
                ::WinHelp(m_hWnd, NULL, HELP_QUIT, 0L);

        CDialog::OnDestroy();

Quote:
}

#endif //_HELP

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnInitDialog
//      OnInitDialog centers the dialog on the screen
#if defined (_STATBAR) || defined (_ALLOPTIONS)
//      Creates the status bar
#endif  //_STATBAR
#if defined (_TOOLBAR) || defined (_ALLOPTIONS)
//      Creates the tool bar
#endif  //_TOOLBAR

BOOL CDialogWinApp::OnInitDialog()
{
        CDialog::OnInitDialog();
#if defined (_CTL3D) || defined (_ALLOPTIONS)
    Ctl3dSubclassDlgEx(m_hWnd, CTL3D_ALL);      // 3D controls for the main
window
#endif

        CenterWindow();

#if defined (_STATBAR) || defined (_ALLOPTIONS)
        // Create status bar at bottom of window
        if (m_wndStatusBar.Create(this))
        {
                RepositionBars(0, 0xFFFF, 0);
                m_wndStatusBar.SetIndicators(m_lpaIDStatusBar, m_cIDStatusBar);
                OnSetMessageString(AFX_IDS_IDLEMESSAGE);
        }
#endif  //_STATBAR

#if defined (_TOOLBAR) || defined (_ALLOPTIONS)
        // Create toolbar at top of window
        if (m_wndToolBar.Create(this) &&
                m_wndToolBar.LoadBitmap(m_nIDBitmap) &&
                m_wndToolBar.SetButtons(m_lpaIDToolBar, m_cIDToolBar))
        {
                RepositionBars(0, 0xFFFF, 0);
        }

#endif
        return TRUE;

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnInitMenuPopup
//      OnInitMenuPopup updates the state of items on a popup menu.  This
//      code is based on CFrameWnd::OnInitMenuPopup.

void CDialogWinApp::OnInitMenuPopup(CMenu *pMenu, UINT, BOOL bSysMenu)
{
        if (!bSysMenu)
        {
                ASSERT(pMenu != NULL);

                // check the enabled state of various menu items
                CCmdUI state;
                state.m_pMenu = pMenu;
                ASSERT(state.m_pOther == NULL);

                state.m_nIndexMax = pMenu->GetMenuItemCount();

                for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
                        state.m_nIndex++)
                {
                        state.m_nID = pMenu->GetMenuItemID(state.m_nIndex);
                        if (state.m_nID == 0)
                                continue; // menu separator or invalid cmd - ignore it

                        ASSERT(state.m_pOther == NULL);
                        ASSERT(state.m_pMenu != NULL);

                        if (state.m_nID == (UINT)-1)
                        {
                                // possibly a popup menu, route to first item of that popup
                                state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex);

                                if (state.m_pSubMenu == NULL ||
                                        (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
                                        state.m_nID == (UINT)-1)
                                {
                               continue;       // first item of popup can't be routed
to
                                }
                                state.DoUpdate(this, FALSE);    // popups are never auto disabled
                }
                        else
                        {
                                // normal menu item
                                // Auto enable/disable if command is _not_ a system command.
                                state.m_pSubMenu = NULL;
                                state.DoUpdate(this, state.m_nID < 0xF000);
                        }
                }
        }

Quote:
}

#if defined (_STATBAR) || defined (_ALLOPTIONS)
/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnViewStatusBar
//      OnViewStatusBar toggles the state of the status bar when the
//      corresponding View option is selected from the menu.  

void CDialogWinApp::OnViewStatusBar()
{
        m_wndStatusBar.ShowWindow(m_wndStatusBar.IsWindowVisible() ? SW_HIDE :
SW_SHOWNA);

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnUpdateViewStatusBar
//      OnUpdateViewStatusBar checks or unchecks the View Status Bar option
//      on the dialog's menu, depending on whether or not the status bar
//      is visible.

void CDialogWinApp::OnUpdateViewStatusBar(CCmdUI* pCmdUI)
{
        ASSERT(pCmdUI->m_nID == ID_VIEW_STATUS_BAR);

        pCmdUI->SetCheck(m_wndStatusBar.IsWindowVisible());

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnEnterIdle
//      OnEnterIdle updates the status bar when there is nothing better
//      to do.  This code is based on CFrameWnd::OnEnterIdle.

void CDialogWinApp::OnEnterIdle(UINT nWhy, CWnd * /*pWho*/)
{
        if (nWhy != MSGF_MENU || m_nIDTracking == m_nIDLastMessage)
                return;

        OnSetMessageString(m_nIDTracking);
        ASSERT(m_nIDTracking == m_nIDLastMessage);

Quote:
}

/////////////////////////////////////////////////////////////////////////////
// CDialogWinApp::OnMenuSelect
//      OnMenuSelect updates the status bar message, based on the state
//      of the dialog menu.  This code is based on CFrameWnd::OnMenuSelect.

void CDialogWinApp::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU
/*hSysMenu*/)
{
        // set the tracking state
        if (nFlags == 0xFFFF)
        {
                // cancel menu operation (go back to idle now)
                m_nIDTracking = AFX_IDS_IDLEMESSAGE;
                OnSetMessageString(m_nIDTracking);   // set string now
                ASSERT(m_nIDTracking == m_nIDLastMessage);
        }
        else if (nItemID == 0 || nFlags &
(MF_SEPARATOR|MF_POPUP|MF_MENUBREAK|MF_MENUBARBREAK))
        {
                // nothing should be displayed
                m_nIDTracking = 0;
        }
        else if (nItemID >= 0xF000 && nItemID < 0xF1F0)
        {
                //
...

read more »



Tue, 13 Jul 1999 03:00:00 GMT  
 Menu won't check-uncheck!

Quote:

> When I click on the item, the code I've
> attached is executed - but the checkmark never changes.

I'm doing something like what you want with an MDI application, but my
code looks like:

        CMDIChildWnd* mdiChild= (CMDIChildWnd*) GetParent();
        CMDIFrameWnd* mdiFrame= mdiChild->GetMDIFrame();
        CMenu* menu= mdiFrame->GetMenu();
        isChecked= isChecked? FALSE: TRUE;
        menu->CheckMenuItem(IDM_TC_DROP_FRAME, isChecked);

(I've left out all the checks for NULL pointers). IDM_TC_DROP_FRAME
is the dialog ID for the menu item which gets checked. The code should
be simpler for SDI applications.

Hope this helps.



Tue, 13 Jul 1999 03:00:00 GMT  
 Menu won't check-uncheck!

Quote:



>>When I click on the item, the code I've
>>attached is executed - but the checkmark never changes.

>>Could someone please tell me what's wrong with the code, or point me
>>in a direction to help me fix the menu checkmarks?

>>====
>>// global variables
>>int report=0, autoexit=1;

>>void CClearDlg::OnUpdateMenuReport(CCmdUI* pCmdUI)
>>{

**************************************************************
Quote:
>>        report=(report? 0:1);   // works ok
>>        pCmdUI->SetCheck(report);   // seems to do nothing!

**************************************************************
// replace with the following:

          pCmdUI->SetCheck(report == 0); // don't need to change report.

Quote:
>>}

>>void CClearDlg::OnMenuReport()
>>{
>>        if(report) { (autoexit=0); }   // works ok
>>}
>>===
>>The variables are updated accordingly whenever I click the item, but
>>the checkmark never goes away!

>Aside from the already-reported problem of CmdUI handlers in dialog
>applications, you shouldn't be changing the state of any variables in
>the OnUpdate* method, since it's called when the menu drops down
>and again when the menu goes away again. So the value of report is
>getting changed back to its old value whenever you drop down the
>menu.

>It's easy to watch this happening by putting a TRACE statement in your
>code.

*********************************************************************
Quote:
>To fix it, move the toggle of the report state to the OnMenuReport
>method, so it only gets changed after the user actually selects the
>item.

*********************************************************************

--------------------------------------------------------------------------------


XIOtech Corporation               Personal Home Page:   http://www.visi.com/~nde
612-828-5961

"Question authority...and the authorities will end up questioning YOU!"
--------------------------------------------------------------------------------



Fri, 16 Jul 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Menu won't check-uncheck!

2. menu - checking & unchecking a menu item

3. check\uncheck menu items

4. How to check/uncheck menu items?

5. Menu Check/Uncheck

6. How to CHECKED<->UNCHECKED Popup Menu

7. check/uncheck menu entries

8. Check box in dialog won't stay checked

9. Getting notifications of check/uncheck in a tree control

10. CDialog bar button enabling/disabling checking/unchecking

11. TreeItem checked/unchecked

12. Determine checked/unchecked for TBBS_CHECKBOX Button in CToolBar

 

 
Powered by phpBB® Forum Software