ListView实在是超级无敌复杂地难封装啊,Vista下的ListView简直是万能的,以至于我萌生了将grid和tree-grid都用ListView来实现的想法。当然这只是想一想,暂时不想做。这个ListView花了1700行代码封装完成,共有4个类,分别是item、column、group和listview,listview自己的事件16个。
本文内容有两幅示例以及.h、.cpp以及main函数代码
图1:初始化
图2:点击column并排序(按照所属column的文字)
代码1:使用
1 #include "..\..\..\..\VL++\Library\Windows\VL_WinGUI.h"
2 #include "..\..\..\..\VL++\Library\Data\VL_System.h"
3
4 using namespace vl;
5 using namespace vl::windows;
6 using namespace vl::system;
7
8 PWChar ListViewItemText[]={
9 L"Add Table",
10 L"Arrange Window",
11 L"Delete Table",
12 L"Graph",
13 L"High Light",
14 L"Home Page",
15 L"Open",
16 L"Paste",
17 L"Save",
18 L"Search"
19 };
20
21 PWChar ListViewItemText2[]={
22 L"Add a table into the system",
23 L"Arrange the windows of system",
24 L"Remove a table from the system",
25 L"Show the graph of the table relations",
26 L"High light a table",
27 L"Switch to home page",
28 L"Open a table",
29 L"Paste data from clipboard into a table",
30 L"Save a table",
31 L"Search information from tables"
32 };
33
34 class MyForm : public VL_WinForm
35 {
36 protected:
37 VUnicodeString FBitmapPath;
38 VL_WinImageList* FLargeImages;
39 VL_WinImageList* FSmallImages;
40 VL_WinListView* FListView;
41 VL_WinComboBox* FViewSelection;
42
43 void InitControls()
44 {
45 FBitmapPath=VFileName(GetApplication()->GetAppName()).GetPath().MakeAbsolute(L"..\\Bitmap\\").GetStrW();
46 FLargeImages=new VL_WinImageList(32,32);
47 FSmallImages=new VL_WinImageList(16,16);
48 for(VInt i=0;i<10;i++)
49 {
50 FLargeImages->Add(new VL_WinBitmap(FBitmapPath+L"b"+VUnicodeString(i)+L".bmp",true,true));
51 FSmallImages->Add(new VL_WinBitmap(FBitmapPath+L"s"+VUnicodeString(i)+L".bmp",true,true));
52 }
53
54 FListView=new VL_WinListView(this);
55 FListView->Move(10,50,380,340);
56 FListView->SetGroupImageList(FLargeImages);
57 FListView->SetSmallImageList(FSmallImages);
58 FListView->SetLargeImageList(FLargeImages);
59 FListView->AddColumn(L"Function");
60 FListView->AddColumn(L"Description",0);
61 FListView->GetColumn(1).SetWidth(200);
62 FListView->GetColumn(1).SetShowSplitButton(true);
63 VInt GroupA=FListView->AddGroup(L"Group A");
64 FListView->GetGroupByID(GroupA).SetFooter(L"Footer of group A");
65 FListView->GetGroupByID(GroupA).SetSubTitle(L"Title of group A");
66 FListView->GetGroupByID(GroupA).SetTask(L"Task of group A");
67 FListView->GetGroupByID(GroupA).SetImageIndex(0);
68 VInt GroupB=FListView->AddGroup(L"Group B");
69 VL_List<VInt , true> Subs;
70 Subs.Add(0);
71 for(VInt i=0;i<10;i++)
72 {
73 FListView->AddItem(ListViewItemText[i],i);
74 FListView->GetItem(i).SetSubItem(0,ListViewItemText2[i]);
75 FListView->GetItem(i).SetTiledSubItem(Subs);
76 FListView->GetItem(i).EnterGroup(i<5?GroupA:GroupB);
77 }
78 FListView->EnableGrouping(true);
79 FListView->SetShowCheckBoxes(true);
80 FListView->SetAutoCheckSelect(true);
81 FListView->OnCompareItems.Bind(this,&MyForm::ListView_OnCompareItem);
82 FListView->OnColumnClick.Bind(this,&MyForm::ListView_OnColumnClick);
83 FListView->OnGroupTaskClick.Bind(this,&MyForm::ListView_OnGroupTaskClick);
84
85 FViewSelection=new VL_WinComboBox(this,true);
86 FViewSelection->Move(10,10,120,30);
87 FViewSelection->AddString(L"Large Icon");
88 FViewSelection->AddString(L"Small Icon");
89 FViewSelection->AddString(L"List");
90 FViewSelection->AddString(L"Report");
91 FViewSelection->AddString(L"Tile");
92 FViewSelection->OnSelChange.Bind(this,&MyForm::ViewSelection_OnSelChange);
93 FViewSelection->SetSelectedIndex(3);
94
95 FListView->SetFocused();
96 }
97
98 void ViewSelection_OnSelChange(VL_Base* Sender)
99 {
100 switch(FViewSelection->GetSelectedIndex())
101 {
102 case 0:
103 FListView->SetViewStyle(vlvsLarge);
104 break;
105 case 1:
106 FListView->SetViewStyle(vlvsSmall);
107 break;
108 case 2:
109 FListView->SetViewStyle(vlvsList);
110 break;
111 case 3:
112 FListView->SetViewStyle(vlvsReport);
113 break;
114 case 4:
115 FListView->SetViewStyle(vlvsTile);
116 break;
117 }
118 }
119
120 void ListView_OnCompareItem(VL_Base* Sender , VInt Index1 , VInt Index2 , VPointer Param , VLE_ListViewSortResult& Result)
121 {
122 VInt ColumnIndex=*(VInt*)Param;
123 VUnicodeString Item1=FListView->GetItem(Index1).GetSubItem(ColumnIndex-1);
124 VUnicodeString Item2=FListView->GetItem(Index2).GetSubItem(ColumnIndex-1);
125 if(Item1>Item2) Result=vlvsrLarger;
126 else if(Item1<Item2) Result=vlvsrSmaller;
127 else Result=vlvsrEqual;
128 }
129
130 void ListView_OnColumnClick(VL_Base* Sender , VInt ItemIndex , VInt SubItemIndex)
131 {
132 if(ItemIndex>=0)
133 {
134 FListView->Sort(&ItemIndex);
135 }
136 }
137
138 void ListView_OnGroupTaskClick(VL_Base* Sender , VInt ItemIndex , VInt SubItemIndex)
139 {
140 VL_WinMsgbox(this,FListView->GetGroupByID(ItemIndex).GetHeader()+L" Clicked.",GetText(),vmbOK);
141 }
142
143 public:
144
145 MyForm():VL_WinForm(true)
146 {
147 SetBorder(vwfbSingle);
148 SetMaximizeBox(false);
149 SetClientWidth(400);
150 SetClientHeight(400);
151 SetText(L"Vczh ListView");
152 MoveCenter();
153 InitControls();
154 Show();
155 }
156
157 ~MyForm()
158 {
159 delete FSmallImages;
160 delete FLargeImages;
161 }
162 };
163
164 void main()
165 {
166 new MyForm;
167 GetApplication()->Run();
168 }
代码2:头文件
1 /*******************************************************************************
2 Vczh Library++ 2.0
3 Windows界面::View
4 开发者:陈梓瀚
5
6 接口:
7 类:
8 VL_WinListViewItem :ListView项目
9 VL_WinListViewColumn :ListView列头
10 VL_WinListViewGroup :ListView组
11 VL_WinListView :ListView
12 函数:
13 *******************************************************************************/
14
15 #ifndef VL_WINVIEW
16 #define VL_WINVIEW
17
18 #include "..\VL_WinClass.h"
19 #include "commctrl.h"
20
21 namespace vl
22 {
23 namespace windows
24 {
25 class VL_WinListViewItem : public VL_Base
26 {
27 friend class VL_WinListView;
28 protected:
29 HWND FHandle;
30 VInt FIndex;
31
32 VL_WinListViewItem(HWND Handle , VInt Index);
33 public:
34 VL_WinListViewItem();
35 VL_WinListViewItem(const VL_WinListViewItem& Item);
36 VL_WinListViewItem& operator=(const VL_WinListViewItem& Item);
37 operator VBool();
38
39 VUnicodeString GetText();
40 void SetText(VUnicodeString Value);
41
42 VInt GetImageIndex();
43 void SetImageIndex(VInt Value);
44
45 VUnicodeString GetSubItem(VInt Index);
46 void SetSubItem(VInt Index , VUnicodeString Value);
47
48 void GetTiledSubItem(VL_List<VInt , true>& Columns);
49 void SetTiledSubItem(VL_List<VInt , true>& Columns);
50
51 VBool GetCutState();
52 void SetCutState(VBool Value);
53
54 VBool GetDropHilitedState();
55 void SetDropHilitedState(VBool Value);
56
57 VBool GetFocusedState();
58 void SetFocusedState(VBool Value);
59
60 VBool GetSelectedState();
61 void SetSelectedState(VBool Value);
62
63 VInt GetIndent();
64 void SetIndent(VInt Value);
65
66 VBool GetChecked();
67 void SetChecked(VBool Value);
68
69 VPointer GetCustomData();
70 void SetCustomData(VPointer Data);
71
72 void EnterGroup(VInt GroupID);
73 void LeaveGroup();
74 VInt GetOwnerGroupID();
75 VBool IsInGroup();
76 };
77
78 class VL_WinListViewColumn : public VL_Base
79 {
80 friend class VL_WinListView;
81 protected:
82 HWND FHandle;
83 VInt FIndex;
84
85 VL_WinListViewColumn(HWND Handle , VInt Index);
86 public:
87 VL_WinListViewColumn();
88 VL_WinListViewColumn(const VL_WinListViewColumn& Item);
89 VL_WinListViewColumn& operator=(const VL_WinListViewColumn& Item);
90 operator VBool();
91
92 VUnicodeString GetText();
93 void SetText(VUnicodeString Value);
94
95 VInt GetSubItemIndex();
96 void SetSubItemIndex(VInt Value);
97
98 VInt GetWidth();
99 void SetWidth(VInt Value);
100
101 VInt GetOrder();
102 void SetOrder(VInt Value);
103
104 VInt GetImageIndex();
105 void SetImageIndex(VInt Value);
106
107 VLE_WinHAlign GetTextAlign();
108 void SetTextAlign(VLE_WinHAlign Value);
109
110 VBool GetImageOnRight();
111 void SetImageOnRight(VBool Value);
112
113 VBool GetFixedWidth();
114 void SetFixedWidth(VBool Value);
115
116 VBool GetShowSplitButton();
117 void SetShowSplitButton(VBool Value);
118 };
119
120 class VL_WinListViewGroup : public VL_Base
121 {
122 friend class VL_WinListView;
123 protected:
124 HWND FHandle;
125 VInt FID;
126
127 VL_WinListViewGroup(HWND Handle , VInt ID);
128
129 VUnicodeString GetListViewGroupText(UINT Mask , LPWSTR LVGROUP::* Item , UINT LVGROUP::* Count);
130 VUnicodeString GetListViewGroupText(UINT Mask , LPWSTR LVGROUP::* Item , int LVGROUP::* Count);
131 void SetListViewGroupText(UINT Mask , LPWSTR LVGROUP::* Item , VUnicodeString Text);
132 public:
133 VL_WinListViewGroup();
134 VL_WinListViewGroup(const VL_WinListViewGroup& Item);
135 VL_WinListViewGroup& operator=(const VL_WinListViewGroup& Item);
136 operator VBool();
137
138 VInt GetGroupID();
139
140 VUnicodeString GetHeader();
141 void SetHeader(VUnicodeString Value);
142
143 VUnicodeString GetFooter();
144 void SetFooter(VUnicodeString Value);
145
146 VUnicodeString GetSubTitle();
147 void SetSubTitle(VUnicodeString Value);
148
149 VUnicodeString GetTask();
150 void SetTask(VUnicodeString Value);
151
152 VUnicodeString GetDescriptionTop();
153 void SetDescriptionTop(VUnicodeString Value);
154
155 VUnicodeString GetDescriptionBottom();
156 void SetDescriptionBottom(VUnicodeString Value);
157
158 VInt GetImageIndex();
159 void SetImageIndex(VInt Value);
160
161 VInt GetExtendedImageIndex();
162 void SetExtendedImageIndex(VInt Value);
163
164 VLE_WinHAlign GetHeaderAlign();
165 void SetHeaderAlign(VLE_WinHAlign Value);
166 };
167
168 enum VLE_ListViewStyle
169 {
170 vlvsLarge,
171 vlvsSmall,
172 vlvsList,
173 vlvsReport,
174 vlvsTile
175 };
176
177 enum VLE_ListViewSortResult
178 {
179 vlvsrLarger,
180 vlvsrEqual,
181 vlvsrSmaller
182 };
183 VL_DEFINE_VOID_EVENT (VE_ListViewItemEvent ,(Sender , ItemIndex , SubItemIndex) ,(VL_Base* Sender , VInt ItemIndex , VInt SubItemIndex));
184 VL_DEFINE_VOID_EVENT (VE_ListViewEditEvent ,(Sender , ItemIndex , Accept , Text) ,(VL_Base* Sender , VInt ItemIndex , VBool& Accept , VUnicodeString Text));
185 VL_DEFINE_VOID_EVENT (VE_ListViewSortEvent ,(Sender , Index1 , Index2 , Param , Result),(VL_Base* Sender , VInt Index1 , VInt Index2 , VPointer Param , VLE_ListViewSortResult& Result));
186
187 class VL_WinListView : public VL_WinControl
188 {
189 protected:
190 VL_WinImageList* FSmallImageList;
191 VL_WinImageList* FLargeImageList;
192 VL_WinImageList* FStateImageList;
193 VL_WinImageList* FGroupImageList;
194
195 VInt FColumnCount;
196 VInt FUsedGroupIDs;
197
198 DWORD InternalGetExStyle();
199 void InternalSetExStyle(DWORD ExStyle);
200 public:
201 VL_WinListView(VL_WinContainer* Parent);
202 ~VL_WinListView();
203 LRESULT ProcessMessage(UINT Message , WPARAM& wParam , LPARAM& lParam , VBool& CallDefaultProcedure);
204
205 VE_ListViewItemEvent OnBeginDrag;
206 VE_ListViewItemEvent OnBeginRightDrag;
207 VE_ListViewEditEvent OnBeginLabelEdit;
208 VE_ListViewEditEvent OnEndLabelEdit;
209 VE_ListViewItemEvent OnColumnClick;
210 VE_ListViewItemEvent OnColumnDropDown;
211 VE_ListViewItemEvent OnItemHotTrack;
212 VE_ListViewItemEvent OnItemActive;
213 VE_ListViewItemEvent OnItemChanging;
214 VE_ListViewItemEvent OnItemChanged;
215 VE_ListViewItemEvent OnGroupTaskClick;
216 VE_ListViewItemEvent OnItemClick;
217 VE_ListViewItemEvent OnItemDbClick;
218 VE_ListViewItemEvent OnItemRightClick;
219 VE_ListViewItemEvent OnItemRightDbClick;
220 VE_ListViewSortEvent OnCompareItems;
221
222 VL_WinImageList* GetSmallImageList();
223 void SetSmallImageList(VL_WinImageList* ImageList);
224
225 VL_WinImageList* GetLargeImageList();
226 void SetLargeImageList(VL_WinImageList* ImageList);
227
228 VL_WinImageList* GetStateImageList();
229 void SetStateImageList(VL_WinImageList* ImageList);
230
231 VL_WinImageList* GetGroupImageList();
232 void SetGroupImageList(VL_WinImageList* ImageList);
233
234 VLE_ListViewStyle GetViewStyle();
235 void SetViewStyle(VLE_ListViewStyle Style);
236
237 COLORREF GetOutlineColor();
238 void SetOutlineColor(COLORREF Value);
239
240 COLORREF GetTextColor();
241 void SetTextColor(COLORREF Value);
242
243 COLORREF GetTextBackColor();
244 void SetTextBackColor(COLORREF Value);
245
246 COLORREF GetBackColor();
247 void SetBackColor(COLORREF Value);
248
249 /**********************************************************
250 Styles
251 **********************************************************/
252
253 VBool GetAutoArrange();
254 void SetAutoArrange(VBool Value);
255
256 VBool GetEditable();
257 void SetEditable(VBool Value);
258
259 VBool GetColumnPushable();
260 void SetColumnPushable(VBool Value);
261
262 VBool GetAlwaysShowSelection();
263 void SetAlwaysShowSelection(VBool Value);
264
265 VBool GetSingleSelection();
266 void SetSingleSelection(VBool Value);
267
268 VBool GetShowCheckBoxes();
269 void SetShowCheckBoxes(VBool Value);
270
271 VBool GetAutoCheckSelect();
272 void SetAutoCheckSelect(VBool Value);
273
274 VBool GetBorderSelect();
275 void SetBorderSelect(VBool Value);
276
277 VBool GetShowGridLines();
278 void SetShowGridLines(VBool Value);
279
280 VBool GetHeaderDragable();
281 void SetHeaderDragable(VBool Value);
282
283 VBool GetOneClickActive();
284 void SetOneClickActive(VBool Value);
285
286 VBool GetTwoClickActive();
287 void SetTwoClickActive(VBool Value);
288
289 VBool GetUnderlineHot();
290 void SetUnderlineHot(VBool Value);
291
292 VBool GetUnderlineCold();
293 void SetUnderlineCold(VBool Value);
294
295 VBool GetAutoAutoArrange();
296 void SetAutoAutoArrange(VBool Value);
297
298 VBool GetSnapToGrid();
299 void SetSnapToGrid(VBool Value);
300
301 /**********************************************************
302 Actions
303 **********************************************************/
304
305 void EditLabel(VInt Index);
306 void CancelEditLabel();
307 void Arrange();
308 void SnapToGrid();
309 void EnsureItemVisible(VInt Index , VBool AcceptPartialVisible);
310 void Sort(VPointer Param=0);
311
312 /**********************************************************
313 Items
314 **********************************************************/
315
316 VInt GetItemCount();
317 void AddItem(VUnicodeString Text , VInt ImageIndex=-1);
318 void InsertItem(VInt Index , VUnicodeString Text , VInt ImageIndex=-1);
319 void DeleteItem(VInt Index);
320 VL_WinListViewItem GetItem(VInt Index);
321 VInt GetSelectedItemCount();
322 VInt &nbs