Introduction :
Un petit chapitre pour aborder les menus contextuels.
Ceux qui apparaissent quand vous faîtes un click droit sur une fenêtre.
Afin de l'implémenter, nous reprendrons l'exemple
de la case à cocher.
Nous metterons le popup menu "Fichier" déjà existant en menu contextuel.
Mise en oeuvre :
Le menu étant déjà été préparé dans les ressources :
LEMENU MENU
BEGIN
POPUP "Fichier"
BEGIN
MENUITEM "Propriétés...", IDM_PROP
MENUITEM SEPARATOR
MENUITEM "Quitter", IDM_QUIT
END
END
Et ses commandes implémentées.
Il nous suffit juste de l'appeler suite à un click droit de la souris sur
la fenêtre.
Mais il existe un message plus approprié WM_CONTEXTMENU qui de plus contient
les coordonnées du curseur par rapport à l'écran dans son paramètre lParam.
case WM_CONTEXTMENU :
{
HMENU hmenu, hpopup;
hmenu = LoadMenu(hinst,"LEMENU");
hpopup = GetSubMenu(hmenu, 0);
TrackPopupMenuEx(hpopup, 0, LOWORD(lParam), HIWORD(lParam), hwnd, NULL);
DestroyMenu(hmenu);
return 0;
}
Nous chargeons le menu avec LoadMenu.
Nous récupérons le handle de son 1er sous menu avec la fonction GetSubMenu
(le 1er a l'indice 0).
Nous affichons le menu avec la fonction TrackPopupMenuEx.
Le premier paramètre de cette fonction est le handle du popup menu. Le second
reçoit des flags influençant son comportement ou affichage. Les trois et quatrième
sont sa position à l'écran. Le cinquième et le handle de fenêtre de son parent.
Le dernier et une structure contenant une zone rectangulaire de l'écran où le
menu ne peut pas s'ouvrir.
Code complet :
resource.h :
#define IDM_QUIT 1
#define IDM_PROP 2
#define ID_CB1 101
#define ID_CB2 102
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"
BEGIN
DEFPUSHBUTTON "OK", IDOK, 36, 52, 42, 12
PUSHBUTTON "Cancel", IDCANCEL, 96, 52, 42, 12
AUTOCHECKBOX "Carré visible ", ID_CB1, 60, 15, 80, 15
AUTOCHECKBOX "Cercle visible", ID_CB2, 60, 30, 80, 15
END
winmain.c :
#include <windows.h>
#include "resource.h"
typedef struct
{
BOOL CarreVisible;
BOOL CercleVisible;
} PROPRIETE ;
PROPRIETE prop;
HINSTANCE hinst;
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)
{
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 = "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)
{
switch (uMsg)
{
case WM_CREATE:
prop.CarreVisible = TRUE;
prop.CercleVisible = TRUE;
return 0;
case WM_CONTEXTMENU :
{
HMENU hmenu, hpopup;
hmenu = LoadMenu(hinst,"LEMENU");
hpopup = GetSubMenu(hmenu, 0);
TrackPopupMenuEx(hpopup, 0, LOWORD(lParam), HIWORD(lParam), hwnd, NULL);
DestroyMenu(hmenu);
return 0;
}
case WM_COMMAND:
if(LOWORD(wParam) == IDM_QUIT) PostMessage(hwnd, WM_CLOSE,0,0);
if(LOWORD(wParam) == IDM_PROP)
{
DialogBox(hinst, "DIALOG1" , hwnd, (DLGPROC)Dialog1Proc);
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 Dialog1Proc(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_COMMAND:
if (LOWORD(wParam) == IDOK )
{
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;
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 DevC++.
A vos PC.
CGi
|