(*向楼主献上RichEdit实现的代码:代码长一点,但并不是因为使用了RichEdit,而是为了实现你要的功能*)var PswStr: string; //全局的变量,初值为''procedure TForm1.RichEdit1Enter(Sender: TObject); begin with TRichEdit(Sender) do begin Clear; Text := '**********'; Tag := 0; SelStart := 0; SetFocus; end; PswStr := ''; end;procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char); begin case Key of #1..#7, #9..#31 : ; #8 : with TRichEdit(Sender) do begin Tag := Tag - 1; if Tag < 0 then Tag := 0; SetLength(PswStr, Tag); SelStart := Tag; SelLength:= 10-Tag; SelAttributes.Color := clBlack; SelStart := Tag; end; else with TRichEdit(Sender) do if Tag <= 9 then begin PswStr := PswStr + Key; SelStart := Tag; SelLength:= 1; Tag := Tag + 1; SelAttributes.Color := clRed; SelStart := Tag; end; end; Key := #0; Caption := PswStr; //在Form.Caption显示输入的字符 end;procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin Key := 0; end;
用RichEdit实现比较简单。
PswStr: string; //全局的变量,初值为''procedure TForm1.RichEdit1Enter(Sender: TObject);
begin
with TRichEdit(Sender) do begin
Clear;
Text := '**********';
Tag := 0;
SelStart := 0;
SetFocus;
end;
PswStr := '';
end;procedure TForm1.RichEdit1KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
#1..#7, #9..#31 : ;
#8 : with TRichEdit(Sender) do begin
Tag := Tag - 1;
if Tag < 0 then Tag := 0;
SetLength(PswStr, Tag);
SelStart := Tag;
SelLength:= 10-Tag;
SelAttributes.Color := clBlack;
SelStart := Tag;
end;
else with TRichEdit(Sender) do
if Tag <= 9 then begin
PswStr := PswStr + Key;
SelStart := Tag;
SelLength:= 1;
Tag := Tag + 1;
SelAttributes.Color := clRed;
SelStart := Tag;
end;
end;
Key := #0;
Caption := PswStr; //在Form.Caption显示输入的字符
end;procedure TForm1.RichEdit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
Key := 0;
end;