EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

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[] = { 
10      "ID_COMMAND_DRAWPIXEL",
11      "ID_COMMAND_DRAWLINE",
12      "ID_COMMAND_DRAWTRIANGLE",
13      "ID_COMMAND_DRAWRECTANGLE",
14      "ID_COMMAND_DRAWPOLY"
15  };
16 
17  class CMyWnd : public CFrameWnd
18  {
19    private:
20    CMenu mnuMain;
21    CMenu mnuDraw;
22    public:
23      CMyWnd();
24      afx_msg void OnRangeCmds(UINT id);
25      afx_msg void OnDestroy();
26      DECLARE_MESSAGE_MAP()
27  };
28  BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
29  ON_COMMAND_RANGE(ID_COMMAND_DRAWPIXEL, ID_COMMAND_DRAWPOLY, CMyWnd::OnRangeCmds)
30  ON_WM_DESTROY()
31  END_MESSAGE_MAP()
32 
33 
34  CMyWnd::CMyWnd()
35  {
36    Create(NULL, "Menu ID Range Demo", WS_OVERLAPPEDWINDOW, CRect(200, 120, 640, 400), NULL);
37    mnuMain.CreateMenu();
38    mnuDraw.CreateMenu();
39    mnuMain.AppendMenu(MF_POPUP | MF_STRING, (UINT)mnuDraw.m_hMenu ,        "Draw");
40    mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWPIXEL,     "Pixel");
41    mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWLINE,      "Line");
42    mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWTRIANGLE,  "Triangle");
43    mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWRECTANGLE, "Rectangle");
44    mnuDraw.AppendMenu(MF_BYPOSITION | MF_STRING, ID_COMMAND_DRAWPOLY,      "Poly");
45    SetMenu(&mnuMain);
46  }
47  afx_msg void CMyWnd::OnDestroy()
48  {
49     mnuMain.DestroyMenu();
50     mnuDraw.DestroyMenu();
51 
52  }
53  afx_msg void CMyWnd::OnRangeCmds(UINT id)
54  {
55    CString msg;
56    msg.Format("Menu id %d %s",id, szMenuID[id-ID_COMMAND_DRAWPIXEL]);
57    AfxMessageBox(msg, IDOK);
58 
59  }
60   
61  class CMyApp : public  CWinApp 
62  {
63 
64    public:
65       virtual BOOL InitInstance()
66      {
67        m_pMainWnd = new CMyWnd();
68        m_pMainWnd->ShowWindow(SW_SHOW);
69        m_pMainWnd->UpdateWindow();
70        return TRUE;
71      }
72   };
73  CMyApp app;

Output

Menu ID range ON_COMMAND_RANGE

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 10:08:04(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.