利用WIN32 APPLIACTIOA创建了一个HELLO WORLD程序,在程序的中ABOUT对话框中添加了一个列表控件
以下是俺对ABOUT对话框的WndProc的改写,在WM_INITDIALOG中使用SendMessage向列表框控件发送LB_ADDSTRING消息即可添加数据。
以下为ABOUT对话框的的窗口过程:
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList = 0; //window handle of the listbox
static const int IDC_LB_DEMO = 7; //id of the listbox
static const wchar_t szClsName[] = L"listbox"; //class type of listbox
static const wchar_t* listboxItems[] = //Strings to be added to to listbox
{
L"春哥",
L"曾哥",
L"凤姐",
L"可爱的羊驼"
};
switch (message)
{
case WM_INITDIALOG:
{ //create listbox window
hwndList = ::CreateWindow(szClsName, NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD,0, 0,
LOWORD(::GetDialogBaseUnits ()) * 16 + ::GetSystemMetrics (SM_CXVSCROLL),
HIWORD(::GetDialogBaseUnits ()) * 5, hDlg, (HMENU)IDC_LB_DEMO, hInst, NULL) ;
for(int i = 0; i < 4; ++i) //add items to the listbox
::SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM)listboxItems[i]);
}
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
if (LOWORD(wParam) == IDC_LB_DEMO && HIWORD (wParam) == LBN_SELCHANGE)
{
int selectedIndex = ::SendMessage (hwndList, LB_GETCURSEL, 0, 0) ;
int lenghtOfCurrentItem = ::SendMessage (hwndList, LB_GETTEXTLEN, selectedIndex, 0) + 1 + MAX_PATH;
wchar_t* pstr = (wchar_t* )::HeapAlloc(::GetProcessHeap(), 0, lenghtOfCurrentItem * sizeof(wchar_t));
::SendMessage (hwndList, LB_GETTEXT, selectedIndex, (LPARAM)pstr) ;
wcscat(pstr, L"---->被你选中了");
::MessageBox(hDlg, pstr, L"Demo of Listbox.", MB_OK);
::HeapFree(::GetProcessHeap(), 0, (LPVOID)pstr);
}
break;
}
return (INT_PTR)FALSE;
}