19,502
社区成员
发帖
与我相关
我的任务
分享
#pragma once
// CIPEdit
class CIPEdit : public CEdit
{
DECLARE_DYNAMIC(CIPEdit)
public:
CIPEdit();
virtual ~CIPEdit();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
};
// IPEdit.cpp : implementation file
//
#include "stdafx.h"
#include "Client.h"
#include "IPEdit.h"
// CIPEdit
IMPLEMENT_DYNAMIC(CIPEdit, CEdit)
CIPEdit::CIPEdit()
{
}
CIPEdit::~CIPEdit()
{
}
BEGIN_MESSAGE_MAP(CIPEdit, CEdit)
ON_WM_CHAR()
END_MESSAGE_MAP()
// CIPEdit message handlers
void CIPEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CString Str, tok, rightStr;
int iStart = 0, loop = 0;
int lastDotPos;
this->GetWindowTextW(Str);
lastDotPos = Str.ReverseFind((TCHAR)'.');
if(nChar >= '0' && nChar <= '9')
{
if(lastDotPos != -1)
{
rightStr = Str.Right(Str.GetLength() - lastDotPos - 1);
}
else
{
rightStr = Str;
}
rightStr.AppendChar((TCHAR)nChar);
if(_tstoi(rightStr) >= 0 && _tstoi(rightStr) <= 255)
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}
if(nChar == '.')
{
if(lastDotPos == Str.GetLength() - 1)
return;
tok = Str.Tokenize(_T("."), iStart);
while(tok != _T(""))
{
loop++;
tok = Str.Tokenize(_T("."), iStart);
}
if(loop < 4)
CEdit::OnChar(nChar, nRepCnt, nFlags);
return;
}
if(nChar == VK_RETURN || nChar == VK_TAB || nChar == VK_BACK)
{
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
}