Edit Box Control Example

#include <windows.h> #define ID_EDIT 1 #define ID_BUTTON 2 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; MSG msg; // Define the window class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("MyWindowClass"); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); RegisterClassEx(&wc); // Create the main window CreateWindowEx( WS_EX_CLIENTEDGE, TEXT("MyWindowClass"), TEXT("Edit Box Demo"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 700, 350, NULL, NULL, hInstance, NULL); // Message loop while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hwndEdit; static HWND hwndButton; static HWND hwndStatic; switch (msg) { case WM_CREATE: // Create the edit box hwndEdit = CreateWindow( TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER, 20, 60, 250, 30, hwnd, (HMENU)ID_EDIT, NULL, NULL); // Create the static text control hwndStatic = CreateWindow( TEXT("STATIC"), TEXT("Enter text below"), WS_CHILD | WS_VISIBLE | WS_BORDER | SS_LEFT, 20, 20, 250, 30, hwnd, NULL, NULL, NULL); // Create the button hwndButton = CreateWindow( TEXT("BUTTON"), TEXT("Set Title"), WS_CHILD | WS_VISIBLE, 20, 100, 100, 30, hwnd, (HMENU)ID_BUTTON, NULL, NULL); break; case WM_COMMAND: // Check if the button was clicked if (LOWORD(wParam) == ID_BUTTON && HIWORD(wParam) == BN_CLICKED) { const int MAX_TEXT = 100; TCHAR textValue[MAX_TEXT]; // Get text from the edit box GetWindowText(hwndEdit, textValue, MAX_TEXT); // Display the text in the static control SetWindowText(hwndStatic, textValue); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }