佇列與DelphiXe新語法

雲水浮萍發表於2014-12-20
好久沒寫程式碼了,更久沒上部落格園的部落格了,無聊寫幾行試一下新語法。

1
unit Main; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, 8 9 iHome.Helper.Telegram, 10 Generics.Collections; 11 12 type 13 TMainForm = class(TForm) 14 ListView_Queue: TListView; 15 btEnQueue: TButton; 16 btDeQueue: TButton; 17 procedure btEnQueueClick(Sender: TObject); 18 private 19 { Private declarations } 20 FPriorityQueue:TQueue<PTelegram>; 21 22 procedure SetupListView(ListView:TListView); 23 procedure ViewQueue(ListView:TListView;PriorityQueue:TQueue<PTelegram>); 24 public 25 { Public declarations } 26 end; 27 28 var 29 MainForm: TMainForm; 30 31 implementation 32 33 {$R *.dfm} 34 35 36 procedure TMainForm.SetupListView(ListView: TListView); 37 procedure SetupListViewColumns(Columns:TListColumns); 38 begin 39 Columns.Clear; 40 TListColumn(Columns.Add).Caption:='Caption'; 41 TListColumn(Columns.Add).Caption:='Message'; 42 TListColumn(Columns.Add).Caption:='Sender'; 43 TListColumn(Columns.Add).Caption:='Receiver'; 44 TListColumn(Columns.Add).Caption:='DispatchTime'; 45 TListColumn(Columns.Add).Caption:='ExtraInfo'; 46 end; 47 begin 48 ListView.ViewStyle:=vsReport;//報表格式顯示 49 SetupListViewColumns(ListView.Columns); //設定顯示列名 50 end; 51 52 procedure TMainForm.ViewQueue(ListView:TListView;PriorityQueue:TQueue<PTelegram>); 53 var 54 FpTelegram:PTelegram; 55 procedure ViewQueueItem(ListItem:TListItem); 56 begin 57 with ListItem do 58 begin 59 Caption := IntToStr(FpTelegram^.msg); 60 SubItems.Add(IntToStr(FpTelegram^.msg)); 61 SubItems.Add(IntToStr(FpTelegram^.Sender)); 62 SubItems.Add(IntToStr(FpTelegram^.Receiver)); 63 SubItems.Add(IntToStr(FpTelegram^.DispatchTime)); 64 SubItems.Add(IntToStr(FpTelegram^.ExtraInfo)); 65 end; 66 end; 67 begin 68 try 69 ListView.Items.BeginUpdate; 70 ListView.Clear; 71 SetupListView(ListView);//設定ListView顯示設定 72 for FpTelegram in PriorityQueue do ViewQueueItem(ListView.Items.Add);//顯示佇列專案 73 finally 74 ListView.Items.EndUpdate 75 end; 76 end; 77 78 procedure TMainForm.btEnQueueClick(Sender: TObject); 79 var 80 FTelegram:PTelegram; 81 82 function MakerQueue(Max:Integer):TQueue<PTelegram>; 83 var 84 i:Integer; 85 begin 86 Result:=TQueue<PTelegram>.Create;//建立優先佇列 87 for I := 0 to Max do 88 begin 89 New(FTelegram); 90 with FTelegram^ do 91 begin 92 DispatchTime:=GetTickCount; 93 Sender:=Self.Handle; 94 Receiver:=Self.Handle; 95 Msg:=i; 96 ExtraInfo:=0; 97 end; 98 Result.Enqueue(FTelegram);//入隊 99 end; 100 end; 101 102 begin 103 if FPriorityQueue=nil then 104 FPriorityQueue:=MakerQueue(15);//建立15個佇列專案 105 ViewQueue(ListView_Queue,FPriorityQueue);//ListView顯示佇列 106 end; 107 108 end.

 

相關文章