如何在ShowMessage中显示表?

Jen*_*off 3 delphi fonts dialog delphi-2007

我试图使用ShowMessage显示一个表,如下所示:

short            | Description for "short"
verylongtext     | Description for "verylongtext"
Run Code Online (Sandbox Code Playgroud)

如何在简单的消息对话框中获得两个正确对齐的列?

我尝试使用空格对齐列,但ShowMessage的字体是可变的.然后我尝试使用制表符对齐它们,但我不知道如何计算每行的正确制表符数.

有没有可靠的方法来计算标签数量?

PS:我想避免为此目的编写自定义对话框.

And*_*and 9

您也可以在自定义对话框中使用列表视图.

列表视图对话框http://privat.rejbrand.se/lvdlg.png

尝试

type StringArray = array of string;

procedure ShowMemoMessage(AOwner: TForm; const Caption: string; Col1, Col2: StringArray; const DialogType: TMsgDlgType = mtInformation; const DlgWidth: integer = 360; const DlgHeight: integer = 200);
var
  dlg: TForm;
  lv: TListView;
  btn: TButton;
  IconType: PChar;
  icon: HICON;
  image: TImage;
  sh: TShape;
  bvl: TBevel;
  i: Integer;
begin

  if length(Col1) <> length(Col2) then
    raise Exception.Create('The length of the columns doesn''t match.');

  dlg := TForm.Create(AOwner);
  try
    dlg.BorderStyle := bsDialog;
    dlg.Caption := Caption;
    dlg.Width := DlgWidth;
    dlg.Height := DlgHeight;
    dlg.Position := poScreenCenter;
    btn := TButton.Create(dlg);
    btn.Parent := dlg;
    btn.Caption := 'OK';
    btn.ModalResult := mrOk;
    btn.Left := dlg.ClientWidth - btn.Width - 8;
    btn.Top := dlg.ClientHeight - btn.Height - 8;
    lv := TListView.Create(dlg);
    lv.Parent := dlg;
    lv.Color := dlg.Color;
    lv.ReadOnly := true;
    lv.BorderStyle := bsNone;
    lv.Left := 8;
    lv.Top := 8;
    lv.Width := dlg.ClientWidth - 16;
    lv.Height := dlg.ClientHeight - 16 - 8 - 4 - btn.Height;
    lv.ViewStyle := vsReport;
    lv.RowSelect := true;
    with lv.Columns.Add do
    begin
      Caption := 'Name';
      Width := 150;
    end;
    lv.Columns.Add.Caption := 'Value';
    lv.ShowColumnHeaders := false;

    for i := 0 to high(Col1) do
      with lv.Items.Add do
      begin
        Caption := Col1[i];
        SubItems.Add(Col2[i]);
      end;

    sh := TShape.Create(dlg);
    sh.Parent := dlg;
    sh.Align := alBottom;
    sh.Shape := stRectangle;
    sh.Pen.Color := clWhite;
    sh.Brush.Color := clWhite;
    sh.Height := btn.Height + 16;

    bvl := TBevel.Create(dlg);
    bvl.Parent := dlg;
    bvl.Align := alBottom;
    bvl.Height := 2;
    bvl.Style := bsLowered;

    case DialogType of
      mtWarning:
        begin
          MessageBeep(MB_ICONWARNING);
          IconType := IDI_WARNING;
        end;
      mtError:
        begin
          MessageBeep(MB_ICONERROR);
          IconType := IDI_ERROR;
        end;
      mtInformation:
        begin
          MessageBeep(MB_ICONINFORMATION);
          IconType := IDI_INFORMATION;
        end;
      mtConfirmation:
        begin
          MessageBeep(MB_ICONQUESTION);
          IconType := IDI_QUESTION;
        end;
      mtCustom: {silence};
    end;

    if DialogType <> mtCustom then
    begin
      image := TImage.Create(dlg);
      image.Parent := dlg;
      icon := LoadIcon(0, IconType);
      image.AutoSize := true;
      image.Picture.Icon.Handle := icon;
      image.Left := 16;
      image.Top := 16;
      lv.Left := image.Width + 32;
      lv.Top := 16;
      lv.Height := lv.Height - 8;
    end;

    dlg.ShowModal;
  finally
    dlg.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

然后就这样称呼它

procedure TForm1.FormCreate(Sender: TObject);
var
  col1, col2: StringArray;
begin

  SetLength(col1, 4);
  col1[0] := 'alpha'; col1[1] := 'beta'; col1[2] := 'gamma'; col1[3] := 'delta';

  SetLength(col2, 4);
  col2[0] := 'yes'; col2[1] := 'no'; col2[2] := 'no'; col2[3] := 'no';

  ShowMemoMessage(Self, 'Test', col1, col2);

end;
Run Code Online (Sandbox Code Playgroud)

这样,列保证保持对齐,您不必担心制表位.

列表视图对话框http://privat.rejbrand.se/lvdlg2.png

鼠标移到:

列表视图对话框http://privat.rejbrand.se/lvdlg3.png


Gol*_*rol 6

如果您没有为此编写自定义对话框,您何时会这样做?这并不难.只需创建一个表单,在其上放一个TMemo并只读取该备忘录.您可以设置像Courier New这样的等宽字体,您的问题就解决了.你也可以获得滚动条和选择的优势,你可以选择使它成为非模态的.

我甚至建议在网格中显示这种类型的数据(如TStringGrid)而不是备忘录或标签.

计算如何在消息框中显示此文本比仅创建自定义对话框需要更多的努力.