InputQuery 格式问题

InputQuery Formatting Issues

我在 Delphi XE2 上使用 InputQuery/InputBox 时遇到问题。

输入区域错位(应该在文本下方)。

有没有办法在制作我自己的输入表单之前重新对齐它?

谢谢!

InputQuery() 并非设计为以这种方式使用。提示文本是显示在文本字段左侧的短标签(类似于 TLabeledEdit)。它并非旨在像您尝试的那样在提示上方显示说明。通过简单地使用您想要的任何控件和布局创建您自己的自定义表单,可以更好地处理这种情况。例如,使用 TDateTimePicker 表示日期和时间,TCheckBoxTRadioGroup 表示重复等。

然而,话虽这么说,InputQuery() 是使用自定义 VCL TForm 实现的,因此 技术上可能 可以完成您想要完成的任务达到。您可以使用 TScreen.OnActiveFormChange 事件在 Form 对象变为可见时获得对它的访问权限,然后您可以根据需要对其进行操作。例如:

procedure TMyForm.ActiveFormChanged(Sender: TObject);
var
  Form: TCustomForm;
  Prompt: TLabel;
  Edit: TEdit;
  Ctrl: TControl;
  I, J, ButtonTop: Integer;
begin
  Form := Screen.ActiveCustomForm;
  if (Form = nil) or (Form.ClassName <> 'TInputQueryForm') then Exit;

  for I := 0 to Form.ControlCount-1 do
  begin
    Ctrl := Form.Controls[i];
    if Ctrl is TLabel then
    begin
      Prompt := TLabel(Ctrl);
    end
    else if Ctrl is TEdit then
    begin
      Edit := TEdit(Ctrl);
    end;
  end;

  Edit.SetBounds(Prompt.Left, Prompt.Top + Prompt.Height + 5, Prompt.Width, Edit.Height);
  Form.ClientWidth := (Edit.Left * 2) + Edit.Width;
  ButtonTop := Edit.Top + Edit.Height + 15;

  J := 0;
  for I := 0 to Form.ControlCount-1 do
  begin
    Ctrl := Form.Controls[i];
    if Ctrl is TButton then
    begin
      Ctrl.SetBounds(Form.ClientWidth - ((Ctrl.Width + 15) * (2-J)), ButtonTop, Ctrl.Width, Ctrl.Height);
      Form.ClientHeight := Ctrl.Top + Ctrl.Height + 13;
      Inc(J);
    end;
  end;
end;

procedure TMyForm.DoSomething;
var
  value: string;
begin
  Screen.OnActiveFormChange := ActiveFormChanged;
  try
    InputQuery('Enter New Schedule', 'Format: <Second> <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>.'#10'Use * for repeating cycles. ex: 0 0 7 * * * * (trigger at 7AM everyday)', value);
  finally
    Screen.OnActiveFormChange := nil;
  end;
end;

或者:

class procedure TScreenEvents.ActiveFormChanged(Sender: TObject);
var
  Form: TCustomForm;
  Instructions: TLabel;
  Ctrl: TControl;
  I, J, K, Offset: Integer;
begin
  Form := Screen.ActiveCustomForm;
  if (Form = nil) or (Form.ClassName <> 'TInputQueryForm') then Exit;

  for I := 0 to Form.ControlCount-1 do
  begin
    Ctrl := Form.Controls[I];
    if Ctrl is TLabel then
    begin
      Instructions := TLabel.Create(Form);
      Instructions.Parent := Form;
      Instructions.Caption := 'Format: <Second> <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>.'#10'Use * for repeating cycles. ex: 0 0 7 * * * * (trigger at 7AM everyday)';
      Instructions.SetBounds(Ctrl.Left, Ctrl.Top, Instructions.Width, Instructions.Height);

      Offset := Instructions.Top + Instructions.Height + 5;
      Form.ClientWidth := Instructions.Width + (Instructions.Left * 2);

      K := 0;
      for J := 0 to Form.ControlCount-1 do
      begin
        Ctrl := Form.Controls[J];
        if Ctrl <> Instructions then
        begin
          Ctrl.Top := Ctrl.Top + Offset;
          if Ctrl is TEdit then
          begin
            Ctrl.Width := (Form.ClientWidth - Ctrl.Left - Instructions.Left);
          end
          else if Ctrl is TButton then
          begin
            Ctrl.Left := (Form.ClientWidth - (Ctrl.Width + 5) * (2-K));
            Inc(K);
          end;
        end;
      end;

      Form.ClientHeight := Form.ClientHeight + Offset;
      Break;
    end;
  end;
end;

procedure TMyForm.DoSomething;
var
  value: string;
begin
  Screen.OnActiveFormChange := ActiveFormChanged;
  try
    InputQuery('Enter New Schedule', 'Value', value);
  finally
    Screen.OnActiveFormChange := nil;
  end;
end;