I. Introduction▲
Nous allons aborder un autre contrôle de la « Common control library » : Le contrôle Tab ou boîte à onglets.
Pour cet exemple on construira une boîte à onglets avec 2 onglets sur une fenêtre. Selon l'onglet sélectionné, on affichera soit un contrôle d'édition soit une case à cocher.
II. Mise en œuvre▲
Le contrôle Tab se construit traditionnellement avec la fonction CreateWindow ou CreateWindowEx. L'insertion de ses éléments se fait à l'aide de la macro TabCtrl_InsertItem. Elle reçoit comme paramètre une structure TC_ITEM décrivant l'élément à insérer.
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_CREATE:
{
TC_ITEM tie;
InitCommonControls
(
);
hTabs =
CreateWindowEx
(
0
, WC_TABCONTROL, ""
,
WS_CHILD |
WS_VISIBLE,
10
, 10
, 300
, 200
, hwnd, NULL
, hinst, NULL
);
hEdit =
CreateWindowEx
(
WS_EX_CLIENTEDGE , "
edit
"
,
"
\r\n
Test de la boîte à onglet.
"
,
WS_CHILD |
WS_VISIBLE |
ES_MULTILINE |
ES_WANTRETURN |
WS_VSCROLL,
15
, 40
, 290
, 163
, hwnd, NULL
, hinst, NULL
);
hstatic =
CreateWindowEx
(
0
, "
static
"
, "
Etes vous content ?
"
,
WS_CHILD,
60
, 80
, 160
, 30
, hwnd, NULL
, hinst, NULL
);
hCB =
CreateWindowEx
(
0
, "
button
"
, "
Oui, je suis content.
"
,
WS_CHILD |
BS_AUTOCHECKBOX,
60
, 100
, 160
, 30
, hwnd, NULL
, hinst, NULL
);
tie.mask =
TCIF_TEXT;
tie.pszText =
"
Volet 1
"
;
TabCtrl_InsertItem
(
hTabs, 1
, &
tie);
tie.pszText =
"
Volet 2
"
;
TabCtrl_InsertItem
(
hTabs, 2
, &
tie);
Dans l'exemple il recevra uniquement un texte. On aurait pu lui mettre une image en utilisant une liste d'images comme pour les contrôles List View ou Tree View.
Le contrôle ne gère pas automatiquement son contenu. Nous allons donc le faire sur réception de la notification TCN_SELCHANGE. Et selon l'onglet sélectionné, nous afficherons les contrôles de notre choix à l'aide de la fonction ShowWindow. On récupère l'onglet sélectionné avec la macro TabCtrl_GetCurSel.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
case
WM_NOTIFY:
{
LPNMHDR pnmhdr =
(
LPNMHDR)lParam ;
if
(
pnmhdr->
code ==
TCN_SELCHANGE &&
TabCtrl_GetCurSel
(
hTabs) ==
0
)
{
ShowWindow
(
hCB,SW_HIDE);
ShowWindow
(
hstatic,SW_HIDE);
ShowWindow
(
hEdit,SW_SHOW);
}
if
(
pnmhdr->
code ==
TCN_SELCHANGE &&
TabCtrl_GetCurSel
(
hTabs) ==
1
)
{
ShowWindow
(
hEdit,SW_HIDE);
ShowWindow
(
hstatic,SW_SHOW);
ShowWindow
(
hCB,SW_SHOW);
}
return
0
;
Ce passage n'est qu'un petit aperçu du contrôle Tab. 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.
#include <windows.h>
#include <commctrl.h>
HINSTANCE hinst;
LRESULT CALLBACK MainWndProc
(
HWND, UINT, WPARAM, LPARAM);
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 =
LoadIcon
(
NULL
, IDI_APPLICATION);
wc.hCursor =
LoadCursor
(
NULL
, IDC_ARROW);
wc.hbrBackground =
(
HBRUSH)(
1
+
COLOR_BTNFACE);
wc.lpszMenuName =
NULL
;
wc.lpszClassName =
"
MaWinClass
"
;
if
(!
RegisterClass
(&
wc)) return
FALSE;
hwnd =
CreateWindow
(
"
MaWinClass
"
, "
Titre
"
,
WS_OVERLAPPED |
WS_SYSMENU |
WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 330
, 253
,
NULL
, NULL
, hinstance, NULL
);
if
(!
hwnd) return
FALSE;
ShowWindow
(
hwnd, nCmdShow);
UpdateWindow
(
hwnd);
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 hTabs;
static
HWND hEdit;
static
HWND hCB;
static
HWND hstatic;
switch
(
uMsg)
{
case
WM_CREATE:
{
TC_ITEM tie;
InitCommonControls
(
);
hTabs =
CreateWindowEx
(
0
, WC_TABCONTROL, ""
,
WS_CHILD |
WS_VISIBLE,
10
, 10
, 300
, 200
, hwnd, NULL
, hinst, NULL
);
hEdit =
CreateWindowEx
(
WS_EX_CLIENTEDGE , "
edit
"
,
"
\r\n
Test de la boîte à onglet.
"
,
WS_CHILD |
WS_VISIBLE |
ES_MULTILINE |
ES_WANTRETURN |
WS_VSCROLL,
15
, 40
, 290
, 163
, hwnd, NULL
, hinst, NULL
);
hstatic =
CreateWindowEx
(
0
, "
static
"
, "
Etes vous content ?
"
,
WS_CHILD,
60
, 80
, 160
, 30
, hwnd, NULL
, hinst, NULL
);
hCB =
CreateWindowEx
(
0
, "
button
"
, "
Oui, je suis content.
"
,
WS_CHILD |
BS_AUTOCHECKBOX,
60
, 100
, 160
, 30
, hwnd, NULL
, hinst, NULL
);
tie.mask =
TCIF_TEXT;
tie.pszText =
"
Volet 1
"
;
TabCtrl_InsertItem
(
hTabs, 1
, &
tie);
tie.pszText =
"
Volet 2
"
;
TabCtrl_InsertItem
(
hTabs, 2
, &
tie);
return
0
;
}
case
WM_NOTIFY:
{
LPNMHDR pnmhdr =
(
LPNMHDR)lParam ;
if
(
pnmhdr->
code ==
TCN_SELCHANGE &&
TabCtrl_GetCurSel
(
hTabs) ==
0
)
{
ShowWindow
(
hCB,SW_HIDE);
ShowWindow
(
hstatic,SW_HIDE);
ShowWindow
(
hEdit,SW_SHOW);
}
if
(
pnmhdr->
code ==
TCN_SELCHANGE &&
TabCtrl_GetCurSel
(
hTabs) ==
1
)
{
ShowWindow
(
hEdit,SW_HIDE);
ShowWindow
(
hstatic,SW_SHOW);
ShowWindow
(
hCB,SW_SHOW);
}
return
0
;
}
case
WM_DESTROY:
PostQuitMessage
(
0
);
return
0
;
default
:
return
DefWindowProc
(
hwnd, uMsg, wParam, lParam);
}
}
J'ai testé les compilations avec C++ Builder et DevC++.
À vos PC.