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.
Mise en oeuvre :
Le contrôle Tab se construit traditionnellement avec la fonction
CreateWindow ou CreateWindowEx. L'insertion de ses éléments ce 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.
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\nTest 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.
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.
Code complet :
#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\nTest 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++.
A vos PC.
CGi
|