1

主窗口上有两个项目和按钮的组合框。组合框:

HWND hCombo;
hCombo = CreateWindow(L"COMBOBOX", L"combobox",
       WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST,
       10, 55, 232, 500, hWnd, 0, hInstance, 0);

const wchar_t *langEnglish = L"English";
const wchar_t *langRussian = L"Russian";
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langEnglish);
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langRussian);
SendMessage(hCombo, CB_SETCURSEL, 0, 0);

我试图通过单击按钮在WndProc中获取选定的项目文本:

case WM_COMMAND:                                            
{
    switch(LOWORD(wParam))
    {  
        case IDC_BUTTON_OK:
            wchar_t buf[10];
            hCombo = GetDlgItem(hWnd, IDC_COMBO);
            GetDlgItemText(hCombo, IDC_COMBO, (LPWSTR)buf, 10);
            MessageBox(hWnd, (LPCWSTR)buf, NULL, MB_OK);
            break;
    }

} break;

我在 MSVS2010 中使用断点来查看buf变量。里面有中文符号!!!消息框显示消息(标题为“错误”)。我想看英文文本。怎么了?

这段代码

nIndex = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
SendMessage(hCombo, CB_GETLBTEXT, nIndex, (LPARAM)buf);

用相同的中文符号填充buf

解决方案:
hCombo = CreateWindow(L"COMBOBOX", L"combobox", WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST, 10, 55, 232, 500, hWnd, (HMENU)IDC_COMBO , hInstance, 0);

4

1 回答 1

1

为了从CBS_DROPDOWNLIST样式组合框中获取当前选定的项目,您需要CB_GETCURSEL获取选择索引,然后CB_GETLBTEXT获取字符串。

于 2012-04-30T07:47:49.990 回答