InputBox 中输入密码 显示为 *?(100分)

  • 主题发起人 主题发起人 汉鸭子
  • 开始时间 开始时间

汉鸭子

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何在InputBox 中输入密码 使其显示为 *?
 
这个可能不行, 要么你自己写一个,
要么改写原代码(这个函数的原代码很简单)
 
把分给我,因为我会给你0距离的帮助。
以下代码修改自Delphi原来的Dialogs单元,如果你想原来一样用,只需将其覆盖原来的函数
即可,否则在使用时,加上uses xInput;

unit xInput;

interface
uses Windows, Messages;

function xInputBox(ACaption, APrompt : String; ADefault: string=''; PwChar:Char=#0): string;
function xInputQuery(ACaption, APrompt: string;
var Value: string; PwChar:Char=#0): Boolean;

implementation

function xInputBox(ACaption, APrompt : String; ADefault: string; PwChar:Char): string;
begin
Result := ADefault;
xInputQuery(ACaption, APrompt, Result);
end;

function xInputQuery(ACaption, APrompt: string; var Value: string; PwChar:Char): Boolean;
var
wClass: TWndClass; // class struct for main window
hFont, // handle of font
hInst, // handle of program (hinstance)
hHandle, // Handle of main window
hOkBtn, // Handle of encrypt button
hCancelBtn, // handle of decrypt button
hEdit, // handle of main edit
hLabel : HWND; // handle of password label
Msg: TMSG; // message struct
dOK, dCancel : Pointer; // default button procedures
ButtonTop, ButtonWidth, ButtonHeight: Integer;

procedure Resize;//移动窗体
var RCT:TRect;
begin
GetWindowRect(hHandle,RCT);
MoveWindow(hEdit,230,5,RCT.Right-RCT.Left-245,24,True);
end;

procedure ShutDown;//关闭
begin
DeleteObject(hFont);
UnRegisterClass('xInputBox',hInst);
ExitProcess(hInst); //end program
end;
//响应消息
function WindowProc(hWnd,Msg,wParam,lParam:Longint):Longint; stdcall;
begin
// Always pass the message to the Default procedure
Result:=DefWindowProc(hWnd,Msg,wParam,lParam);

case Msg of
WM_SIZE: Resize;
// When buttons are clicked the message is passed to
// the parent window, so we handle it here.
WM_COMMAND: if lParam=hOkBtn then ShutDown
else if lParam=hCancelBtn then ShutDown;
WM_DESTROY: ShutDown;
end;
end;

function OnOk(hWnd,Msg,wParam,lParam:Longint):Longint; stdcall;
var
i: Integer;
begin
// Always pass the message to the Default procedure
Result:=CallWindowProc(dOk,hWnd,Msg,wParam,lParam);

case Msg of
// If the user presses TAB we're gunna switch the
// focus over to the Decrypt button.
WM_KEYDOWN: if wParam=9 then SetFocus(hCancelBtn);
end;
end;

// This function processes every message sent to the Decrypt Button
function OnCancel(hWnd,Msg,wParam,lParam:Longint):Longint; stdcall;
begin
// Always pass the message to the Default procedure
Result:=CallWindowProc(dCancel,hWnd,Msg,wParam,lParam);

case Msg of
// if the user presses TAB we're gunna switch
// the focus back to the Encrypt button.
WM_KEYDOWN: if wParam=9 then SetFocus(hOkBtn);
end;
end;

begin
Result := False;
if ACaption='' then ACaption := '请输入';
if APrompt='' then APrompt := '请在下面的文本框中输入:';
hInst:=GetModulehandle(nil); // 获取程序句柄

with wClass do
begin
Style:= CS_PARENTDC;
hIcon:= LoadIcon(hInst,'MAINICON');
lpfnWndProc:= @WindowProc;
hInstance:= hInst;
hbrBackground:= COLOR_BTNFACE+1;
lpszClassName:= 'xInputBox';
hCursor:= LoadCursor(0,IDC_ARROW);
end;

// Once our class is registered we
// can start making windows with it
RegisterClass(wClass);

//建立主窗口
hHandle:=CreateWindow(
'xInputBox', // Registered Class Name
PChar(ACaption), // Title of Window
WS_OVERLAPPEDWINDOW or // Basic Window Style
WS_VISIBLE, // Make it Visible
10, 10, 400, 300, // Left Top Width Height
0, 0, // Parent Window and Menu handle
hInst, // Application Instance
nil); // Structure for Creation Data

//创建确定按钮
hOkBtn:=CreateWindow(
'Button', '确定(&O)',
WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT,
5,5,65,24,hHandle,0,hInst,nil);

//创建取消按钮
hCancelBtn:=CreateWindow(
'Button', '取消(&C)',
WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT,
75,5,65,24,hHandle,0,hInst,nil);

//创建输入框
hEdit:=CreateWindowEx(
WS_EX_CLIENTEDGE,
'Edit', '',
WS_VISIBLE or WS_CHILD or ES_LEFT or ES_AUTOHSCROLL or ES_PASSWORD,
230,5,155,24,hHandle,0,hInst,nil);

//创建提示条
hLabel:=CreateWindow(
'Static', PChar(APrompt),
WS_VISIBLE or WS_CHILD or SS_LEFT,
160,10,70,20,hHandle,0,hInst,nil);

//为窗口创建自定字体,否则将使用系统字体
hFont:=CreateFont(-12, 0, 0, 0, //高度 宽度 旋转角度 方向
0, 0, 0, 0, //Weight 斜体 下线 // Strike Out // Italic
DEFAULT_CHARSET, // Char Set
OUT_DEFAULT_PRECIS, // Precision
CLIP_DEFAULT_PRECIS, // Clipping
DEFAULT_QUALITY, // Render Quality
DEFAULT_PITCH or FF_DONTCARE, // Pitch & Family
'宋体'); // Font Name

// Set the fonts for all our controls
SendMessage(hOkBtn,WM_SETFONT,hFont,0);
SendMessage(hCancelBtn,WM_SETFONT,hFont,0);
SendMessage(hEdit,WM_SETFONT,hFont,0);
SendMessage(hLabel,WM_SETFONT,hFont,0);

// Subclass Encrypt Button (assign it a custom WindowProc)
dOk:=Pointer(GetWindowLong(hOkBtn,GWL_WNDPROC));
SetWindowLong(hOkBtn,GWL_WNDPROC,Longint(@OnOk));

// Subclass Decrypt Button
dCancel:=Pointer(GetWindowLong(hCancelBtn,GWL_WNDPROC));
SetWindowLong(hCancelBtn,GWL_WNDPROC,Longint(@OnCancel));

// Focus on first control (otherwise people with no mouse are screwed)
SetFocus(hOkBtn);

// Now we loop GetMessage to process each Message in
// our main window's message list. Every time the main
// window recieves a message its added to the list, so
// this loop here will eventually process it.
while(GetMessage(Msg,hHandle,0,0))do
begin
TranslateMessage(Msg); // Translate any keyboard Msg's
DispatchMessage(Msg); // Send it to our WindowProc
end; // for processing.
end;

end.
 
执行XInputBox 过程没有响应?
 
给我吧:

unit InputBoxX;

interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
StdCtrls, Buttons;

function InputQuery(const ACaption, APrompt: string;
var Value: string): Boolean;

implementation

function GetAveCharSize(Canvas: TCanvas): TPoint;
var
I: Integer;
Buffer: array[0..51] of Char;
begin
for I := 0 to 25 do Buffer := 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;

function InputQuery(const ACaption, APrompt: string;
var Value: string): Boolean;
var
Form: TForm;
Prompt: TLabel;
Edit: TEdit;
DialogUnits: TPoint;
ButtonLeft, ButtonTop, ButtonWidth, ButtonHeight: Integer;
begin
Result := False;
Form := TForm.Create(Application);
with Form do
try
Font.Name:='宋体';
Font.Size:=9;
Canvas.Font := Font;
DialogUnits := GetAveCharSize(Canvas);
BorderStyle := bsDialog;
Caption := ACaption;
ClientWidth := MulDiv(180, DialogUnits.X, 4);
ClientHeight := MulDiv(63, DialogUnits.Y, 8);
Position := poScreenCenter;
Prompt := TLabel.Create(Form);
with Prompt do
begin
Parent := Form;
AutoSize := True;
Left := MulDiv(8, DialogUnits.X, 4);
Top := MulDiv(8, DialogUnits.Y, 8);
Caption := APrompt;
end;
Edit := TEdit.Create(Form);
with Edit do
begin
Parent := Form;
Left := Prompt.Left;
Top := MulDiv(19, DialogUnits.Y, 8);
Width := MulDiv(164, DialogUnits.X, 4);
PasswordChar := '*';
MaxLength := 255;
Text := Value;
SelectAll;
end;
ButtonTop := MulDiv(41, DialogUnits.Y, 8);
ButtonWidth := MulDiv(50, DialogUnits.X, 4);
ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
ButtonLeft:=Edit.Left+Edit.Width-ButtonWidth;
with TButton.Create(Form) do
begin
Parent := Form;
Caption := '取消(&C)';
ModalResult := mrCancel;
Cancel := True;
SetBounds(ButtonLeft, ButtonTop, ButtonWidth,
ButtonHeight);
ButtonLeft:=ButtonLeft-ButtonWidth-5;
end;
with TButton.Create(Form) do
begin
Parent := Form;
Caption := '确定(&O)';
ModalResult := mrOk;
Default := True;
SetBounds(ButtonLeft, ButtonTop, ButtonWidth,
ButtonHeight);
end;
if ShowModal = mrOk then
begin
Value := Edit.Text;
Result := True;
end;
finally
Form.Free;
end;
end;


end.


//调用时如下:


var
S: String;
begin
if InputBoxX.InputQuery('输入', '请输入密码:', S) then
begin
//Do something...
end;
end;
 
这个单元我一直在用,没有任何问题。调用方式如下:
str := xInputBox('标题', '根据提示输入:'; '此为默认值'; '*');
//最后一个字符是什么,则字符显示为什么。
 
Wqhuo,好使,这是我全部的分了
 
WiseAnt:
还是没响应,可能你的代码有些遗漏,比如:调用xInputQuery时好象少了个参数 PwChar;
 
后退
顶部