好久没写代码了,更久没上博客园的博客了,无聊写几行试一下新语法。 1 unit Main; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, iHome.Helper.Telegram,
Generics.Collections; type
TMainForm = class(TForm)
ListView_Queue: TListView;
btEnQueue: TButton;
btDeQueue: TButton;
procedure btEnQueueClick(Sender: TObject);
private
{ Private declarations }
FPriorityQueue:TQueue<PTelegram>; procedure SetupListView(ListView:TListView);
procedure ViewQueue(ListView:TListView;PriorityQueue:TQueue<PTelegram>);
public
{ Public declarations }
end; var
MainForm: TMainForm; implementation {$R *.dfm} procedure TMainForm.SetupListView(ListView: TListView);
procedure SetupListViewColumns(Columns:TListColumns);
begin
Columns.Clear;
TListColumn(Columns.Add).Caption:='Caption';
TListColumn(Columns.Add).Caption:='Message';
TListColumn(Columns.Add).Caption:='Sender';
TListColumn(Columns.Add).Caption:='Receiver';
TListColumn(Columns.Add).Caption:='DispatchTime';
TListColumn(Columns.Add).Caption:='ExtraInfo';
end;
begin
ListView.ViewStyle:=vsReport;//报表格式显示
SetupListViewColumns(ListView.Columns); //设置显示列名
end; procedure TMainForm.ViewQueue(ListView:TListView;PriorityQueue:TQueue<PTelegram>);
var
FpTelegram:PTelegram;
procedure ViewQueueItem(ListItem:TListItem);
begin
with ListItem do
begin
Caption := IntToStr(FpTelegram^.msg);
SubItems.Add(IntToStr(FpTelegram^.msg));
SubItems.Add(IntToStr(FpTelegram^.Sender));
SubItems.Add(IntToStr(FpTelegram^.Receiver));
SubItems.Add(IntToStr(FpTelegram^.DispatchTime));
SubItems.Add(IntToStr(FpTelegram^.ExtraInfo));
end;
end;
begin
try
ListView.Items.BeginUpdate;
ListView.Clear;
SetupListView(ListView);//设置ListView显示设置
for FpTelegram in PriorityQueue do ViewQueueItem(ListView.Items.Add);//显示队列项目
finally
ListView.Items.EndUpdate
end;
end; procedure TMainForm.btEnQueueClick(Sender: TObject);
var
FTelegram:PTelegram; function MakerQueue(Max:Integer):TQueue<PTelegram>;
var
i:Integer;
begin
Result:=TQueue<PTelegram>.Create;//创建优先队列
for I := to Max do
begin
New(FTelegram);
with FTelegram^ do
begin
DispatchTime:=GetTickCount;
Sender:=Self.Handle;
Receiver:=Self.Handle;
Msg:=i;
ExtraInfo:=;
end;
Result.Enqueue(FTelegram);//入队
end;
end; begin
if FPriorityQueue=nil then
FPriorityQueue:=MakerQueue();//创建15个队列项目
ViewQueue(ListView_Queue,FPriorityQueue);//ListView显示队列
end; end.