請問有人可以解釋一下listview sorting???? |
尚未結案
|
mahoihei
一般會員 發表:6 回覆:6 積分:2 註冊:2009-08-10 發送簡訊給我 |
之前我問過listview sorting 也成功了,可是我不明白其中的功能,有人可以詳細解釋一下嗎,因為我的program是要寫report的,謝謝
以下附上code var ColumnToSort: Integer; procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); begin ColumnToSort := Column.Index; (Sender as TCustomListView).AlphaSort; end; // 寫 OnCompare 事件 procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer); var ix: Integer; begin if ColumnToSort = 0 then Compare := CompareText(Item1.Caption,Item2.Caption) else begin ix := ColumnToSort - 1; Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]); end; end; 不明白到方 1.compare什麼時候觸發 2.(Sender as TCustomListView).AlphaSort; 這句到底是什麼?? 3.CompareText()這個function又是什麼來的?? |
小傑克
資深會員 發表:5 回覆:209 積分:357 註冊:2009-02-16 發送簡訊給我 |
請善用說明
This example shows how to use the OnColumnClick and OnCompare events of a list view to let users sort the columns in a report-style list view by clicking on the column headers. This requires a global variable to keep track of the column that was clicked: var ColumnToSort: Integer; The OnColumnClick event handler sets the global variable to indicate the column to sort and calls AlphaSort: procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); begin ColumnToSort := Column.Index; (Sender as TCustomListView).AlphaSort; end; The OnCompare event handler causes the list view to sort on the selected column: procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer); var ix: Integer; begin if ColumnToSort = 0 then Compare := CompareText(Item1.Caption,Item2.Caption) else begin ix := ColumnToSort - 1; Compare := CompareText(Item1.SubItems[ix],Item2.SubItems[ix]); end; end; Note: This OnCompare event handler uses the global CompareText function. An application may want to use AnsiCompareText, CompareStr, or AnsiCompareStr instead, depending on whether the comparison should be case-sensitive and whether the locale should be considered.
------
額有朝天骨,眼中有靈光 |
mahoihei
一般會員 發表:6 回覆:6 積分:2 註冊:2009-08-10 發送簡訊給我 |
|
小傑克
資深會員 發表:5 回覆:209 積分:357 註冊:2009-02-16 發送簡訊給我 |
要回答下面問題要先知道ListView 是ComCtrl 底層運作方式透過SendMessage, 詳細部份請自行trace delphi source , 寫到完天都黑了
1.compare什麼時候觸發 --> 呼叫AlphaSort 時觸發 2.(Sender as TCustomListView).AlphaSort; 這句到底是什麼?? --> 將 Sender 轉型成 TCustomListView 然後呼叫AlphaSort 3.CompareText()這個function又是什麼來的?? --> 字串比對, 是delphi提供的function Compares two strings by ordinal value without case sensitivity. CompareText compares S1 and S2 and returns 0 if they are equal. If S1 is greater than S2, CompareText returns an integer greater than 0. If S1 is less than S2, CompareText returns an integer less than 0. CompareText is not case sensitive and is not affected by the current locale.
------
額有朝天骨,眼中有靈光
編輯記錄
小傑克 重新編輯於 2009-08-19 17:00:14, 註解 無‧
|
mahoihei
一般會員 發表:6 回覆:6 積分:2 註冊:2009-08-10 發送簡訊給我 |
|
小傑克
資深會員 發表:5 回覆:209 積分:357 註冊:2009-02-16 發送簡訊給我 |
關於AlphaSort底下這邊看不懂我也沒辦法講的更詳細了
function TCustomListView.AlphaSort: Boolean; begin if HandleAllocated then Result := ListView_SortItems(Handle, @DefaultListViewSort, 0) else Result := False; end; function ListView_SortItems(hwndLV: HWND; pfnCompare: TLVCompare; lPrm: Longint): Bool; begin Result := Bool( SendMessage(hwndLV, LVM_SORTITEMS, lPrm, Longint(@pfnCompare)) ); end; 最終就是call windows 的api SendMessage ===================引 用 mahoihei 文 章=================== AlphaSort呢 到底是什麼???這個function是做什麼的
------
額有朝天骨,眼中有靈光 |
Ktop_Robot
站務副站長 發表:0 回覆:3511 積分:0 註冊:2007-04-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |