delphi中的inpubox,如何能控制它的位置?
发布网友
发布时间:2022-12-16 17:09
我来回答
共2个回答
热心网友
时间:2024-12-05 07:26
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;
procere TForm1.Button2Click(Sender: TObject);
var str1, str2: string;
begin
// 参数中的X,Y,就是你想设置的位置
str1 := InputBoxEx('注意', '输入数字', str2, 100, 10);
ShowMessage(str1);
end;
热心网友
时间:2024-12-05 07:27
呵呵,这个有意思,我学了一下,哈哈