MFC 入门级菜鸟的问题如下我有一个按钮,name为“<--”,ID为backspace;
OnTimer函数
void Cxx::OnTimer(UINT_PTR nIDEvent)
{
  OnBnClickedBackspace();//单击“<--”键的响应事件  CDialog::OnTimer(nIDEvent);
}
有PreTranslateMessage函数
BOOL Cxx::PreTranslateMessage(MSG* pMsg)
{
  CString str =_T("");
  if(pMsg->message == WM_LBUTTONDOWN)
  {
    GetFocus()->GetWindowText(str);
    if(str == _T("<--"))
    {
      SetTimer(1,1000,NULL);
      return 1;
    }
  }
    else if(pMsg->message == WM_LBUTTONUP && str == _T("<--"))
      {
        KillTimer(1);
        return 1;
      }
     
    return CDialog::PreTranslateMessage(pMsg);
}
可是在运行的时候,我首先按下“<--”按钮后马上松开运行正常,但是在第二次按下这个键的时候,似乎掉进OnTimer()函数里出不来了,就算我马上松开按键,也无济于事
还望各位高手请指点迷津!!

解决方案 »

  1.   

    else if(pMsg->message == WM_LBUTTONUP && str == _T("<--"))你的这个if里判断str == _T("<--"), 可是在这个WM_LBUTTONUP消息里,你的str还没有初始化过,还是空的。另外,PreTranslateMessage里判断消息发往的窗口,最好使用pMsg->hwnd,这个就代表消息发往的窗口。还有OnTimer里,最好还是判断下nIDEvent == 1,写规范点会减少些无谓的BUG的可能。
      

  2.   

    最简单办法,把CButton按钮重载一下,然后在重载类中加入WM_TIMER、WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息;把你的那个按钮和这个类进行关联就行了
      

  3.   

    dream0411,当我松开按钮时,还能用GetFocus()->GetWindowText(str);来获取刚刚松开的按钮的标题吗?
    我用pMsg->hwnd的话,怎么获取当前按下的按钮呢,pMsg->hwnd == GetDlgItem(backspace)这样是有错的呀,错误提示:no conversion from 'CWnd *' to ‘HWND’,而且如果我来一个强制类型转换,pMsg->hwnd == (HWND)GetDlgItem(backspace)的话似乎没用哦
      

  4.   

    还有,就是,我将代码改了下:
    BOOL Cxx::PreTranslateMessage(MSG* pMsg)
    {
      CString str =_T("");
      GetFocus()->GetWindowText(str);  if(pMsg->message == WM_LBUTTONDOWN)
      {
        if(str == _T("<--"))
         {
           SetTimer(1,1000,NULL);
           return 1;
         }
      }
      if(pMsg->message == WM_LBUTTONUP)
      {
        if(str == _T("<--"))
         {
           KillTimer(1);
           return 1;
         }
      }
        
      return CDialog::PreTranslateMessage(pMsg);
    }将GetFocus()->GetWindowText(str);这一句提出来后,能实现长按该按钮的功能,但是,按了“<--”以后,按下每一个键都是按“<--”的效果,这是为什么呀?
      

  5.   

    现修改代码如下:BOOL Cxx::PreTranslateMessage(MSG* pMsg)
    {
      CString str =_T("");
       if(pMsg->message == WM_LBUTTONDOWN)
      {
        GetFocus()->GetWindowText(str);
      if(str == _T("<--"))
      {
      SetTimer(1,1000,NULL);
      }
      }
      if(pMsg->message == WM_LBUTTONUP)
      {
      KillTimer(1);
      str = "";
      }
        
      return CDialog::PreTranslateMessage(pMsg);
    }出现最后一个问题,似乎每一个键长按,if(str == _T("<--"))都会判断为true,难道说 GetFocus()->GetWindowText(str);这一句有问题吗?问题在哪?
      

  6.   

     MFC中两CString字符串不能直接进行比较,需要转换成char*类型来进行比较。
    CString  a;
    CString  b;
    char *pa = (char*)((LPCTSTR)a);
    char *pb = (char*)((LPCTSTR)b);
    if(!strcmp(pa,pb))
    {
          MessageBox("pa==pb");
    }
      

  7.   

    问题解决,
    BOOL Cxx::PreTranslateMessage(MSG* pMsg)
    {
      if(pMsg->message == WM_LBUTTONDOWN && pMsg-> hwnd == GetDlgItem(backspace)-> GetSafeHwnd())
      {
      SetTimer(1,1000,NULL);
      }
      if(pMsg->message == WM_LBUTTONUP)
      {
      KillTimer(1);
      }   
      return CDialog::PreTranslateMessage(pMsg);
    }