/* listbox.c Creating listbox controls */
#include <windows.h>
#include "listbox.h"
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hWnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH)GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = "MYMENU" ;
wndclass.lpszClassName = "MyClass" ;
if (!RegisterClass (&wndclass))
return 0 ;
hWnd = CreateWindow ("MyClass", "Listbox Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hWnd, nCmdShow) ;
UpdateWindow (hWnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return (msg.wParam) ;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage,
WPARAM wParam, LPARAM lParam)
{
static HWND hListbox ;
HINSTANCE hInstance ;
int nSel ;
char cBuf [256], cSelBuf [128] ; /* used to get/display...*/
/* list box contents */
switch (wMessage) /* process windows messages */
{
case WM_CREATE: /* program is just starting */
/* get the instance handle */
hInstance = (HINSTANCE)GetWindowLong (hWnd, GWL_HINSTANCE) ;
/* create empty listbox control */
hListbox = CreateWindow ("LISTBOX", "",
WS_CHILD | LBS_STANDARD,
10, 10, 230, 80,
hWnd, (HMENU)LISTBOX_ID, hInstance, NULL) ;
ShowWindow (hListbox, SW_SHOWNORMAL) ;
break ;
case WM_COMMAND:
switch (LOWORD(wParam)) /* menu item or control ID's */
{
case LISTBOX_ID: /* user did something to listbox */
if (HIWORD (wParam) == LBN_SELCHANGE)
{ /* if user selected an item */
/* get number of selected item */
nSel = SendMessage (hListbox, LB_GETCURSEL,
0, 0) ;
/* load sel. string into cSelBuf */
SendMessage (hListbox, LB_GETTEXT, nSel,
(LPARAM) cSelBuf) ;
/* build a readable string */
wsprintf (cBuf, "The selected item = %s.", cSelBuf) ;
/* show string in a message box */
MessageBox (hWnd, cBuf, "Selection", MB_OK) ;
}
break ;
case IDM_FILL: /* fill listbox with strings */
SendMessage (hListbox, LB_RESETCONTENT, 0, 0) ;
SendMessage (hListbox, LB_ADDSTRING, 0,
(LPARAM) "First string added.") ;
SendMessage (hListbox, LB_ADDSTRING, 0,
(LPARAM) "Second string added.") ;
SendMessage (hListbox, LB_ADDSTRING, 0,
(LPARAM) "Third string added.") ;
SendMessage (hListbox, LB_ADDSTRING, 0,
(LPARAM) "Another string added.") ;
SendMessage (hListbox, LB_ADDSTRING, 0,
(LPARAM) "Yet another string added.") ;
SendMessage (hListbox, LB_ADDSTRING, 0,
(LPARAM) "Last string added.") ;
break ;
case IDM_QUIT:
DestroyWindow (hWnd) ;
break ;
}
break ;
case WM_DESTROY:
PostQuitMessage (0) ;
break ;
default:
return DefWindowProc (hWnd, wMessage, wParam, lParam) ;
}
return (0) ;
}