I. Introduction▲
Nous avons vu plusieurs exemples d'utilisation des boîtes de dialogue. Elles étaient toutes modales. C'est-à-dire que la fenêtre parente n'est plus accessible tant que la boîte de dialogue n'est pas fermée.
Dans l'exemple suivant, nous allons voir une boîte de dialogue non modale (modeless). C'est-à-dire que la fenêtre parente et toujours accessible, même lorsque la boîte de dialogue est ouverte.
Nous reprendrons l'exemple des boutons radio, mais la boîte de dialogue n'aura plus de bouton OK ou Annuler et l'action sur bouton radio aura un effet immédiat sur la fenêtre principale.
II. Création de la boîte de dialogue▲
CreateDialog
(
hinst, "
DIALOG1
"
, hwnd, (
DLGPROC)Dialog1Proc);
La boîte de dialogue sera créée avec la fonction CreateDialog. Il faut savoir qu'une boîte de dialogue de ce type quand elle est fermée avec la croix, en réalité elle est rendue invisible. Donc si on veut la réafficher une seconde fois, il suffira de la rendre visible.
if
(
hdlg) ShowWindow
(
hdlg,SW_SHOWNORMAL);
else
hdlg =
CreateDialog
(
hinst, "
DIALOG1
"
, hwnd, (
DLGPROC)Dialog1Proc);
III. Message de notifications▲
On se servira de la notification BN_CLICKED envoyée à la fenêtre parente des boutons lorsqu'ils sont cliqués. Le mot bas du paramètre wParam du message WM_COMMAND contenant l'identificateur du bouton qui a été cliqué.
case
WM_COMMAND:
if
(
HIWORD
(
wParam) ==
BN_CLICKED) {
switch
(
LOWORD
(
wParam)) {
case
ID_RB1:
Trait =
FIN;
break
;
case
ID_RB2:
Trait =
MOYEN;
break
;
case
ID_RB3:
Trait =
LARGE;
break
;
case
ID_CARRE:
Forme =
ID_CARRE;
break
;
case
ID_CERCLE:
Forme =
ID_CERCLE;
break
;
case
ID_TRIANGLE:
Forme =
ID_TRIANGLE;
break
;
}
InvalidateRect
(
hwnd,NULL
,TRUE);
}
IV. Code complet▲
resource.h :
#define IDM_QUIT 1
#define IDM_PROP 2
#define ID_RB1 101
#define ID_RB2 102
#define ID_RB3 103
#define ID_CARRE 104
#define ID_CERCLE 105
#define ID_TRIANGLE 106
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
DIALOG1 DIALOG
60
, 70
, 175
, 80
STYLE WS_POPUP |
WS_VISIBLE |
WS_CAPTION |
WS_SYSMENU
CAPTION "
Propriétés de la figure
"
BEGIN
GROUPBOX "
Forme
"
, -
1
, 20
, 10
, 60
, 60
, WS_GROUP
AUTORADIOBUTTON "
Carré
"
, ID_CARRE, 30
, 20
, 40
, 15
,
AUTORADIOBUTTON "
Cercle
"
, ID_CERCLE, 30
, 35
, 40
, 15
,
AUTORADIOBUTTON "
Triangle
"
, ID_TRIANGLE, 30
, 50
, 40
, 15
,
GROUPBOX "
Trait
"
, -
1
, 96
, 10
, 60
, 60
, WS_GROUP
AUTORADIOBUTTON "
Fin
"
, ID_RB1, 106
, 20
, 40
, 15
AUTORADIOBUTTON "
Moyen
"
, ID_RB2, 106
, 35
, 40
, 15
AUTORADIOBUTTON "
Large
"
, ID_RB3, 106
, 50
, 40
, 15
END
winmain.c :
#include <windows.h>
#include "resource.h"
#define DB_OK 1
#define FIN 1
#define MOYEN 2
#define LARGE 4
UINT Forme;
UINT Trait;
HINSTANCE hinst;
HWND hwnd;
LRESULT CALLBACK MainWndProc
(
HWND, UINT, WPARAM, LPARAM);
BOOL APIENTRY Dialog1Proc
(
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 =
CreateWindow
(
"
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)
{
static
HWND hdlg;
switch
(
uMsg)
{
case
WM_CREATE:
Forme =
ID_CARRE;
Trait =
FIN;
return
0
;
case
WM_COMMAND:
if
(
LOWORD
(
wParam) ==
IDM_QUIT) PostMessage
(
hwnd, WM_CLOSE,0
,0
);
if
(
LOWORD
(
wParam) ==
IDM_PROP)
{
if
(
hdlg) ShowWindow
(
hdlg,SW_SHOWNORMAL);
else
hdlg =
CreateDialog
(
hinst, "
DIALOG1
"
, hwnd,
(
DLGPROC)Dialog1Proc);
}
return
0
;
case
WM_PAINT :
{
PAINTSTRUCT ps;
HDC hdc ;
HPEN hpen, hpOld;
POINT ptTriangle[3
];
ptTriangle[0
].x =
20
;
ptTriangle[0
].y =
20
;
ptTriangle[1
].x =
20
;
ptTriangle[1
].y =
170
;
ptTriangle[2
].x =
170
;
ptTriangle[2
].y =
95
;
hdc =
BeginPaint
(
hwnd, &
ps);
hpen =
CreatePen
(
PS_SOLID, Trait, 0
);
hpOld =
SelectObject
(
hdc,hpen);
if
(
Forme ==
ID_CARRE) Rectangle
(
hdc, 20
, 20
, 170
, 170
);
if
(
Forme ==
ID_CERCLE) Ellipse
(
hdc, 20
, 20
, 170
, 170
);
if
(
Forme ==
ID_TRIANGLE) Polygon
(
hdc, ptTriangle, 3
);
SelectObject
(
hdc,hpOld);
DeleteObject
(
hpen);
EndPaint
(
hwnd, &
ps);
return
0
;
}
case
WM_DESTROY:
PostQuitMessage
(
0
);
return
0
;
default
:
return
DefWindowProc
(
hwnd, uMsg, wParam, lParam);
}
}
/**
***************************************************************************
*/
BOOL APIENTRY Dialog1Proc
(
HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch
(
uMsg)
{
case
WM_INITDIALOG:
{
if
(
Trait ==
FIN ) CheckDlgButton
(
hDlg, ID_RB1, BST_CHECKED);
if
(
Trait ==
MOYEN) CheckDlgButton
(
hDlg, ID_RB2, BST_CHECKED);
if
(
Trait ==
LARGE) CheckDlgButton
(
hDlg, ID_RB3, BST_CHECKED);
CheckRadioButton
(
hDlg, ID_CARRE, ID_TRIANGLE, Forme);
return
TRUE;
}
case
WM_COMMAND:
if
(
HIWORD
(
wParam) ==
BN_CLICKED) {
switch
(
LOWORD
(
wParam)) {
case
ID_RB1:
Trait =
FIN;
break
;
case
ID_RB2:
Trait =
MOYEN;
break
;
case
ID_RB3:
Trait =
LARGE;
break
;
case
ID_CARRE:
Forme =
ID_CARRE;
break
;
case
ID_CERCLE:
Forme =
ID_CERCLE;
break
;
case
ID_TRIANGLE:
Forme =
ID_TRIANGLE;
break
;
}
InvalidateRect
(
hwnd,NULL
,TRUE);
}
if
(
LOWORD
(
wParam) ==
IDCANCEL)
{
EndDialog
(
hDlg,0
);
return
TRUE;
}
default
:
return
FALSE;
}
}
J'ai testé les compilations avec C++ Builder et DevC++.
À vos PC.
CGi