I. Introduction▲
Nous allons aborder un autre contrôle de la « Common control library » : Le contrôle Tree View. Vous le connaissez, c'est lui qui affiche l'arborescence des dossiers dans le panneau de gauche de l'explorateur de fichiers de Windows.
Nous allons voir une mise en œuvre simple.
Pour cet exemple, nous reprendrons l'exemple de la List View que l'on remplacera par le contrôle Tree View.
II. Mise en œuvre▲
Nous devrons comme pour la List View construire une liste d'images. Le contrôle Tree View se construit traditionnellement avec la fonction CreateWindowEx, avec des flags de style propre à lui-même influençant sur son aspect visuel.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
case
WM_CREATE:
{
TV_INSERTSTRUCT tviis;
TV_ITEM tvi;
HTREEITEM hitem;
InitCommonControls
(
);
hTreeView =
CreateWindowEx
(
WS_EX_CLIENTEDGE , WC_TREEVIEW, ""
,
WS_CHILD |
WS_VISIBLE |
TVS_HASLINES |
TVS_LINESATROOT
|
TVS_HASBUTTONS |
TVS_SHOWSELALWAYS
, 0
, 0
, 0
, 0
, hwnd, NULL
, hinst, NULL
);
TreeView_SetImageList
(
hTreeView, himgList, TVSIL_NORMAL);
tviis.hInsertAfter =
TVI_LAST;
ZeroMemory
(&(
tviis.item), sizeof
(
TV_ITEM));
tviis.item.mask =
TVIF_TEXT |
TVIF_IMAGE |
TVIF_SELECTEDIMAGE |
TVIF_PARAM ;
tviis.hParent =
TVI_ROOT;
tviis.item.iImage =
0
;
tviis.item.iSelectedImage =
1
;
tviis.item.lParam =
1
;
tviis.item.pszText =
"
Option 1
"
;
hitem =
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.hParent =
hitem ;
tviis.item.iImage =
2
;
tviis.item.iSelectedImage =
2
;
tviis.item.lParam =
3
;
tviis.item.pszText =
"
Sous Option A
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.item.iImage =
3
;
tviis.item.iSelectedImage =
3
;
tviis.item.lParam =
4
;
tviis.item.pszText =
"
Sous Option B
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.hParent =
TVI_ROOT;
tviis.item.iImage =
0
;
tviis.item.iSelectedImage =
1
;
tviis.item.lParam =
2
;
tviis.item.pszText =
"
Option 2
"
;
hitem =
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.hParent =
hitem ;
tviis.item.iImage =
3
;
tviis.item.iSelectedImage =
3
;
tviis.item.lParam =
5
;
tviis.item.pszText =
"
Sous Option C
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.item.iSelectedImage =
2
;
tviis.item.iImage =
2
;
tviis.item.lParam =
6
;
tviis.item.pszText =
"
Sous Option D
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
La liste d'images lui est affectée avec la macro TreeView_SetImageList. Pour l'insertion des éléments nous utiliserons une structure spécifique TV_INSERTSTRUCT décrivant les modalités d'insertion et qui contiendra entre autres une structure TV_ITEM décrivant l'élément à insérer. Son champ Mask influence l'affichage et les propriétés de l'élément. Cette structure une fois remplie, est passée comme paramètre à la macro d'insertion TreeView_InsertItem.
Sous C++ Builder 6 la compilation n'a pas passé. La structure TV_INSERTSTRUCT possède une union de deux structures TV_ITEM et TV_ITEMEX J'ai donc du modifié le code pour nommer l'union.
Je n'ai pas eu ce problème sur BCB 4 ou Dev-C++.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
tviis.hInsertAfter =
TVI_LAST;
ZeroMemory
(&(
tviis.u.item), sizeof
(
TV_ITEM));
tviis.u.item.mask =
TVIF_TEXT |
TVIF_IMAGE |
TVIF_SELECTEDIMAGE |
TVIF_PARAM ;
tviis.hParent =
TVI_ROOT;
tviis.u.item.iImage =
0
;
tviis.u.item.iSelectedImage =
1
;
tviis.u.item.lParam =
1
;
tviis.u.item.pszText =
"
Option 1
"
;
hitem =
TreeView_InsertItem
(
hTreeView, &
tviis);
/* ...
*/
Nous allons maintenant effectuer une action quand un de ses éléments est sélectionné. Comme pour la List View c'est sur la réception du message WM_NOTIFY. Pour différencier chaque élément, nous utiliserons le champ lParam de la structure TV_ITEM. Il permet d'enregistrer une valeur sur 32 bits pour chaque élément.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
case
WM_NOTIFY:
{
HTREEITEM hitem;
LPNM_TREEVIEW pntv =
(
LPNM_TREEVIEW)lParam;
if
(
pntv->
hdr.code ==
TVN_SELCHANGED)
{
if
(
pntv->
itemNew.lParam ==
1
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné l'option 1.
"
);
if
(
pntv->
itemNew.lParam ==
2
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné l'option 2.
"
);
if
(
pntv->
itemNew.lParam ==
3
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option A.
"
);
if
(
pntv->
itemNew.lParam ==
4
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option B.
"
);
if
(
pntv->
itemNew.lParam ==
5
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option C.
"
);
if
(
pntv->
itemNew.lParam ==
6
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option D.
"
);
}
return
0
;
}
Ce passage n'est qu'un petit aperçu du contrôle Tree View. Je vous propose de consulter l'aide API win32 pour plus d'information.
III. Code complet▲
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
#include <windows.h>
#include <commctrl.h>
#ifdef STRICT
WNDPROC wpOrigEditProc ;
#else
FARPROC wpOrigEditProc ;
#endif
LRESULT CALLBACK MainWndProc
(
HWND, UINT, WPARAM, LPARAM);
LRESULT APIENTRY EditProc
(
HWND, UINT, WPARAM, LPARAM);
HINSTANCE hinst;
int
WINAPI WinMain
(
HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int
nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
hinst =
hinstance;
wc.style =
0
;
wc.lpfnWndProc =
MainWndProc;
wc.cbClsExtra =
0
;
wc.cbWndExtra =
0
;
wc.hInstance =
hinstance;
wc.hIcon =
NULL
;
wc.hCursor =
LoadCursor
(
NULL
, IDC_ARROW);
wc.hbrBackground =
NULL
;
wc.lpszMenuName =
NULL
;
wc.lpszClassName =
"
MaWinClass
"
;
if
(!
RegisterClass
(&
wc)) return
FALSE;
hwnd =
CreateWindow
(
"
MaWinClass
"
, "
Tree View
"
, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 600
, 300
,
NULL
, NULL
, hinstance, NULL
);
if
(!
hwnd) return
FALSE;
ShowWindow
(
hwnd, nCmdShow);
while
(
GetMessage
(&
msg, NULL
, 0
, 0
))
{
TranslateMessage
(&
msg);
DispatchMessage
(&
msg);
}
return
msg.wParam;
}
/**
***************************************************************************
*/
LRESULT CALLBACK MainWndProc
(
HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static
HWND hEdit;
static
HWND hTreeView;
switch
(
uMsg)
{
case
WM_CREATE:
{
TV_INSERTSTRUCT tviis;
TV_ITEM tvi;
HTREEITEM hitem;
HINSTANCE dllhinst;
HICON hIcon;
HIMAGELIST himgList;
InitCommonControls
(
);
himgList =
ImageList_Create
(
GetSystemMetrics
(
SM_CXSMICON),
GetSystemMetrics
(
SM_CYSMICON), ILC_COLOR32 , 4
, 4
);
ImageList_SetBkColor
(
himgList, GetSysColor
(
COLOR_WINDOW));
dllhinst =
LoadLibrary
(
"
shell32.dll
"
);
hIcon =
LoadIcon
(
dllhinst, MAKEINTRESOURCE
(
4
));
ImageList_AddIcon
(
himgList, hIcon);
hIcon =
LoadIcon
(
dllhinst, MAKEINTRESOURCE
(
5
));
ImageList_AddIcon
(
himgList, hIcon);
hIcon =
LoadIcon
(
dllhinst, MAKEINTRESOURCE
(
6
));
ImageList_AddIcon
(
himgList, hIcon);
hIcon =
LoadIcon
(
dllhinst, MAKEINTRESOURCE
(
12
));
ImageList_AddIcon
(
himgList, hIcon);
FreeLibrary
(
dllhinst);
hTreeView =
CreateWindowEx
(
WS_EX_CLIENTEDGE , WC_TREEVIEW, ""
,
WS_CHILD |
WS_VISIBLE |
TVS_HASLINES |
TVS_LINESATROOT
|
TVS_HASBUTTONS |
TVS_SHOWSELALWAYS
, 0
, 0
, 0
, 0
, hwnd, NULL
, hinst, NULL
);
hEdit =
CreateWindowEx
(
WS_EX_CLIENTEDGE , "
edit
"
,
"
\r\n
Test de la ListView.
"
,
WS_CHILD |
WS_VISIBLE |
ES_MULTILINE |
ES_WANTRETURN |
WS_VSCROLL,
0
, 0
, 0
, 0
, hwnd, NULL
, hinst, NULL
);
SetClassLong
(
hEdit, GCL_HCURSOR, (
LONG)LoadCursor
(
NULL
, IDC_ARROW));
wpOrigEditProc =
(
PVOID)SetWindowLong
(
hEdit,
GWL_WNDPROC, (
LONG)EditProc);
TreeView_SetImageList
(
hTreeView, himgList, TVSIL_NORMAL);
tviis.hInsertAfter =
TVI_LAST;
ZeroMemory
(&(
tviis.item), sizeof
(
TV_ITEM));
tviis.item.mask =
TVIF_TEXT |
TVIF_IMAGE |
TVIF_SELECTEDIMAGE |
TVIF_PARAM ;
tviis.hParent =
TVI_ROOT;
tviis.item.iImage =
0
;
tviis.item.iSelectedImage =
1
;
tviis.item.lParam =
1
;
tviis.item.pszText =
"
Option 1
"
;
hitem =
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.hParent =
hitem ;
tviis.item.iImage =
2
;
tviis.item.iSelectedImage =
2
;
tviis.item.lParam =
3
;
tviis.item.pszText =
"
Sous Option A
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.item.iImage =
3
;
tviis.item.iSelectedImage =
3
;
tviis.item.lParam =
4
;
tviis.item.pszText =
"
Sous Option B
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.hParent =
TVI_ROOT;
tviis.item.iImage =
0
;
tviis.item.iSelectedImage =
1
;
tviis.item.lParam =
2
;
tviis.item.pszText =
"
Option 2
"
;
hitem =
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.hParent =
hitem ;
tviis.item.iImage =
3
;
tviis.item.iSelectedImage =
3
;
tviis.item.lParam =
5
;
tviis.item.pszText =
"
Sous Option C
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
tviis.item.iSelectedImage =
2
;
tviis.item.iImage =
2
;
tviis.item.lParam =
6
;
tviis.item.pszText =
"
Sous Option D
"
;
TreeView_InsertItem
(
hTreeView, &
tviis);
return
0
;
}
case
WM_NOTIFY:
{
HTREEITEM hitem;
LPNM_TREEVIEW pntv =
(
LPNM_TREEVIEW)lParam;
if
(
pntv->
hdr.code ==
TVN_SELCHANGED)
{
if
(
pntv->
itemNew.lParam ==
1
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné l'option 1.
"
);
if
(
pntv->
itemNew.lParam ==
2
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné l'option 2.
"
);
if
(
pntv->
itemNew.lParam ==
3
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option A.
"
);
if
(
pntv->
itemNew.lParam ==
4
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option B.
"
);
if
(
pntv->
itemNew.lParam ==
5
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option C.
"
);
if
(
pntv->
itemNew.lParam ==
6
)
SetWindowText
(
hEdit,
"
\r\n\r\n\r\n
Vous avez sélectionné la sous Option D.
"
);
}
return
0
;
}
case
WM_SIZE:
MoveWindow
(
hEdit, 200
, 0
, LOWORD
(
lParam)-
200
, HIWORD
(
lParam), TRUE);
MoveWindow
(
hTreeView, 0
, 0
, 200
, HIWORD
(
lParam), TRUE);
return
0
;
case
WM_DESTROY:
SetWindowLong
(
hEdit, GWL_WNDPROC,(
LONG) wpOrigEditProc);
PostQuitMessage
(
0
);
return
0
;
default
:
return
DefWindowProc
(
hwnd, uMsg, wParam, lParam);
}
}
/**
***************************************************************************
*/
LRESULT APIENTRY EditProc
(
HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if
(
uMsg ==
WM_SETFOCUS ||
uMsg ==
WM_CONTEXTMENU ||
uMsg ==
WM_CHAR ||
uMsg ==
WM_KEYDOWN)
return
0
;
return
CallWindowProc
(
wpOrigEditProc, hwnd, uMsg, wParam, lParam);
}
J'ai testé les compilations avec C++ Builder et DevC++.
À vos PC.