The following messages are sent to both the listbox and staticbox as a SendMessage API function parameter.
LB_ADDSTRING- add items to the listbox
LB_GETCURSEL – get index of currently selected item in listbox
LB_GETTEXT – retrieves listbox item text
WM_SETTEXT – sets value of static box display value
#include <windows.h> #define IDC_LIST 1 #define IDC_STATIC 2 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; MSG msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("MyWindowClass"); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wc); CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("MyWindowClass"), TEXT("List Box Demo"), WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700, 200, NULL, NULL, hInstance, NULL); while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hwndList; static HWND hwndStatic; TCHAR *names[] = { TEXT("Matthew"), TEXT("Mark"), TEXT("Luke"), TEXT("John") }; switch(msg) { case WM_CREATE: { // Create list box hwndList = CreateWindow( TEXT("LISTBOX"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | LBS_NOTIFY, 10, 10, 150, 80, hwnd, (HMENU)IDC_LIST, NULL, NULL); // Create static control hwndStatic = CreateWindow( TEXT("STATIC"), TEXT("Select a name"), WS_CHILD | WS_VISIBLE | WS_BORDER, 10, 100, 150, 25, hwnd, (HMENU)IDC_STATIC, NULL, NULL); // Add items to list box for(int i = 0; i < 4; i++) { SendMessage( hwndList, LB_ADDSTRING, 0, (LPARAM)names[i]); } return 0; } case WM_COMMAND: { // List box selection changed if(LOWORD(wParam) == IDC_LIST && HIWORD(wParam) == LBN_SELCHANGE) { TCHAR selectedText[50]; // Get selected item index int index = (int)SendMessage( hwndList, LB_GETCURSEL, 0, 0); // Get selected item text SendMessage( hwndList, LB_GETTEXT, index, (LPARAM)selectedText); // Display selected text SetWindowText( hwndStatic, selectedText); } return 0; } case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, msg, wParam, lParam); }