CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> Windows & Dialogs >> Dialog >> CommandUI


Using ON_UPDATE_COMMAND_UI with menu items and controls (2)
Rating: none

John Wismar (view profile)
November 11, 1998


(continued)



-->

 If you have a dialog-based application, to which you have added a menu, any OnUpdate handlers that you included in the dialog's message map will never be called.  (Note that the corresponding OnCommand handlers work fine).

By handling the dialog's WM_KICKIDLE message, and mimicking MFC's routing of its UPDATE_UI messages, you can implement this with a little work.

The first step is to handle WM_KICKIDLE, which is #defined in afxpriv.h, by declaring OnKickIdle(), and adding it to the message map.

//[in TESTDLG.H]
class CTestDlg : public CDialog
{
 . . .
 // Generated message map functions
 //{{AFX_MSG(CTestDlg)
 . . .
 //}}AFX_MSG
 afx_msg LRESULT OnKickIdle(WPARAM, LPARAM);
 DECLARE_MESSAGE_MAP()
 . . .
};
#include "stdafx.h"
#include "TestDlg.h"
#include <afxpriv.h>
. . . 
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
 //{{AFX_MSG_MAP(CTestDlg)
 . . .
 //}}AFX_MSG_MAP
 ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
END_MESSAGE_MAP()

The real work comes in defining OnKickIdle(). You need to iterate over each item that might have an OnUpdate handler, set up a CCmdUI instance for that item, and call CCmdUI::DoUpdate().  The OnKickIdle() I have written will update the menu items in each of the first-level dropdown menus (e.g. the "File" menu, the "Edit" menu, etc. from a normal app).  You will need to modify it if you have further levels of submenus.  You will need a similar loop if you want to have OnUpdate handlers for a status bar, toolbar, or specific dialog controls.

LRESULT CTestDlg::OnKickIdle(WPARAM, LPARAM)
{
 CMenu* pMainMenu = GetMenu();
 CCmdUI cmdUI;
 for (UINT n = 0; n < pMainMenu->GetMenuItemCount(); ++n)
 {
  CMenu* pSubMenu = pMainMenu->GetSubMenu(n);
  cmdUI.m_nIndexMax = pSubMenu->GetMenuItemCount();
  for (UINT i = 0; i < cmdUI.m_nIndexMax;++i)
  {
   cmdUI.m_nIndex = i;
   cmdUI.m_nID = pSubMenu->GetMenuItemID(i);
   cmdUI.m_pMenu = pSubMenu;
   cmdUI.DoUpdate(this, FALSE);
  }
 }
 return TRUE;
}

Download demo project - 10 KB

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
Handling OnUpdate() processing for menu items - Legacy CodeGuru (10/16/2001)
return false; ^^ - Legacy CodeGuru (06/28/2001)
Smaller, faster and efficient (I hope) K.I.S.S. in other word. - Legacy CodeGuru (06/14/2001)
Avoid KickIdle,A simple way toUpdate!! - Legacy CodeGuru (11/30/2000)
Re : Code hoards CPU time - Legacy CodeGuru (11/02/2000)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers