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()函数里出不来了,就算我马上松开按键,也无济于事
还望各位高手请指点迷津!!
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()函数里出不来了,就算我马上松开按键,也无济于事
还望各位高手请指点迷津!!
我用pMsg->hwnd的话,怎么获取当前按下的按钮呢,pMsg->hwnd == GetDlgItem(backspace)这样是有错的呀,错误提示:no conversion from 'CWnd *' to ‘HWND’,而且如果我来一个强制类型转换,pMsg->hwnd == (HWND)GetDlgItem(backspace)的话似乎没用哦
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);这一句提出来后,能实现长按该按钮的功能,但是,按了“<--”以后,按下每一个键都是按“<--”的效果,这是为什么呀?
{
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);这一句有问题吗?问题在哪?
CString a;
CString b;
char *pa = (char*)((LPCTSTR)a);
char *pb = (char*)((LPCTSTR)b);
if(!strcmp(pa,pb))
{
MessageBox("pa==pb");
}
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);
}