您也可以在自定義對話框中使用列表視圖。
List view dialog box 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;
,然後就這樣稱呼它
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;
這樣的列都保證保持一致,你不必擔心片停止。
List view dialog box http://privat.rejbrand.se/lvdlg2.png
鼠標懸停:
List view dialog box http://privat.rejbrand.se/lvdlg3.png
等寬看起來嚇人,但定製對話是解決方案都是一樣的 – 2011-01-10 17:15:52
好了,你讓我相信,我會與Adreas的解決方案去。 – 2011-01-11 09:42:37