#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("listDemo");
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 = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Register failure..."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam) {
static HWND hwndList, hwndStatic;
static int cxChar, cyChar;
TCHAR* buffer[] = {
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten"
};
int i, iIndex, iLength;
TCHAR* text;
switch(message) {
case WM_CREATE:
hwndList = CreateWindow(TEXT("listbox"),
NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
0, 0, 0, 0,
hwnd,
(HMENU)1,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
hwndStatic = CreateWindow(TEXT("static"),
NULL,
WS_CHILD | WS_VISIBLE | SS_LEFT,
0, 0, 0, 0,
hwnd,
(HMENU)2,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
SendMessage(hwndList, WM_SETREDRAW, FALSE, 0);
for(i = 0; i < 10; i++) {
SendMessage(hwndList, LB_ADDSTRING, 0, buffer[i]);
}
SendMessage(hwndList, WM_SETREDRAW, TRUE, 0);
return 0;
case WM_SIZE:
MoveWindow(hwndStatic, cxChar, cyChar, cxChar * 10, cyChar, TRUE);
MoveWindow(hwndList, cxChar, 4 * cyChar, cxChar * 6, cyChar * 5, TRUE);
return 0;
case WM_SETFOCUS:
SetFocus(hwndList);
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == 1 && HIWORD(wParam) == LBN_SELCHANGE) {
iIndex = SendMessage(hwndList, LB_GETCURSEL, 0, 0);
iLength = SendMessage(hwndList, LB_GETTEXTLEN, iIndex, 0) + 1;
text = calloc(iLength, sizeof(TCHAR));
SendMessage(hwndList, LB_GETTEXT, iIndex, (LPARAM)text);
SetWindowText(hwndStatic, text);
free(text);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|