Introduction :
Nous allons voir un contrôle un peu spécial : Le PropertySheet
Ce n'est pas vraiment un contrôle, mais plutôt une boîte de dialogue
préfabriqué. Elle contient déjà des boutons. Elle est utilisée pour faire les
Wisards mais aussi des boîtes de propriétés avec onglets.
C'est sur la base de ce deuxième point que l'on va construire notre exemple.
Création de la boîte de dialogue :
Avant de construire cette boîte de dialogue, il faut construire le
contenu de chaque page de la boîte à onglets. Il est défini dans
les ressources comme des boîtes de dialogue indépendantes :
TAB1 DIALOG
0, 0, 232, 0
BEGIN
AUTOCHECKBOX "Carré visible ", ID_CB1, 60, 15, 80, 15
AUTOCHECKBOX "Cercle visible", ID_CB2, 60, 30, 80, 15
END
TAB2 DIALOG
0, 0, 232, 0
BEGIN
EDITTEXT IDE_EDIT1, 88, 25, 74, 12
LTEXT "Titre de la fenêtre", -1, 24, 28, 60, 10
END
Ensuite avant de créer la PropertySheet il faut initialiser les champs
d'une structure PROPSHEETHEADER correspondant à la PropertySheet et les
champs d'une structure PROPSHEETPAGE, une pour chaque page de la boîte
à onglet.
PROPSHEETPAGE psp[2];
PROPSHEETHEADER psh;
ZeroMemory(psp, sizeof(psp));
ZeroMemory(&psh, sizeof(psh));
psp[0].dwSize = sizeof(PROPSHEETPAGE);
psp[0].dwFlags = PSP_USETITLE ;
psp[0].hInstance = hinst;
psp[0].u.pszTemplate = "TAB1";
psp[0].pfnDlgProc = Tab1Proc;
psp[0].pszTitle = "Figures";
psp[1].dwSize = sizeof(PROPSHEETPAGE);
psp[1].dwFlags = PSP_USETITLE;
psp[1].hInstance = hinst;
psp[1].u.pszTemplate = "TAB2";
psp[1].pfnDlgProc = Tab2Proc;
psp[1].pszTitle = "Fenêtre";
psh.dwSize = sizeof(PROPSHEETHEADER);
psh.dwFlags = PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW
| PSH_NOCONTEXTHELP;
psh.hwndParent = hwnd;
psh.pszCaption = (LPSTR) "Propriétés";
psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
psh.u3.ppsp = (LPCPROPSHEETPAGE) &psp;
PropertySheet(&psh);
Certain de ces champs sont assez explicite. Un mot sur le champ pfnDlgProc de
PROPSHEETPAGE, il contient un pointeur sur la procédure de fenêtre de la boîte de dialogue
(page dans notre cas). Le champs pszTemplate contient lui le nom de la ressource de type DIALOG.
Ensuite il suffit d'appeler la fonction PropertySheet pour ouvrir la boîte de dialogue.
Vous pouvez consulter la msdn pour plus d'information sur ces structures.
(Ces structures contenant des unions u, u2... Sous BCB6, j'ai du les nommer pour compiler l'exemple
avec d'autre compilateur, il n'est pas nécessaire de les nommer.)
Traitement des messages :
On traite chaque page de la boîte à onglet dans la procédure de fenêtre
qui lui a été affectée.
BOOL APIENTRY Tab1Proc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
if(prop.CarreVisible == TRUE)
CheckDlgButton(hDlg, ID_CB1, BST_CHECKED);
if(prop.CercleVisible == TRUE)
CheckDlgButton(hDlg, ID_CB2, BST_CHECKED);
return TRUE;
case WM_NOTIFY:
if ((((NMHDR *)lParam)->code)== PSN_APPLY)
{
if(IsDlgButtonChecked(hDlg, ID_CB1) == BST_CHECKED)
prop.CarreVisible = TRUE;
else prop.CarreVisible = FALSE;
if(IsDlgButtonChecked(hDlg, ID_CB2) == BST_CHECKED)
prop.CercleVisible = TRUE;
else prop.CercleVisible = FALSE;
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
/******************************************************************************/
BOOL APIENTRY Tab2Proc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
CHAR st[256];
switch (uMsg)
{
case WM_INITDIALOG:
GetWindowText(hwnd, st, 256);
SetDlgItemText(hDlg, IDE_EDIT1, st);
return TRUE;
case WM_NOTIFY:
if ((((NMHDR *)lParam)->code)== PSN_APPLY)
{
GetDlgItemText(hDlg, IDE_EDIT1, st, 256);
SetWindowText(hwnd,st);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
Les boutons ne fesant pas partie de nos boîtes de dialogue (pages),
la boîte de dialogue parente (PropertySheet) envoie des messages de notification
au pages pour leur signaler qu'un événement à eu lieu (Ex: bouton de la PropertySheet actionné).
Dans l'exemple la notification PSN_APPLY signale que la boîte a été validée. On peut donc
modifier la fenêtre principale en conséquence.
Code complet :
resource.h :
#define IDM_QUIT 1
#define IDM_PROP 2
#define ID_CB1 101
#define ID_CB2 102
#define IDE_EDIT1 103
resource.rc :
#include <windows.h>
#include "resource.h"
LEMENU MENU
BEGIN
POPUP "Fichier"
BEGIN
MENUITEM "Propriétés...", IDM_PROP
MENUITEM SEPARATOR
MENUITEM "Quitter", IDM_QUIT
END
END
TAB1 DIALOG
0, 0, 232, 0
BEGIN
AUTOCHECKBOX "Carré visible ", ID_CB1, 60, 15, 80, 15
AUTOCHECKBOX "Cercle visible", ID_CB2, 60, 30, 80, 15
END
TAB2 DIALOG
0, 0, 232, 0
BEGIN
EDITTEXT IDE_EDIT1, 88, 25, 74, 12
LTEXT "Titre de la fenêtre", -1, 24, 28, 60, 10
END
winmain.c :
#include <windows.h>
#include "resource.h"
typedef struct
{
BOOL CarreVisible;
BOOL CercleVisible;
} PROPRIETE ;
PROPRIETE prop;
HINSTANCE hinst;
HWND hwnd;
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL APIENTRY Dialog1Proc(HWND, UINT, WPARAM, LPARAM);
BOOL APIENTRY Tab1Proc(HWND ,UINT ,WPARAM ,LPARAM );
BOOL APIENTRY Tab2Proc(HWND ,UINT ,WPARAM ,LPARAM );
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
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 = "LEMENU";
wc.lpszClassName = "MaWinClass";
if(!RegisterClass(&wc)) return FALSE;
hwnd = CreateWindowEx(0, "MaWinClass", "Check Box",
WS_OVERLAPPED | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 260,
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)
{
switch (uMsg)
{
case WM_CREATE:
prop.CarreVisible = TRUE;
prop.CercleVisible = TRUE;
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == IDM_QUIT) PostMessage(hwnd, WM_CLOSE,0,0);
if(LOWORD(wParam) == IDM_PROP)
{
PROPSHEETPAGE psp[2];
PROPSHEETHEADER psh;
ZeroMemory(psp, sizeof(psp));
ZeroMemory(&psh, sizeof(psh));
psp[0].dwSize = sizeof(PROPSHEETPAGE);
psp[0].dwFlags = PSP_USETITLE ;
psp[0].hInstance = hinst;
psp[0].u.pszTemplate = "TAB1";
psp[0].pfnDlgProc = Tab1Proc;
psp[0].pszTitle = "Figures";
psp[1].dwSize = sizeof(PROPSHEETPAGE);
psp[1].dwFlags = PSP_USETITLE;
psp[1].hInstance = hinst;
psp[1].u.pszTemplate = "TAB2";
psp[1].pfnDlgProc = Tab2Proc;
psp[1].pszTitle = "Fenêtre";
psh.dwSize = sizeof(PROPSHEETHEADER);
psh.dwFlags = PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW
| PSH_NOCONTEXTHELP;
psh.hwndParent = hwnd;
psh.pszCaption = (LPSTR) "Propriétés";
psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
psh.u3.ppsp = (LPCPROPSHEETPAGE) &psp;
PropertySheet(&psh);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_PAINT :
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
if(prop.CarreVisible == TRUE) Rectangle(hdc, 20, 20, 170, 170);
if(prop.CercleVisible == TRUE) Ellipse(hdc, 200, 20, 350, 170);
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
/******************************************************************************/
BOOL APIENTRY Tab1Proc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
if(prop.CarreVisible == TRUE)
CheckDlgButton(hDlg, ID_CB1, BST_CHECKED);
if(prop.CercleVisible == TRUE)
CheckDlgButton(hDlg, ID_CB2, BST_CHECKED);
return TRUE;
case WM_NOTIFY:
if ((((NMHDR *)lParam)->code)== PSN_APPLY)
{
if(IsDlgButtonChecked(hDlg, ID_CB1) == BST_CHECKED)
prop.CarreVisible = TRUE;
else prop.CarreVisible = FALSE;
if(IsDlgButtonChecked(hDlg, ID_CB2) == BST_CHECKED)
prop.CercleVisible = TRUE;
else prop.CercleVisible = FALSE;
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
/******************************************************************************/
BOOL APIENTRY Tab2Proc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
CHAR st[256];
switch (uMsg)
{
case WM_INITDIALOG:
GetWindowText(hwnd, st, 256);
SetDlgItemText(hDlg, IDE_EDIT1, st);
return TRUE;
case WM_NOTIFY:
if ((((NMHDR *)lParam)->code)== PSN_APPLY)
{
GetDlgItemText(hDlg, IDE_EDIT1, st, 256);
SetWindowText(hwnd,st);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
J'ai testé les compilations avec C++ Builder et DevC++.
A vos PC.
CGi
|