实现带密码掩饰的Input窗口
Dialogs单元中有个InputQuery函数,能显示一个输入框,获取用户输入文本,返回一个布尔值。
function InputQueryEx(const ACaption, APrompt: string; var Value: string):Boolean;
今天看《Delphi5程序员指南》第4章P107中有个实例程序:在DPR文件中增加代码,其中用到了InputQuery。
作用是显示一个InputQuery窗体,输入密码,正确运行程序,错误退出。发现一个问题,密码是明码显示的,没有
mask,所以想到能否做出带mask效果的InputQuery。
有两个思路:第一是看InputQuery的源码,拿来修改一下,生成一个自己的common单元文件,以后可以方便调用。
第二是面向对象的方法:继承Dialogs类,覆盖InputQuery方法,修改自己想要实现的功能。
第一个思路很简单也直接,拿来用就可以。第二个思路就需要斟酌一下,首先是InputQuery能否被override,其次如果可以的话怎样具体修改。
以目前本人的功底,还是比较难实现第二个思路,故先按照思路一解决问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | unit InputQueryExs; //通过修改Dialogs单元中InputQuery函数,实现带密码掩饰的Input窗口。 interface function InputQueryEx( const ACaption, APrompt: string ; var Value: string ; PassWordMask: char =chr( 0 )): Boolean ; //PassWordMask:char=chr(0)默认还是显示内容 implementation uses Graphics,Windows, Controls, Forms, StdCtrls; function GetAveCharSize(Canvas: TCanvas): TPoint; {$IF DEFINED(CLR)} var I: Integer ; Buffer: string ; Size: TSize; begin SetLength(Buffer, 52 ); for I := 0 to 25 do Buffer[I + 1 ] := Chr(I + Ord( 'A' )); for I := 0 to 25 do Buffer[I + 27 ] := Chr(I + Ord( 'a' )); GetTextExtentPoint(Canvas . Handle, Buffer, 52 , Size); Result . X := Size . cx div 52 ; Result . Y := Size . cy; end ; { $ELSE } var I: Integer ; Buffer: array [ 0..51 ] of Char ; begin for I := 0 to 25 do Buffer[I] := Chr(I + Ord( 'A' )); for I := 0 to 25 do Buffer[I + 26 ] := Chr(I + Ord( 'a' )); GetTextExtentPoint(Canvas . Handle, Buffer, 52 , TSize(Result)); Result . X := Result . X div 52 ; end ; { $IFEND } function InputQueryEx( const ACaption, APrompt: string ; var Value: string ; PassWordMask: char =chr( 0 )): Boolean ; var Form: TForm; Prompt: TLabel; Edit: TEdit; DialogUnits: TPoint; ButtonTop, ButtonWidth, ButtonHeight: Integer ; begin Result := False ; Form := TForm . Create(Application); with Form do try Canvas . Font := Font; DialogUnits := GetAveCharSize(Canvas); BorderStyle := bsDialog; Caption := ACaption; ClientWidth := MulDiv( 180 , DialogUnits . X, 4 ); PopupMode := pmAuto; Position := poScreenCenter; Prompt := TLabel . Create(Form); with Prompt do begin Parent := Form; Caption := APrompt; Left := MulDiv( 8 , DialogUnits . X, 4 ); Top := MulDiv( 8 , DialogUnits . Y, 8 ); Constraints . MaxWidth := MulDiv( 164 , DialogUnits . X, 4 ); WordWrap := True ; end ; Edit := TEdit . Create(Form); with Edit do begin Parent := Form; Left := Prompt . Left; Top := Prompt . Top + Prompt . Height + 5 ; Width := MulDiv( 164 , DialogUnits . X, 4 ); MaxLength := 255 ; Text := Value; SelectAll; PasswordChar:=PasswordMask; //这里实现了密码掩饰 end ; ButtonTop := Edit . Top + Edit . Height + 15 ; ButtonWidth := MulDiv( 50 , DialogUnits . X, 4 ); ButtonHeight := MulDiv( 14 , DialogUnits . Y, 8 ); with TButton . Create(Form) do begin Parent := Form; Caption := '确定' ; //原先为SMsgDlgOK; ModalResult := mrOk; Default := True ; SetBounds(MulDiv( 38 , DialogUnits . X, 4 ), ButtonTop, ButtonWidth, ButtonHeight); end ; with TButton . Create(Form) do begin Parent := Form; Caption := '取消' ; //原先为SMsgDlgCancel; ModalResult := mrCancel; Cancel := True ; SetBounds(MulDiv( 92 , DialogUnits . X, 4 ), Edit . Top + Edit . Height + 15 , ButtonWidth, ButtonHeight); Form . ClientHeight := Top + Height + 13 ; end ; if ShowModal = mrOk then begin Value := Edit . Text; Result := True ; end ; finally Form . Free; end ; end ; end . |
几点心得:
第一,GetAveCharSize()我一开始没有复制过来,本以为引用Dialogs就可以,但不行,原因待查…
第二,函数缺省值参数的使用方法PassWordMask:char=chr(0) 值是跟在类型名后。
posted on 2010-10-30 22:55 Delphi7456 阅读(1684) 评论(0) 编辑 收藏 举报
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步