在winapi中使用Listbox的非常简单的示例

时间:2012-10-26 11:20:52

标签: c++ c winapi

如何在ListBox中选择的MessageBox文本中显示?我尝试使用下一个代码,但它只显示空白的MessageBox

wchar_t listBoxStr[15];
HWND hWndList;

switch (message)
{
case WM_CREATE:
    hWndList = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL, 240, 40, 150, 20, hwnd, (HMENU)105, NULL, NULL);
    SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"name");
    SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"extension");
    SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"date");
    SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)"size");
    CreateWindowEx(NULL, TEXT("button"), TEXT("FIND"), WS_VISIBLE | WS_CHILD, 410, 40, 50, 20, hwnd, (HMENU)106, NULL, NULL);
    return 0 ;
    return 0 ;
case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case 106:
        SendMessageW(hWndList, LB_GETTEXT,0,(LPARAM)listBoxStr);
        MessageBoxW(NULL, listBoxStr, L"", NULL);
        return 0;
    }
    return 0;
case WM_PAINT:
    hdc = BeginPaint (hwnd, &ps);
    GetClientRect (hwnd, &rect) ;
    EndPaint (hwnd, &ps) ;
    return 0 ;
case WM_DESTROY:
    PostQuitMessage (0) ;
    return 0 ;
}

2 个答案:

答案 0 :(得分:3)

SendMessage的第一个参数应该是列表框句柄,而不是父窗口句柄。

答案 1 :(得分:1)

通过定义

使hWndList成为静态
static HWND hWndList;

然后尝试这样

 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"name");
 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"extension");
 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"date");
 -  SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM) L"size");