发布于2010-05-17 16:22
最佳答案
inputbox封装的是InputQuery函数,你可以查看InputQuery的源代码,发现在InputQuery函数里面动态创建了一个窗体,你可以把InputQuery函数的源代码复制出来,自己修改成可控制位置的就行了
unit inputboxExs;
interface
uses Windows, Graphics, Forms, StdCtrls, Consts, Dialogs, Controls;
function InputQueryEx(const ACaption, APrompt: string;
var Value: string; X, Y: Integer): Boolean;
function InputBoxEx(const ACaption, APrompt, ADefault: string; X, Y: Integer): string;
implementation
function GetAveCharSize(Canvas: TCanvas): TPoint;
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;
function InputQueryEx(const ACaption, APrompt: string;
var Value: string; X, Y: Integer): 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);
Left := X;
Top := Y;
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;
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;
function InputBoxEx(const ACaption, APrompt, ADefault: string; X, Y: Integer): string;
begin
Result := ADefault;
InputQueryEx(ACaption, APrompt, Result, X, Y);
end;
以上是源代码,在Delphi源代码基础上稍微修改了下,我单独写在了一个文件中,使用时需要引用我这个单元文件,下面是调用示例:
uses inputboxExs;
procedure TForm1.Button2Click(Sender: TObject);
var str1, str2: string;
begin
// 参数中的X,Y,就是你想设置的位置
str1 := InputBoxEx('注意', '输入数字', str2, 100, 10);
ShowMessage(str1);
end;
其他回答
呵呵,这个有意思,我学了一下,哈哈
changjeff | 发布于2010-05-16 12:32
0 0
为您推荐:
delphi的相关知识
- 2009-12-29Delphi控件太多!!! 32
- 2010-12-13DELPHI连接数据库问题 38
- 2010-02-12DELPHI 的程序很大 9
- 2006-12-31delphi 字符串长度 14
- 2008-10-28delphi查找控件 12
登录
还没有百度账号?
立即注册
delphi lazarus opengl
网页操作自动化, 图像分析破解,游戏开发