我是编程的初学者,在编绘图软件的调色板时,编了20多个STATIC的控件用于颜色的选择,但是在消息响应时,我不想加20多个ON_BN_CLICKED,别人告诉我把这20多的STATIC控件添加同一个名的消息响应,然后在消息函数中,用switch判断按的是哪个STATIC,然后对画笔赋值,我想问如何判断我按的是哪个STATIC呢?
我的代码如下,请大家帮忙,谢谢了。//左键获取颜色
void CColorSet::Oncolor() 
{
// TODO: Add your control notification handler code here        //GetDlgCtrlID();
switch (/*如何判断当前ID?*/)
{
case IDC_COLOR1:
Pencolor=color[1];
break;
case IDC_COLOR2:
Pencolor=color[2];
break;
case IDC_COLOR3:
Pencolor=color[3];
break; }
}

解决方案 »

  1.   

    Oncolor是响应什么消息的 看看有没有参数wParam/lParam
    一般参数里有ID 再switch里判断
      

  2.   

    Oncolor是我的STATIC的消息响应函数,就是把20多个的STATIC消息的响应函数变成一个响应函数
      

  3.   

    DWORD dwID = GetFocus()->GetDlgCtrlID(); 试一下
      

  4.   


    ON_CONTROL_RANGE(BN_CLICKED, IDC_COLOR1, IDC_COLOR20, &CColorSet::OnColor)void CColorSet::OnColor(UINT nIndex)
    {
        // nIndex 就是控件ID
    }
      

  5.   

    在资源编辑中设置static控件的属性,在style属性页中将Notify选上,这样static控件才接收消息
      

  6.   

    void CColorSet::Oncolor() 
    {
        // TODO: Add your control notification handler code here    switch (GetFocus()->GetDlgCtrlID())    
        {
        case IDC_COLOR1:
            Pencolor=color[1];
            break;
        case IDC_COLOR2:
            Pencolor=color[2];
            break;
        case IDC_COLOR3:
            Pencolor=color[3];
            break;    }
    }
      

  7.   

    或者
    BOOL CDlgFunc::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if(::TranslateAccelerator(AfxGetMainWnd()->m_hWnd,g_hAccelTable,pMsg))
    return TRUE;
    if(pMsg->message==WM_LBUTTONDOWN)
    {
    CRect rect;
    GetDlgItem(IDC_STATIC_FIRST)->GetWindowRect(&rect);
    if(rect.PtInRect(pMsg->pt))
    {
                       Pencolor=color[1];
    }
             
                    GetDlgItem(IDC_STATIC_SECOND)->GetWindowRect(&rect);
            if(rect.PtInRect(pMsg->pt))
    {
                      Pencolor=color[2];
    }
            }
    }
      

  8.   

    上面的if(::TranslateAccelerator(AfxGetMainWnd()->m_hWnd,g_hAccelTable,pMsg))
            return TRUE; 删除掉。另外在后面加上return CDialog::PreTranslateMessage(pMsg);

    BOOL CDlgFunc::PreTranslateMessage(MSG* pMsg) 
    {
        // TODO: Add your specialized code here and/or call the base class
          if(pMsg->message==WM_LBUTTONDOWN)
        {
            CRect rect;
            GetDlgItem(IDC_STATIC_FIRST)->GetWindowRect(&rect);
            if(rect.PtInRect(pMsg->pt))
            {
                       Pencolor=color[1];
            }
             
                    GetDlgItem(IDC_STATIC_SECOND)->GetWindowRect(&rect);
                if(rect.PtInRect(pMsg->pt))
            {
                      Pencolor=color[2];
            }
            }
    return CDialog::PreTranslateMessage(pMsg);
    }
      

  9.   

    9楼的办法好像没起作用,PreTranslateMessage的方法我用过,可是我想用9楼的方法,不知该怎么办?
      

  10.   


    PreTranslateMessage的办法我试了一下是可以的。 
    看了一下你说的是STATIC控件,,,在你的函数里又是个IDC_COLOR2...如果是static控件的话,貌似没有想按钮一样能得到控件ID,也获取不到焦点。。所以只能获取该static控件的位置。