我试图通过覆盖 OnChar 和 OnKeydown 来阻止某些类型的字符插入到我的编辑控件中。我试图阻止不止一点'。以及任何不是数字的东西。
首先我检查是否已经有一个“。” 通过将对话框类中定义的变量设置为 false,在具有焦点的编辑控件中:
void MyMainDialog::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
  CWnd * eb1 = GetDlgItem(IDC_EDIT1); //Reference dimension 1 box;
  CWnd * eb2 = GetDlgItem(IDC_EDIT2); //Reference dimension 2 box
  CWnd * eb3 = GetDlgItem(IDC_EDIT3); //Reference dimension 3 box
  CString temp;
  CWnd * focusedHand = MyMainDialog::GetFocus(); //Reference edit box being focused
  if(focusedHand == eb1)
  {
    eb1->GetWindowTextA(temp);
    if(temp.Find('.') != -1)
      checkPoint = true;
    else
      checkPoint = false;
  }
  else if(focusedHand == eb2)
  {
    eb2->GetWindowTextA(temp);
    if(temp.Find('.') != -1)
      checkPoint = true;
    else
      checkPoint = false;
  }
  else if(focusedHand == eb3)
  {
    eb3->GetWindowTextA(temp);
    if(temp.Find('.') != -1)
      checkPoint = true;
    else
      checkPoint = false;
  }
  CDialogEx::OnKeyDown(nChar, nRepCnt, nFlags);
}
在 OnChar 我正在检查正在输入的字符。如果它不是一个点,但已经有一个点,那么我不会从 CDialog 调用 OnChar:
void MyMainDialog::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
   if(nChar == '.' && checkPoint == false) //Is a point and there is no other point
  {
     CDialogEx::OnChar(nChar, nRepCnt, nFlags);   
  }
  if((nChar < '0' || nChar > '9')) //Is not a number
  {
     //Show message to user
  }
  else //Is a number
  {
    CDialogEx::OnChar(nChar, nRepCnt, nFlags);
  }
}
好吧,我的代码不起作用。它编译并且在编辑控件中键入时不会崩溃,但它根本不做任何事情。我想知道覆盖它的正确方法是否是阻止调用 CDialogEx::OnChar() 或者我是否应该使 nChar = 0 以便显示的字符为空。但最重要的是,我试图在 OnChar 上显示的消息也没有显示,这意味着 MyMainDialog::OnChar() 甚至没有被调用。我应该改写 CDialogEx::OnChar() 吗?
感谢您的关注