Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,422 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
https://learn.microsoft.com/en-us/windows/win32/controls/create-a-simple-list-box
People are wasting time by reading this.
Plesa make it complete or remove.
``
typedef struct
{
TCHAR achName[MAX_PATH];
TCHAR achPosition[12];
int nGamesPlayed;
int nGoalsScored;
} Player;
Player Roster[] =
{
{TEXT("Haas, Jonathan"), TEXT("Midfield"), 18, 4 },
{TEXT("Pai, Jyothi"), TEXT("Forward"), 36, 12 },
{TEXT("Hanif, Kerim"), TEXT("Back"), 26, 0 },
{TEXT("Anderberg, Michael"), TEXT("Back"), 24, 2 },
{TEXT("Jelitto, Jacek"), TEXT("Midfield"), 26, 3 },
{TEXT("Raposo, Rui"), TEXT("Back"), 24, 3},
{TEXT("Joseph, Brad"), TEXT("Forward"), 13, 3 },
{TEXT("Bouchard, Thomas"), TEXT("Forward"), 28, 5 },
{TEXT("Salmre, Ivo "), TEXT("Midfield"), 27, 7 },
{TEXT("Camp, David"), TEXT("Midfield"), 22, 3 },
{TEXT("Kohl, Franz"), TEXT("Goalkeeper"), 17, 0 },
};
INT_PTR CALLBACK ListBoxExampleProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Add items to list.
HWND hwndList = GetDlgItem(hDlg, IDC_LISTBOX_EXAMPLE);
for (int i = 0; i < ARRAYSIZE(Roster); i++)
{
int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0,
(LPARAM) Roster[i].achName);
// Set the array index of the player as item data.
// This enables us to retrieve the item from the array
// even after the items are sorted by the list box.
SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i);
}
// Set input focus to the list box.
SetFocus(hwndList);
return TRUE;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
case IDC_LISTBOX_EXAMPLE:
{
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
{
HWND hwndList = GetDlgItem(hDlg, IDC_LISTBOX_EXAMPLE);
// Get selected index.
int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);
// Get item data.
int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);
// Do something with the data from Roster[i]
TCHAR buff[MAX_PATH];
StringCbPrintf (buff, ARRAYSIZE(buff),
TEXT("Position: %s\nGames played: %d\nGoals: %d"),
Roster[i].achPosition, Roster[i].nGamesPlayed,
Roster[i].nGoalsScored);
SetDlgItemText(hDlg, IDC_STATISTICS, buff);
return TRUE;
}
}
}
return TRUE;
}
}
return FALSE;
}
The code snippets in much of Microsoft's documentation are intended to demonstrate specific points. If you want to see a more complete sample of basic Win32 code take a look at https://learn.microsoft.com/en-us/windows/win32/learnwin32/learn-to-program-for-windows