我有一个程序需要创建100多个按钮,而这些按钮实现的功能只有部分差别,那么我想在一个消息响应中来完成,有没有办法可以实现的?
    比如这些控件的ID是从10000~10100之间,我该如何在一个消息中来实现对100个按键的响应,通过ID的不同来实现其不同部分的功能?

解决方案 »

  1.   

    头文件
    afx_msg void OnBnClickedYourBt();
    BEGIN_MESSAGE_MAP(CDataView, CFormView)
        ON_BN_CLICKED(10000, OnBnClickedYourBt)
        ON_BN_CLICKED(10001, OnBnClickedYourBt)
        ON_BN_CLICKED(10002, OnBnClickedYourBt)
        ..............//大概不能使用循环
    END_MESSAGE_MAP()void CXXX::OnBnClickedYourBt()
    {
    int id = GetFocus()->GetDlgCtrlID();
    switch(id)
    {
    case 10000:
    ...
    case 10001:
    ...}}
      

  2.   

    参考ON_COMMAND_RANGE:如:
    afx_msg void OnIndexComp(UINT wParam, LONG lParam);
    ON_COMMAND_RANGE(IDM_VIEW_SETTING_CHART1, IDM_VIEW_SETTING_CHART39, OnIndexComp)
      

  3.   

    ON_COMMAND_RANGE
    ON_COMMAND_RANGE( id1, id2, memberFxn )Parametersid1Command ID at the beginning of a contiguous range of command IDs.id2Command ID at the end of a contiguous range of command IDs.memberFxnThe name of the message-handler function to which the commands are mapped.ResUse this macro to map a contiguous range of command IDs to a single message handler function. The range of IDs starts with id1 and ends with id2. Use ON_COMMAND_RANGE to map a range of command IDs to one member function. Use ON_COMMAND to map a single command to a member function. Only one message-map entry can match a given command ID. That is, you can’t map a command to more than one handler. For more information on mapping message ranges, seeHandlers for Message-Map Ranges in Visual C++ Programmer’s 
      

  4.   

    同意楼上。ON_COMMAND_RANGE可以处理一系统连续的自定义消息。
      

  5.   

    ON_COMMAND_RANGE或者重载PretransLateMessage