1

中国とインドで使用されているアプリケーションがあります。Cedit コントロールのセットでは、ユーザーが古典的なラテン アルファベットの一部である文字のみを入力できるようにしたいと考えています (ISO8859-1 で問題ありません)。これらのコントロールは登録データを入力するために使用されるため、漢字は読めないため役に立ちません。

アプリケーションは、UNICODE ビルドを使用した MFC ベースです。
これらの CEdit をラテン文字に制限するにはどうすればよいですか。使用可能な文字は、CEdits またはこのフォントの CharacterSet で使用されるフォントに依存しますか?
現在、私はかなり混乱しており、助け、ヒント、ヒント、方向性があれば本当に感謝しています.

4

1 に答える 1

0

これは私が私のプログラムでやったことです:

void CRequestForm::OnEnChangeEditDetail()
{
  // Filter out the clinical note of any invalid characters as the user types.
  CString strNotes;

  m_edit.GetWindowText(strNotes);
  BOOL bValid = TRUE;
  int nStartChar, nEndChar;
  CString strTyped;
  m_edit.GetSel(nStartChar, nEndChar);
  if(strNotes.GetLength() - m_strOldNote.GetLength() == 1)
  {
    // this is normal user typing
    CString strInvalidChars;

    if(nStartChar == nEndChar && nStartChar > 0)
      strTyped = strNotes.Mid(nStartChar-1, 1);
    if(!CheckInvalidCharacters(strTyped))
    {
      m_edit.SetWindowText(m_strOldNote);
      if(nStartChar > 0 && nEndChar > 0)
        m_edit.SetSel(nStartChar-1, nEndChar-1);
      else
        m_edit.SetSel(nStartChar, nEndChar);
      bValid = FALSE;
    }
  }
  else if(strNotes.GetLength() - m_strOldNote.GetLength() == 2 &&
    nStartChar == nEndChar && nStartChar > 0 && 
    strNotes.Mid(nStartChar-2, 2) == _T("\r\n"))
  {
    // Allow CrLf
    bValid = TRUE;
  }
  else
  {
    // this is most likely the case of "Pasted" text. need just to remove invalid characters
    RemoveInvalidChars(strNotes);
  }

  if(bValid)
  {
    m_strOldNote = strNotes;
    m_edit.SetWindowText(strNotes);
    m_edit.SetSel(nStartChar, nEndChar);
  }
}

ご覧のとおり、m_strOldNote(OnInitDialog 内の) 初期テキストで開始されるインスタンス変数が必要です。m_edit では、EN_CHANGE イベントを処理する必要があります。

2 つの関数が必要になります:CheckInvalidCharacters()そして、RemoveInvalidChars()これは次のように簡単です:

BOOL CRequestForm::CheckInvalidCharacters(const CString& strText)
{
  BOOL bResult = TRUE;

  int nPos, nLenght;
  nLenght = strText.GetLength();
  for(nPos = 0; nPos < nLenght; ++nPos)
  {
    TCHAR ch = strText.GetAt(nPos);

    if(!(ch >= '0' && ch <= '9') &&
       !(ch >= 'A' && ch <= 'Z') &&
       !(ch >= 'a' && ch <= 'z') &&
       !(ch >=  32 && ch <=  64 && ch != '&') && // Space thru @, excluding |^~\&
       !(ch == '[' || ch == ']' || ch == '{' || ch == '}' || ch == '_' || ch == '\r' || ch == '\n'))
    {
      bResult = FALSE;
    }
  }

  return bResult;
}

void CRequestForm::RemoveInvalidChars(CString &str)
{
  // remove non-ASCII characters
  int nPos, nLenght;
  nLenght = str.GetLength();
  for(nPos = 0; nPos < nLenght; ++nPos)
  {
    TCHAR ch = str.GetAt(nPos);

    if(!((ch >= _T('0') && ch <= _T('9')) ||
         (ch >= _T('A') && ch <= _T('Z')) ||
         (ch >= _T('a') && ch <= _T('z')) ||
         (ch >=  32 && ch <=  64 && ch != _T('&')) || // Space thru @, excluding |^~\&
          ch == _T('[') || ch == _T(']') || ch == _T('{') || ch == _T('}') || ch == _T('_')))
    {
      str.SetAt(nPos, _T(' '));
    }
  }
}
于 2013-03-21T00:22:11.390 に答える