1。 // The code fragment below shows how to use ON_COMMAND_RANGE macro // to map a contiguous range of command IDs to a single message // handler function (i.e. OnFileMenuItems() is the sample below). In // addition, it also shows how to use CheckMenuRadioItem() to check a // selected menu item and makes it a radio item. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) // ... //}}AFX_MSG_MAP ON_COMMAND_RANGE(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, OnFileMenuItems) END_MESSAGE_MAP()void CMainFrame::OnFileMenuItems(UINT nID) { CMenu* mmenu = GetMenu(); CMenu* submenu = mmenu->GetSubMenu(0); submenu->CheckMenuRadioItem(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, nID, MF_BYCOMMAND); }
Well I program a example for you! #include <afxtempl.h> #define ID_BUTTON_FRIST 100 #define BUTTON_COUNT 5 class CAaaaaDlg : public CDialog { void CAaaaaDlg::CreateButttons(void); protected: HICON m_hIcon; CArray<HWND,HWND> m_arrButton; .............. //}}AFX_MSG afx_msg void OnCustomButtonHandler(UINT nID );
DECLARE_MESSAGE_MAP() };BEGIN_MESSAGE_MAP(CAaaaaDlg, CDialog) //{{AFX_MSG_MAP(CAaaaaDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_WM_DESTROY() //}}AFX_MSG_MAP ON_COMMAND_RANGE(ID_BUTTON_FRIST,ID_BUTTON_FRIST+BUTTON_COUNT,OnCustomButtonHandler) END_MESSAGE_MAP()// CAaaaaDlg message handlers void CAaaaaDlg::CreateButttons(void) { CRect rect; GetClientRect(&rect); rect.bottom=rect.top +30; rect.right =rect.left+50; for(int i=0;i<BUTTON_COUNT;i++) { CString str; str.Format("Btn %d",i+1); HWND hWnd=::CreateWindow("BUTTON",str,WS_VISIBLE|WS_CHILD,rect.left,rect.top,rect.Width(),rect.Height(),this->GetSafeHwnd(),(HMENU)(ID_BUTTON_FRIST+i),::GetModuleHandle(NULL),NULL); rect.OffsetRect(60,0); m_arrButton.Add(hWnd); } }BOOL CAaaaaDlg::OnInitDialog() { CDialog::OnInitDialog(); .............. // TODO: Add extra initialization here CreateButttons(); return TRUE; // return TRUE unless you set the focus to a control }void CAaaaaDlg::OnDestroy() { CDialog::OnDestroy();
// The code fragment below shows how to use ON_COMMAND_RANGE macro
// to map a contiguous range of command IDs to a single message
// handler function (i.e. OnFileMenuItems() is the sample below). In
// addition, it also shows how to use CheckMenuRadioItem() to check a
// selected menu item and makes it a radio item.
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// ...
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3, OnFileMenuItems)
END_MESSAGE_MAP()void CMainFrame::OnFileMenuItems(UINT nID)
{
CMenu* mmenu = GetMenu();
CMenu* submenu = mmenu->GetSubMenu(0);
submenu->CheckMenuRadioItem(ID_FILE_MENUITEM1, ID_FILE_MENUITEM3,
nID, MF_BYCOMMAND);
}
#include <afxtempl.h>
#define ID_BUTTON_FRIST 100
#define BUTTON_COUNT 5
class CAaaaaDlg : public CDialog
{
void CAaaaaDlg::CreateButttons(void);
protected:
HICON m_hIcon;
CArray<HWND,HWND> m_arrButton;
..............
//}}AFX_MSG
afx_msg void OnCustomButtonHandler(UINT nID );
DECLARE_MESSAGE_MAP()
};BEGIN_MESSAGE_MAP(CAaaaaDlg, CDialog)
//{{AFX_MSG_MAP(CAaaaaDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
ON_COMMAND_RANGE(ID_BUTTON_FRIST,ID_BUTTON_FRIST+BUTTON_COUNT,OnCustomButtonHandler)
END_MESSAGE_MAP()// CAaaaaDlg message handlers
void CAaaaaDlg::CreateButttons(void)
{
CRect rect;
GetClientRect(&rect);
rect.bottom=rect.top +30;
rect.right =rect.left+50;
for(int i=0;i<BUTTON_COUNT;i++)
{
CString str;
str.Format("Btn %d",i+1);
HWND hWnd=::CreateWindow("BUTTON",str,WS_VISIBLE|WS_CHILD,rect.left,rect.top,rect.Width(),rect.Height(),this->GetSafeHwnd(),(HMENU)(ID_BUTTON_FRIST+i),::GetModuleHandle(NULL),NULL);
rect.OffsetRect(60,0);
m_arrButton.Add(hWnd);
}
}BOOL CAaaaaDlg::OnInitDialog()
{
CDialog::OnInitDialog();
..............
// TODO: Add extra initialization here
CreateButttons();
return TRUE; // return TRUE unless you set the focus to a control
}void CAaaaaDlg::OnDestroy()
{
CDialog::OnDestroy();
for(int n=0;n<m_arrButton.GetSize();n++)
{
HWND h=m_arrButton.GetAt(n);
::DestroyWindow(h);
}
}
void CAaaaaDlg::OnCustomButtonHandler(UINT nID )
{
int n=nID-ID_BUTTON_FRIST+1;
CString str;
str.Format("Button %d Clicked!",n);
AfxMessageBox(str);
}