1. 程式人生 > >CEdit只允許十六進位制的資料輸入

CEdit只允許十六進位制的資料輸入

網上說的大部分方法是過載CEdit,重寫OnChar函式,在歷遍進行資料判斷,如果只實現這一個功能完全不必過載CEdit,只需重寫PreTranslateMessage函式即可,函式新增方法,選中對話方塊類,右鍵屬性在過載的函式中找。實現方法如下:

BOOL CWindowsRouterDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_CHAR)
{

//檢查控制元件是否是當前焦點
if(GetDlgItem(IDC_EDIT6)==GetFocus()||
GetDlgItem(IDC_EDIT7)==GetFocus())
{

//檢查Ctrl組合鍵
short nks = GetKeyState(VK_CONTROL);


if (nks & 0x8000)
{
return CDialog::PreTranslateMessage(pMsg);
}

//檢查輸入的內容
if ((pMsg->wParam >= 0x30 && pMsg->wParam <=  0x39) ||
(pMsg->wParam  >= 'a' && pMsg->wParam  <= 'f') ||
(pMsg->wParam  >= 'A' && pMsg->wParam  <= 'F') ||
(pMsg->wParam  == 0x08) ||
(pMsg->wParam  == 0x20))
{
return CDialog::PreTranslateMessage(pMsg);

else
{
MessageBeep(-1);
pMsg->wParam=NULL;
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}