Introduction :
Après la barre de progression
nous allons voir un deuxième contrôle de la
bibliothèque dynamique "Common control library" : la barre d'état.
Vous l'avez sûrement vu dans divers logiciel, c'est celle qui ce trouve en bas
des fenêtres et qui sert à donner des informations.
Elle sera créée dans la fenêtre pricipale.
Nous reprendrons l'exemple du chapitre 10
"Communiquer avec les boîtes de dialogue",
auquel nous ajouterons une barre d'état. Elle affichera des informations
textuelles pour chaque option de menu. Ceci quand elle seront sélectionnées.
Mise en oeuvre :
Comme elle sera sur la fenêtre principale nous la créons avec la fonction
CreateStatusWindow. On aurait pu utiliser CreateWindow mais CreateStatusWindow
est plus simple.
InitCommonControls();
hsb = CreateStatusWindow(WS_CHILD | WS_VISIBLE, "Texte", hwnd, -1);
Comme pour la barre de progression,
il faut appeler la fonction InitCommonControls
avant de créer la barre. C'est tout pour sa création. Mais il va falloir gérer
son redimensionnement quand les dimensions de sa fenêtre parent sont changées par l'utilisateur.
Cela se fera comme pour le contrôle d'édition, à la réception du message WM_SIZE de la fenêtre
parent.
case WM_SIZE:
{
RECT sbRect;
UINT sbheight;
GetWindowRect(hsb, &sbRect);
sbheight = sbRect.bottom - sbRect.top;
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam)-sbheight,
TRUE);
MoveWindow(hsb, 0, HIWORD(lParam)-sbheight, LOWORD(lParam),
sbheight, TRUE);
return 0;
}
On récupére ses dimensions à l'aide de la fonction GetWindowRect, puis on positionne
le contrôle d'édition avec la fonction MoveWindow afin qu'il laisse la place pour la barre d'état
et enfin on positionne la barre d'état sur la place restante toujours avec la fonction
MoveWindow.
Notre but est maintenant d'y afficher des informations quand des options de menus
sont sélectionnées (ça ne veut pas dire actionnées, mais seulement quelles sont en surbrillances).
Pour cela nous allons intercepter le message WM_MENUSELECT qui est envoyé à
la fenêtre quand une option de son menu est sélectionnée.
case WM_MENUSELECT:
{
if (lParam == (LONG)GetMenu(hwnd))
{
if(LOWORD(wParam) == 0)
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"Menu fichiers");
if(LOWORD(wParam) == 1)
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"Menu Aide");
}
else
{
if(LOWORD(wParam) == IDM_QUIT)
SendMessage(hsb, SB_SETTEXT, 0,
(LONG)"Quitter l'application.");
if(LOWORD(wParam) == IDM_NEW)
SendMessage(hsb, SB_SETTEXT, 0,
(LONG)"Crée un nouveau fichier.");
if(LOWORD(wParam) == IDM_PARAM)
SendMessage(hsb, SB_SETTEXT, 0,
(LONG)"Changer les paramètres.");
if(LOWORD(wParam) == IDM_ABOUT)
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"A Propos...");
}
return 0;
}
Puis nous testons le mot de poids faible du paramètre wParam qui est joint au message
pour savoir de quelle option il s'agit. Ce mot contient identificateur de l'option.
Le paramètre lParam contient le handle de menu,
nous nous en servons pour savoir s'il s'agit du menu principal ou non.
Suite à la réception du message,
nous modifions le texte de la barre d'état en lui envoyant un message SB_SETTEXT
avec un pointeur sur la chaîne de caractère contenant le texte souhaité dans
son paramètre lParam.
Nous changerons de même ce texte quand le menu ne sera plus en cours d'utilisation :
case WM_EXITMENULOOP:
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"Texte");
return 0;
Code complet :
resource.h :
#define IDM_QUIT 1
#define IDM_NEW 2
#define IDM_ABOUT 3
#define IDM_PARAM 4
#define IDE_EDIT1 101
#define IDSB_SB1 201
resource.rc :
#include <windows.h>
#include "resource.h"
1 ICON icone.ico
2 ICON autre.ico
LEMENU MENU
BEGIN
POPUP "Fichier"
BEGIN
MENUITEM "&Nouveau\tCtrl+N", IDM_NEW
MENUITEM "&Paramètres...\tCtrl+P", IDM_PARAM
MENUITEM SEPARATOR
MENUITEM "Quitter\tAlt+F4", IDM_QUIT
END
POPUP "Aide"
BEGIN
MENUITEM "A propos...", IDM_ABOUT
END
END
LesAccel ACCELERATORS
BEGIN
"N", IDM_NEW, CONTROL, VIRTKEY
"P", IDM_PARAM, CONTROL, VIRTKEY
VK_F1, IDM_ABOUT, VIRTKEY
END
DIALOG2 DIALOG
60, 60, 182, 70
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Paramètres"
BEGIN
DEFPUSHBUTTON "OK", IDOK, 36, 42, 42, 12
PUSHBUTTON "Cancel", IDCANCEL, 96, 42, 42, 12
EDITTEXT IDE_EDIT1, 88, 15, 74, 12
LTEXT "Titre de la fenêtre", -1, 24, 18, 60, 10
END
DIALOG1 DIALOG
60, 60, 160, 80
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "A propos"
BEGIN
DEFPUSHBUTTON "Ok", IDOK, 56, 50, 42, 12
ICON 2, -1, 20, 15, 32, 32
LTEXT "Mon beau programme !", -1, 60, 18, 80, 10
END
winmain.c :
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
HINSTANCE hinst;
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL APIENTRY Dialog1Proc(HWND, UINT, WPARAM, LPARAM);
BOOL APIENTRY Dialog2Proc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
HACCEL haccel;
hinst = hinstance;
wc.style = 0 ;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinstance;
wc.hIcon = LoadIcon(hinstance,MAKEINTRESOURCE(2));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = "LEMENU";
wc.lpszClassName = "MaWinClass";
if(!RegisterClass(&wc)) return FALSE;
hwnd = CreateWindow("MaWinClass", "Titre", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hinstance, NULL);
if (!hwnd) return FALSE;
ShowWindow(hwnd, nCmdShow);
haccel = LoadAccelerators(hinstance, "LesAccel");
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(hwnd, haccel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
/******************************************************************************/
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HWND hEdit;
static HWND hsb;
static BOOL EditNotChg = TRUE;
switch (uMsg)
{
case WM_CREATE:
{
HFONT hFont;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE ,"edit", "Texte",
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL,
0, 0, 0, 0, hwnd, NULL, hinst, NULL);
hFont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
SendMessage(hEdit,WM_SETFONT,(UINT)hFont,TRUE);
SendMessage(hEdit, EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN,
MAKELONG(5, 5));
InitCommonControls();
hsb = CreateStatusWindow(WS_CHILD | WS_VISIBLE, "Texte", hwnd, -1);
return 0;
}
case WM_CLOSE:
if(EditNotChg ||
MessageBox(hwnd,"Le texte a été modifié.\r\nEtes vous sûr de \
vouloir fermer l'application ?"
,"Question ?",MB_YESNO | MB_ICONQUESTION ) == IDYES)
DestroyWindow(hwnd);
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == IDM_QUIT) PostMessage(hwnd, WM_CLOSE,0,0);
if(LOWORD(wParam) == IDM_NEW)
if(EditNotChg ||
MessageBox(hwnd,"Le texte a été modifié.\r\nEtes vous sûr de \
vouloir fermer votre travail ?"
,"Question ?",MB_YESNO | MB_ICONQUESTION ) == IDYES)
{
SendMessage(hEdit,WM_SETTEXT,0,(LPARAM)"");
EditNotChg = TRUE;
}
if(LOWORD(wParam) == IDM_ABOUT)
DialogBox(hinst, "DIALOG1" , hwnd, (DLGPROC)Dialog1Proc);
if(LOWORD(wParam) == IDM_PARAM)
DialogBoxParam(hinst, "DIALOG2" , hwnd,
(DLGPROC)Dialog2Proc, (LPARAM)hwnd);
if(HIWORD(wParam) == EN_CHANGE) EditNotChg = FALSE;
return 0;
case WM_SIZE:
{
RECT sbRect;
UINT sbheight;
GetWindowRect(hsb, &sbRect);
sbheight = sbRect.bottom - sbRect.top;
MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam)-sbheight,
TRUE);
MoveWindow(hsb, 0, HIWORD(lParam)-sbheight, LOWORD(lParam),
sbheight, TRUE);
return 0;
}
case WM_MENUSELECT:
{
if (lParam == (LONG)GetMenu(hwnd))
{
if(LOWORD(wParam) == 0)
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"Menu fichiers");
if(LOWORD(wParam) == 1)
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"Menu Aide");
}
else
{
if(LOWORD(wParam) == IDM_QUIT)
SendMessage(hsb, SB_SETTEXT, 0,
(LONG)"Quitter l'application.");
if(LOWORD(wParam) == IDM_NEW)
SendMessage(hsb, SB_SETTEXT, 0,
(LONG)"Crée un nouveau fichier.");
if(LOWORD(wParam) == IDM_PARAM)
SendMessage(hsb, SB_SETTEXT, 0,
(LONG)"Changer les paramètres.");
if(LOWORD(wParam) == IDM_ABOUT)
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"A Propos...");
}
return 0;
}
case WM_EXITMENULOOP:
SendMessage(hsb, SB_SETTEXT, 0, (LONG)"Texte");
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:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDCANCEL || LOWORD(wParam) == IDOK)
{
EndDialog(hDlg,0);
return TRUE;
}
default:
return FALSE;
}
}
/******************************************************************************/
BOOL APIENTRY Dialog2Proc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
static HWND hParent;
switch (uMsg)
{
case WM_INITDIALOG:
{
int WindowTextLength;
CHAR *buffer;
hParent = (HWND)lParam;
WindowTextLength = GetWindowTextLength(hParent);
buffer = (CHAR*)LocalAlloc(LMEM_FIXED, WindowTextLength+1);
GetWindowText(hParent, buffer, WindowTextLength+1);
SetDlgItemText(hDlg, IDE_EDIT1, buffer);
LocalFree(buffer);
}
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK )
{
CHAR st[128];
GetDlgItemText(hDlg, IDE_EDIT1, st, 128);
SetWindowText(hParent,st);
EndDialog(hDlg,0);
return TRUE;
}
if (LOWORD(wParam) == IDCANCEL )
{
EndDialog(hDlg,0);
return TRUE;
}
default:
return FALSE;
}
}
J'ai testé les compilations avec C++ Builder et Dev-C++.
N'oubliez pas d'ajouter la librairie d'importation "libcomctl32.a"
avec Dev-C++ (Load object files dans Project options).
A voir aussi : la barre de progression.
A vos PC.
CGi
|