Guide pratique pour utiliser un Windows associé
En définissant d’autres contrôles en tant que fenêtres d’amis pour un TrackBar, vous pouvez positionner automatiquement ces contrôles aux extrémités du TrackBar en tant qu’étiquettes.
L’illustration suivante montre un TrackBar horizontal et vertical, tous deux avec des contrôles statiques comme fenêtres associées.

Bon à savoir
Technologies
Prérequis
- C/C++
- Windows Programmation de l’interface utilisateur
Instructions
Utiliser le Windows d’ami
L’exemple de code suivant crée les fenêtres d’amis présentées dans l’illustration.
void LabelTrackbarsWithBuddies(HWND hDlg)
{
HWND hwndTrackbar;
HWND hwndBuddy;
const int staticWidth = 50;
const int staticHeight = 20;
//======================================================
// For horizontal Trackbar.
hwndTrackbar = GetDlgItem(hDlg, IDC_SLIDER1);
hwndBuddy = CreateWindowEx(0, L"STATIC", L"Left", SS_RIGHT | WS_CHILD | WS_VISIBLE,
0, 0, staticWidth, staticHeight, hDlg, NULL, g_hInst, NULL);
SendMessage(hwndTrackbar, TBM_SETBUDDY, (WPARAM)TRUE, (LPARAM)hwndBuddy);
//-------------------------------------------------
hwndBuddy = CreateWindowEx(0, L"STATIC", L"Right", SS_LEFT | WS_CHILD | WS_VISIBLE,
0, 0, staticWidth, staticHeight, hDlg, NULL, g_hInst, NULL);
SendMessage(hwndTrackbar, TBM_SETBUDDY, (WPARAM)FALSE, (LPARAM)hwndBuddy);
//======================================================
// For vertical Trackbar.
hwndTrackbar = GetDlgItem(hDlg, IDC_SLIDER2);
hwndBuddy = CreateWindowEx(0, L"STATIC", L"Top", SS_CENTER | WS_CHILD | WS_VISIBLE,
0, 0, staticWidth, staticHeight, hDlg, NULL, g_hInst, NULL);
SendMessage(hwndTrackbar, TBM_SETBUDDY, (WPARAM)TRUE, (LPARAM)hwndBuddy);
//-------------------------------------------------
hwndBuddy = CreateWindowEx(0, L"STATIC", L"Bottom", SS_CENTER | WS_CHILD | WS_VISIBLE,
0, 0, staticWidth, staticHeight, hDlg, NULL, g_hInst, NULL);
SendMessage(hwndTrackbar, TBM_SETBUDDY, (WPARAM)FALSE, (LPARAM)hwndBuddy);
}
Notes
IDC _ SLIDER1 et IDC _ SLIDER2 sont trackbars créés dans l’éditeur de ressources.