带有值winapi的C ++组合框项

时间:2014-07-29 06:41:48

标签: c++ winapi combobox

以下代码成功运行,但我想如何将值传递给每个项目

//创建Combox框

hWndComboBox = CreateWindowEx(WS_EX_CLIENTEDGE, "Combobox", NULL,WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST , 20, 20, 200, 200, hWnd, (HMENU)IDC_MEDIA_SERVER, NULL, NULL);

//添加项目

SendMessage(hWndComboBox, CB_ADDSTRING, 1 , (LPARAM)(LPCTSTR)"Item1");
SendMessage(hWndComboBox, CB_ADDSTRING, 2 , (LPARAM)(LPCTSTR)"Item2");
SendMessage(hWndComboBox, CB_ADDSTRING, 3 , (LPARAM)(LPCTSTR)"Item3");

//函数

case IDC_MEDIA_SERVER:
{
    switch(HIWORD(wParam))
    {
        case CBN_DROPDOWN:
            break;
        case BN_CLICKED:
            break;
        case CBN_SELCHANGE:
            int idx_row;
            char strText[255] = {0};
            idx_row = SendMessage(hWndComboBox , CB_GETCURSEL, 0, 0 );
            SendMessage(hWndComboBox,CB_GETLBTEXT, idx_row,(LPARAM)strText);
            MessageBox(0,strText,"Information", MB_OK | MB_SYSTEMMODAL);
            break;
    }
}
break;

如何获取所选项目值???

1 个答案:

答案 0 :(得分:0)

你需要:

  1. 使用TEXT()宏代替char[]文字类型LPCTSTR

    SendMessage(hWndComboBox, CB_ADDSTRING, 1, (LPARAM)TEXT("Item1"));
    SendMessage(hWndComboBox, CB_ADDSTRING, 2 , (LPARAM)TEXT("Item2"));
    SendMessage(hWndComboBox, CB_ADDSTRING, 3 , (LPARAM)TEXT("Item3"));
    
  2. strText[]声明为TCHAR而不是char`:

    case CBN_SELCHANGE:
    {
        TCHAR strText[255] = {0};
        int idx_row = SendMessage(hWndComboBox, CB_GETCURSEL, 0, 0 );
        SendMessage(hWndComboBox, CB_GETLBTEXT, idx_row, (LPARAM)strText);
        MessageBox(0, strText, TEXT("Information"), MB_OK | MB_SYSTEMMODAL);
        break;
    }