4.在小键盘的OnKeyPadClick事件中加入以下代码 Delphi(Pascal) code procedure TKeyPadForm.MyKeypadKeyPadClick(var aChar: Char); begin //密码长度超过16 则发出警告然后退出 if Length(TruePassword) >= 16 then begin MessageBeep(MB_OK); Exit; end; //发送消息使密码框增加一个● PostMessage(Application.MainForm.Handle, WM_PASSWORDADDCHAR, 0, 0); //修改密码 achar为鼠标点击小键盘的字符 Insert(aChar, TruePassword, PasswordSelStart + 1); //光标向前进一位 PasswordSelStart := PasswordSelStart + 1; //随机改变小键盘上按键的顺序 MyKeypad.RandomKeyPad; end;
5.小键盘按钮点击事件 Delphi(Pascal) code procedure TMainForm.KeyPadBtnClick(Sender: TObject); var BtnPoint: TPoint; begin //判断小键盘窗体有无显示 if not KeyPadForm.Showing then begin //计算小键盘窗体在屏幕上显示的坐标 BtnPoint.X := KeyPadBtn.Left - 4; BtnPoint.Y := KeyPadBtn.Top + 17; BtnPoint := ClientToScreen(BtnPoint); KeyPadForm.Top := BtnPoint.Y; KeyPadForm.Left := BtnPoint.X; KeyPadForm.Show; //初始化光标位置 PasswordSelStart := PasswordEdit.SelStart; end else KeyPadForm.Hide; end;
6.主窗体被激活的时候需要隐藏小键盘 Delphi(Pascal) code procedure TMainForm.FormActivate(Sender: TObject); begin //防止小键盘显示不正确 //因为有可能主窗体激活事件和小键盘按钮点击事件接连发生 if MouseOnKeyPad then Exit; if KeyPadForm.Showing then KeyPadForm.Hide; end;
procedure TMainForm.KeyPadBtnMouseEnter(Sender: TObject); begin MouseOnKeyPad := True; end;
procedure TMainForm.KeyPadBtnMouseLeave(Sender: TObject); begin MouseOnKeyPad := False; end;
7.现在小键盘可以使用了,但是密码框还是不行 Delphi(Pascal) code procedure TMainForm.PasswordEditKeyPress(Sender: TObject; var Key: Char); begin case Ord(Key) of 8: DeletePasswordStr;//是BackSpace键的话就删除选中的字符 13: BtnClick;//是回车的话就点击按钮 //如果字符被允许则增加 else if IsAllowedPasswordChar(Key) then AddPasswordChar(Key); end; Key := Chr(0); end;
procedure TMainForm.DeletePasswordStr; var SelStart: Integer; SelLength: Integer; begin SelStart := PasswordEdit.SelStart; SelLength := PasswordEdit.SelLength; //删除TruePassword对应字符串 if SelLength <> 0 then Delete(TruePassword, SelStart + 1, SelLength) else if SelStart <> 0 then Delete(TruePassword, SelStart, 1) else MessageBeep(MB_OK); //同步TruePassword长度与密码框中●的个数 PasswordEdit.Text := CreatePasswordStrByCount(Length(TruePassword)); if SelLength <> 0 then PasswordEdit.SelStart := SelStart else PasswordEdit.SelStart := SelStart - 1; end;
procedure TMainForm.AddPasswordChar(const aChar: Char); var SelStart: Integer; SelLength: Integer; begin SelStart := PasswordEdit.SelStart; SelLength := PasswordEdit.SelLength;
if SelLength <> 0 then Delete(TruePassword, SelStart + 1, SelLength) else if Length(TruePassword) = 16 then begin MessageBeep(MB_OK); Exit; end; Insert(aChar, TruePassword, SelStart + 1); //同步TruePassword长度与密码框中●的个数 PasswordEdit.Text := CreatePasswordStrByCount(Length(TruePassword)); ; PasswordEdit.SelStart := SelStart + 1; end;
function CreatePasswordStrByCount(aPasswordCharCount: Integer): string; var i: Integer; begin Result := ''; for I := 0 to aPasswordCharCount - 1 do Result := Result + '●'; end;