Win32 Combobox选中内容写入Edit控件技术咨询
Hey there! I see you're working on a Win32 app and want to get the selected ComboBox item to show up in an Edit control instead of just a MessageBox. Let's break down the simple changes you need to make—since you already have a solid programming foundation, this should click quickly.
Step 1: Add the Edit Control
First, we need to add an Edit control to your UI. We'll start by defining an ID for it, then create the control in your addControl function.
Step 2: Update the ComboBox Selection Logic
Instead of popping a MessageBox when the selection changes, we'll send the selected text directly to the Edit control using its window handle.
Full Modified Code
Here's your updated code with all necessary changes (I've marked new sections with comments):
#include <windows.h> #include <tchar.h> #include <iostream> using namespace std; #define OPTINBT1 1 #define OPTINBT2 2 #define COMBO1 3 #define EDIT1 4 // New: Macro for Edit control ID HWND hWnd, hComboOne, hEdit; // New: Add handle for Edit control void addControl(HWND); LPCWSTR egClassName = L"myWindowClass"; LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int nCmdShow) { WNDCLASSW wc = { 0 }; wc.lpszClassName = egClassName; wc.lpfnWndProc = WindowProcedure; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.hInstance = hInst; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); if (!RegisterClassW(&wc)) { const wchar_t Error01[] = L"Register Issue To Check On : "; const wchar_t Error01_Caption[] = L"Error 01"; MessageBoxW(hWnd, Error01, Error01_Caption, MB_OK | MB_ICONERROR); return 0; } LPCWSTR parentWinTitle = L"My Window"; hWnd = CreateWindowW(egClassName, parentWinTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 500, NULL, NULL, NULL, NULL); if (hWnd == NULL) { const wchar_t Error02[] = L"Window Creation Issue To Check On : "; const wchar_t Error02_Caption[] = L"Window Creation Issue To Check On : "; MessageBoxW(hWnd, Error02, Error02_Caption, MB_OK | MB_ICONERROR); } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); MSG msg = { 0 }; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_CREATE: addControl(hWnd); break; case WM_COMMAND: if (HIWORD(wp) == CBN_SELCHANGE) { if (LOWORD(wp) == COMBO1) { HWND hcombo = (HWND)lp; LRESULT index = SendMessageW(hcombo, CB_GETCURSEL, (WPARAM)0, (LPARAM)0); wchar_t buf[256]; SendMessageW(hcombo, (UINT)CB_GETLBTEXT, (WPARAM)index, (LPARAM)buf); // New: Send selected text to the Edit control SetWindowTextW(hEdit, buf); // Alternative: SendMessageW(hEdit, WM_SETTEXT, 0, (LPARAM)buf); } break; } switch (LOWORD(wp)) { case OPTINBT1: MessageBoxW(hWnd, L"This is Radio button1: ", L"Radio Button 2 is good", MB_OK); break; case OPTINBT2: MessageBoxW(hWnd, L"This is Radio button2: ", L"Radio Button 1 is good", MB_OK); break; default:break; } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProcW(hWnd, msg, wp, lp); } return 0; } void addControl(HWND hWnd) { HWND OptBt1, OptBt2; const LPCWSTR cont1 = L"STATIC"; const LPCWSTR cont2 = L"COMBOBOX"; const LPCWSTR cont3 = L"BUTTON"; const LPCWSTR cont4 = L"EDIT"; // New: Class name for Edit control const LPCWSTR emptyS = L""; const LPCWSTR bl = L"RButton 1"; const LPCWSTR b2 = L"RButton 2"; // Option buttons OptBt1 = CreateWindowW(cont3, bl, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 8, 90, 25, hWnd, (HMENU)OPTINBT1, NULL, NULL); OptBt2 = CreateWindowW(cont3, b2, WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 24, 40, 90, 25, hWnd, (HMENU)OPTINBT2, NULL, NULL); SendMessage(OptBt1, BM_SETCHECK, BST_CHECKED, 0); // ComboBox hComboOne = CreateWindowW(cont2, emptyS, WS_VISIBLE | WS_CHILD | CBS_DROPDOWN | CBS_HASSTRINGS | WS_VSCROLL, 77, 70, 150, 150, hWnd, (HMENU)COMBO1, 0, 0); LPCWSTR ComboBoxItems[] = { L"Subject1", L"Subject2", L"Subject3", L"Subject4", L"Subject5" }; // Simplified item addition with a loop for (int i = 0; i < 5; i++) { SendMessageW(hComboOne, CB_ADDSTRING, 0, (LPARAM)ComboBoxItems[i]); } // New: Create the Edit control hEdit = CreateWindowW(cont4, emptyS, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOHSCROLL, 77, 120, 150, 25, hWnd, (HMENU)EDIT1, NULL, NULL); }
Key Changes Explained
- Edit Control ID: We added
EDIT1to uniquely identify the Edit control in message handling. - Global Edit Handle:
hEditis declared globally so we can access it in the window procedure to update its text. - Edit Control Creation: In
addControl, we created the Edit control with standard styles (border, auto-scroll for long text). - Selection Logic Update: Replaced the
MessageBoxWcall withSetWindowTextW(hEdit, buf)—this directly sets the Edit control's text. You could also useSendMessageW(hEdit, WM_SETTEXT, 0, (LPARAM)buf); both work, butSetWindowTextis a simpler wrapper for the underlying Windows message.
That's all! Now when you select an item from the ComboBox, it will immediately appear in the Edit control below it. You're already halfway there with your existing code—this should get you exactly what you need.
内容的提问来源于stack exchange,提问作者Mae Davis




