在对话框上拖入一个edit控件,想实现每次鼠标点击edit控件时,改变控件的背景色。在网上找了一些资料,我的大概思路是1.响应对话框的OnLButtonDown消息,在这里设置标识,用于标记现在点击在哪个edit上2.添加OnCtlColor,在其中改变edit框的背景色好像OnLButtonDown消息被edit框截获了,所以edit控件收不到鼠标点下的消息请教高手有什么好的思路,最好能贴上代码!谢谢

解决方案 »

  1.   

    对话框类的PreTranslateMessage虚函数中过滤处理WM_LBUTTONDOWN消息
      

  2.   

    在父对话框类添加虚函数PreTranslateMessage(),在它里面过滤WM_LBUTTONDOWN消息是这样吗?
    if (pMsg->wParam==WM_LBUTTONDOWN)
    {
    return FALSE;
    }
    好像还是不行啊
      

  3.   

    囧,是if(WM_LBUTTONDOWN == pMsg->message)不是pMsg->wParam
      

  4.   

    问题是我把edit控件放在对话框上,添加对话框的WM_LBUTTONDOWN消息会被edit控件截获啊,PreTranslateMessage好像不行啊HBRUSH CEditcolorDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO: Change any attributes of the DC here

    // TODO: Return a different brush if the default is not desired if (TRUE == m_b)
    {
    CRect rect;
    GetDlgItem(IDC_EDIT1)->GetClientRect(rect); 
    CClientDC dc(GetDlgItem(IDC_EDIT1));

    CBrush brush(RGB(255,0,0));
    dc.FillRect(rect,&brush);  }
    return hbr;
    }BOOL CEditcolorDlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if (pMsg->message==WM_LBUTTONDOWN)
    {
    return FALSE;
    }
    return CDialog::PreTranslateMessage(pMsg);
    }void CEditcolorDlg::OnLButtonDown(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    CRect rc;
    GetDlgItem(IDC_EDIT1)->GetClientRect(rc);
    ClientToScreen(rc);
    InvalidateRect(rc);
    if (rc.PtInRect(point))
    {
    m_b = TRUE;
    }
    CDialog::OnLButtonDown(nFlags, point);
    }
    你看看我的代码对不对,点击edit控件进不到OnLButtonDown函数里去啊?
      

  5.   

    我的意思是你把OnLButtonDown的处理放到PreTranslateMessage中去做
      

  6.   

    BOOL CXXDlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
    ASSERT(pEdit);
    if((pEdit->GetSafeHwnd() == pMsg->hwnd) && (WM_LBUTTONDOWN == pMsg->message))
    {
    CRect rect;
    pEdit->GetWindowRect(&rect);
    if(rect.PtInRect(pMsg->pt))
    {
    AfxMessageBox(_T("In Edit"));
    }
    }
    return CDialog::PreTranslateMessage(pMsg);
    }
      

  7.   

    这样是实现了这个功能。但是有个不太合理的地方是,PreTranslateMessage只是在程序启动的时候被调用。而我想实现的功能是在程序运行过程中,每次点击一个edit框,其背景就发生变化,点击另一个,之前的那个恢复成白色,后面被点击的那个的背景色发生变化。不知道有没更好的办法呢