MFC中OnChar函数问题

来源:百度知道 编辑:UC知道 时间:2024/06/25 19:16:57
我写了一个简单的计算器程序,但是不能响应键盘,问一下大家那个OnChar函数怎么写?
可能我没说清楚,我的那个计算器编好了,就是不能响应键盘,也就是说我按键盘上的7,那个对话框不能接受数据,我写的一部分是这样的(编译器用的VC6.0)
void CMyDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch(nChar)
{
case ‘a’:
MessageBox("a");
break;
case 55:
OnButton7();
break;
}
}
按键盘没反应

回答二楼的,好像不行,其实我就是你那样写的

看到
OnButton7();

你肯定在界面上用了按钮, 如果当前焦点控件是按钮, 键盘事件会被按钮先处理掉了,不会传递到CMyDlg.

要想实现你的想法, 重载 PreTranslateMessage :

BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam}
{
'a' :......
}
}
return CDialog::PreTranslateMessage(pMsg);
}

你说的是你在界面上用鼠标按可以计算,但不能用键盘吧。改成下面样式就可以了。

void CMyDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch(nChar)
{
case '0':
//执行用鼠标按0函数
break;
case '1':
//执行用鼠标按1函数
break;
……
default:
break;

}

void MyFrameWindow::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
TCHAR str[2];
str[0]=(TCHAR)nChar;
str[1]=0;
}
字符数组str里面存储的就是你输入的字符