type
TForm1 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
OldWndProc: TWndMethod;
procedure EditWndProc(var Message: TMessage);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.EditWndProc(var Message: TMessage);
begin
if (Message.Msg = WM_PASTE) or (Message.Msg = WM_CUT)
or ((Message.Msg = WM_CHAR) and (Message.WParam > 127)) then
Exit;
OldWndProc(Message);
end;
type
TForm1 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
OldWndProc: TWndMethod;
procedure EditWndProc(var Message: TMessage);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.EditWndProc(var Message: TMessage);
var
ch: Word;
begin
if (Message.Msg = WM_PASTE) or (Message.Msg = WM_CUT)
or ((Message.Msg = WM_CHAR) and (Message.WParam > 127)) then
Exit;
OldWndProc(Message);
end;
太麻烦了,判断你的输入框里是否是汉字,可以利用,字符串类型,string和widestring的占位,1个是1个字节为单位的,1个是2个字节为单位的
function CheckInputStrIsChinese(Var S:string):boolean;
var
Str:Widestring;
begin
Str:=S;
if length(Str)=2*length(S) then
result:=false //没有汉字
else result:=true; //有了!
end;
for I := Length(S) downto 1 do
if S[I] > #255 then // 这样判断更简洁一些
begin
if vSelStart <= Length(string(Copy(S, 1, I))) then Inc(J, 2);
Delete(S, I, 1);
end;
//包括粘贴也会过滤掉
procedure TForm1.Edit1Change(Sender: TObject);
var
S: WideString;
I, J: Integer;
vSelStart: Integer;
begin
vSelStart := TEdit(Sender).SelStart;
S := TEdit(Sender).Text;
J := 0;
for I := Length(S) downto 1 do
if Length(string(S[I])) >= 2 then
begin
if vSelStart <= Length(string(Copy(S, 1, I))) then Inc(J, 2);
Delete(S, I, 1);
end;
TEdit(Sender).Text := S;
TEdit(Sender).SelStart := vSelStart - J;
end;