Introduction :
Nous allons voir le contrôle TrackBar appellé aussi slider. C'est le curseur à glissière de
comportement semblable à l'ascenseur ou scroll bar.
Vous l'utilisez pour modifier le volume du son sur votre PC.
Il fait aussi partie des contrôles de la "Common control library" (comctrl32.dll)
Afin de l'implémenter, nous reprendrons l'exemple
de la scroll bar ou ascenseur,
où nous remplacerons l'ascenseur horizontal par une trackbar horizontale
afin de modifier la largeur du trait de la figure.
Mise en oeuvre :
La track bar sera ajoutée depuis les ressources et identifié
par la constante ID_TB1.
CONTROL "", ID_TB1, TRACKBAR_CLASS, TBS_AUTOTICKS | WS_TABSTOP
,96, 24, 60, 16
Comme pour l'exemple de l'ascenseur la largeur du trait sera mémorisé
dans la variable globale Trait.
Son utilisation est plus simple que celle de l'ascenseur. Ne pas oublier
l'initialisation de la "Common control library".
Initialisation des données :
static HWND htb;
switch (uMsg)
{
case WM_INITDIALOG:
{
htb = GetDlgItem(hDlg, ID_TB1);
SendMessage(htb, TBM_SETRANGE,FALSE, MAKELONG(1, 10));
SendMessage(htb, TBM_SETPOS,TRUE, Trait);
SetDlgItemInt(hDlg, ID_LT1, Trait, FALSE);
return TRUE;
}
A l'initialisation de la boîte de dialogue, nous récupérons son handle de fenêtre
avec la fonction GetDlgItem. Ensuite, nous lui envoyons un message TBM_SETRANGE
pour initialiser ses bornes.
Enfin un message TBM_SETPOS pour initialiser sa position.
La gestion de sa position visuelle est automatique contrairement à l'ascenseur.
Donc au traitement du message WM_HSCROLL de la boîte de dialogue, nous nous
contenterons de mettre à jour le texte affichant la largeur désirée du trait.
case WM_HSCROLL:
SetDlgItemInt(hDlg, ID_LT1, SendMessage(htb, TBM_GETPOS, 0, 0), FALSE);
return TRUE;
Pour cela on récupère la position de la TrackBar en lui envoyant un message TBM_GETPOS
et on envoie le résultat au champ texte avec la fonction SetDlgItemInt.
A la fermeture de la boîte de dialogue par Ok :
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
Trait = SendMessage(htb, TBM_GETPOS, 0, 0);
EndDialog(hDlg,DB_OK);
return TRUE;
}
Nous récupérons sa position en lui envoyant un message TBM_GETPOS et nous l'affectons
à la variable Trait.
Code complet :
resource.h :
#define IDM_QUIT 1
#define IDM_PROP 2
#define ID_TB1 101
#define ID_CB1 102
#define ID_LT1 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
DIALOG1 DIALOG
110, 0, 175, 90
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Propriétés de la figure"
BEGIN
LTEXT "Figure", -1, 35, 10, 30, 10
COMBOBOX ID_CB1, 20, 24, 56, 45, CBS_DROPDOWNLIST | WS_TABSTOP
LTEXT "Trait", -1, 115, 10, 30, 10
CONTROL "", ID_TB1, TRACKBAR_CLASS, TBS_AUTOTICKS | WS_TABSTOP
,96, 24, 60, 16
LTEXT "Larg :", -1, 110, 44, 25, 10
RTEXT "0", ID_LT1, 130, 44, 10, 10
DEFPUSHBUTTON "OK", IDOK, 36, 65, 42, 12
PUSHBUTTON "Cancel", IDCANCEL, 96, 65, 42, 12
END
winmain.c :
#include <windows.h>
#include <commctrl.h>
#include "resource.h"
#define DB_OK 1
#define ID_CARRE 0
#define ID_CERCLE 1
#define ID_TRIANGLE 2
UINT Forme;
UINT Trait;
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);
InitCommonControls();
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:
Forme = ID_CARRE;
Trait = 1;
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == IDM_QUIT) PostMessage(hwnd, WM_CLOSE,0,0);
if(LOWORD(wParam) == IDM_PROP)
{
if(DialogBox(hinst, "DIALOG1" , hwnd, (DLGPROC)Dialog1Proc)
== DB_OK)
InvalidateRect(hwnd,NULL,TRUE);
}
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 = (HPEN)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)
{
static HWND htb;
switch (uMsg)
{
case WM_INITDIALOG:
{
SendDlgItemMessage(hDlg, ID_CB1, CB_ADDSTRING, 0, (LONG)"Carré");
SendDlgItemMessage(hDlg, ID_CB1, CB_ADDSTRING, 0, (LONG)"Cercle");
SendDlgItemMessage(hDlg, ID_CB1, CB_ADDSTRING, 0, (LONG)"Triangle");
SendDlgItemMessage(hDlg, ID_CB1, CB_SETCURSEL, Forme, 0);
htb = GetDlgItem(hDlg, ID_TB1);
SendMessage(htb, TBM_SETRANGE,FALSE, MAKELONG(1, 10));
SendMessage(htb, TBM_SETPOS,TRUE, Trait);
SetDlgItemInt(hDlg, ID_LT1, Trait, FALSE);
return TRUE;
}
case WM_HSCROLL:
SetDlgItemInt(hDlg, ID_LT1, SendMessage(htb, TBM_GETPOS, 0, 0), FALSE);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
Trait = SendMessage(htb, TBM_GETPOS, 0, 0);
Forme = SendDlgItemMessage(hDlg, ID_CB1, CB_GETCURSEL, 0, 0);
EndDialog(hDlg,DB_OK);
return TRUE;
}
if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg,0);
return TRUE;
}
default:
return FALSE;
}
}
J'ai testé les compilations avec C++ Builder et DevC++.
Comme pour la Progress Bar le compilateur de ressources de Dev-C++ n'ayant pas
accepté la constante TRACKBAR_CLASS,
j'ai du la remplacer par son véritable nom de classe de fenêtre :
CONTROL "", ID_TB1, "msctls_trackbar32", TBS_AUTOTICKS | WS_TABSTOP
,96, 24, 60, 16
A vos PC.
CGi
|