You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS2015 WinAPI项目中const char*数组绑定ComboBox遇乱码问题

Fixing Weird Characters in WinAPI ComboBox with VS2015

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.

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

  1. Right-click your project in Solution Explorer → Properties
  2. Go to Configuration Properties → General
  3. Set Character Set to Use Multi-Byte Character Set
  4. Now all WinAPI macros will map to ANSI versions (e.g., SendDlgItemMessageA), which match your char* 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:43:19