ON_COMMAND_RANGE

ON_COMMAND_RANGE can be used to map a contiguous range of command IDs to a single message handler function.

Syntax

ON_COMMAND_RANGE (id1, id2, memberFxn)

Parameters

  • id1 - Command ID at the beginning of a contiguous range of command IDs.
  • id2 - Command ID at the end of a contiguous range of command IDs.
  • memberFxn - The name of the message-handler function to which the commands are mapped.

ON_CONTROL_RANGE

ON_CONTROL_RANGE macro to map a contiguous range of control IDs to a single message handler function for a specified Windows notification message,

Syntax

ON_CONTROL_RANGE(wNotifyCode, id1, id2, memberFxn )

Parameters

  • wNotifyCode - The notification code to which your handler is responding.
  • id1 - Command ID at the beginning of a contiguous range of control IDs.
  • id2 - Command ID at the end of a contiguous range of control IDs.
  • memberFxn - The name of the message-handler function to which the controls are mapped.

Remarks

The range of IDs starts with id1 and ends with id2. The handler is called for the specified notification coming from any of the mapped controls. There is no automatic support for message map ranges, so you must place the macro yourself.

Demo Application

Here is a demo app to show the utility of ON_COMMAND_RANGE(). We are showing a menu based app used for drawing verious shapes. We have taken a range of menu IDs to map to asingle drawing function. We decode the drawing function using the argument ID passed to the command function. We are displaying which id is called with the help of message box.

#include <afxwin.h>

#define ID_COMMAND_DRAWPIXEL      1001
#define ID_COMMAND_DRAWLINE       1002
#define ID_COMMAND_DRAWTRIANGLE   1003
#define ID_COMMAND_DRAWRECTANGLE  1004
#define ID_COMMAND_DRAWPOLY       1005

char * szMenuID[] = { 
    "ID_COMMAND_DRAWPIXEL",
    "ID_COMMAND_DRAWLINE",
    "ID_COMMAND_DRAWTRIANGLE",
    "ID_COMMAND_DRAWRECTANGLE",
    "ID_COMMAND_DRAWPOLY"
};

class CMyWnd : public CFrameWnd
{
  private:
  CMenu mnuMain;
  CMenu mnuDraw;
  public:
    CMyWnd();
    afx_msg void OnRangeCmds(UINT id);
    afx_msg void OnDestroy();
    DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_COMMAND_RANGE(ID_COMMAND_DRAWPIXEL, ID_COMMAND_DRAWPOLY, CMyWnd::OnRangeCmds)
ON_WM_DESTROY()
END_MESSAGE_MAP()


CMyWnd::CMyWnd()
{
  Create(NULL, "Menu ID Range Demo", WS_OVERLAPPEDWINDOW, CRect(200, 120, 640, 400), NULL);
  mnuMain.CreateMenu();
  mnuDraw.CreateMenu();
  mnuMain.AppendMenu(MF_POPUP | MF_STRING, (UINT)mnuDraw.m_hMenu ,        "Draw");
  mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWPIXEL,     "Pixel");
  mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWLINE,      "Line");
  mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWTRIANGLE,  "Triangle");
  mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWRECTANGLE, "Rectangle");
  mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWPOLY,      "Poly");
  SetMenu(&mnuMain);
}
afx_msg void CMyWnd::OnDestroy()
{
   mnuMain.DestroyMenu();
   mnuDraw.DestroyMenu();

}
afx_msg void CMyWnd::OnRangeCmds(UINT id)
{
  CString msg;
  msg.Format("Menu id %d %s",id, szMenuID[id-ID_COMMAND_DRAWPIXEL]);
  AfxMessageBox(msg, IDOK);

}
 
class CMyApp : public  CWinApp 
{

  public:
     virtual BOOL InitInstance()
    {
      m_pMainWnd = new CMyWnd();
      m_pMainWnd->ShowWindow(SW_SHOW);
      m_pMainWnd->UpdateWindow();
      return TRUE;
    }
 };
CMyApp app;

Output

Menu ID range ON_COMMAND_RANGE

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

Learn on Youtube

#