VS2015 WinAPI项目中const char*数组绑定ComboBox遇乱码问题
Hey there, let's get to the bottom of those garbled characters in your combo box! This is a super common pitfall when working with WinAPI in Visual Studio 2015, and it all comes down to Unicode vs. ANSI encoding mismatch. Here's how to fix it:
Why the Weird Characters Happen
Visual Studio 2015 defaults to the Unicode Character Set for new projects. That means every WinAPI macro like CreateWindow or SendDlgItemMessage actually maps to its wide-character (UTF-16) counterpart (e.g., CreateWindowW, SendDlgItemMessageW). But your array uses const char* (ANSI 8-bit strings), and passing these directly to wide-character functions causes the bytes to be misinterpreted as UTF-16, resulting in garbage characters.
Solution 1: Switch to Wide-Character Strings (Recommended)
The cleanest fix is to use wide characters throughout your WinAPI calls, since Unicode is the standard for modern Windows apps. Adjust your array and keep using the default macro calls:
// Use wchar_t instead of char, and prefix strings with L to make them wide-character literals const wchar_t* Filtros[] = { L"Media", L"Media Ponderada", L"Mediana", L"Gaussiano", L"Ecualización Simple", L"Ecualización Unif.", L"Suavizado Conservador", L"Laplasiano" }; // Then add items to the combo box as usual (macros map to W versions automatically) for (int i = 0; i < _countof(Filtros); ++i) { SendDlgItemMessage(hwnd, YOUR_COMBOBOX_ID, CB_ADDSTRING, 0, (LPARAM)Filtros[i]); }
Solution 2: Stick with ANSI (If You Prefer)
If you want to keep using char* strings, you have two options:
Option A: Change Project Character Set
- Right-click your project in Solution Explorer → Properties
- Go to Configuration Properties → General
- Set Character Set to Use Multi-Byte Character Set
- Now all WinAPI macros will map to ANSI versions (e.g.,
SendDlgItemMessageA), which match yourchar*array. Your existing code should work without changes.
Option B: Explicitly Call ANSI Functions
If you don't want to modify project settings, explicitly use the A-suffixed WinAPI functions:
// Keep your original char* array const char* Filtros[] = { "Media", "Media Ponderada", "Mediana", "Gaussiano", "Ecualización Simple", "Ecualización Unif.", "Suavizado Conservador", "Laplasiano" }; // Use SendDlgItemMessageA instead of SendDlgItemMessage for (int i = 0; i < _countof(Filtros); ++i) { SendDlgItemMessageA(hwnd, YOUR_COMBOBOX_ID, CB_ADDSTRING, 0, (LPARAM)Filtros[i]); } // Make sure you also use CreateWindowA when creating the combo box! HWND hCombo = CreateWindowA( "COMBOBOX", "", CBS_DROPDOWN | WS_VISIBLE | WS_CHILD, x, y, width, height, hwnd, (HMENU)YOUR_COMBOBOX_ID, GetModuleHandle(NULL), NULL );
Quick Checks to Rule Out Other Issues
- Double-check that your combo box is created with valid styles (e.g.,
CBS_DROPDOWN,CBS_SIMPLE)—avoid any custom styles that might interfere with text rendering. - Verify your loop is correctly iterating through all array elements (use
_countof(Filtros)to get the element count safely, instead of hardcoding numbers).
内容的提问来源于stack exchange,提问作者Maurice Le Olavarriet

