Home | API | MFC | C++ | C | Up

Programming Windows API

Edit 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;
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);
//Creating the Window
CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("myWindowClass"),TEXT("Edit Control Demo"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, hInstance, NULL);
//The Message Loop
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
HWND static hwndEdit,hwndButton,hwndStatic;
switch(msg) {
// The WM_CREATE message is sent to the window procedure after the window is created, but before the window becomes visible.
case WM_CREATE:
//3 child windows are registered
hwndEdit = CreateWindow(TEXT("edit"), NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,20, 60, 250, 30, hwnd, (HMENU) ID_EDIT,NULL, NULL);//edit control
hwndStatic=CreateWindow(TEXT("static"), NULL,WS_BORDER | WS_CHILD | WS_VISIBLE | SS_LEFT,20, 20, 250, 30,hwnd, (HMENU) 1, NULL, NULL);//static control
hwndButton = CreateWindow(TEXT("button"), TEXT("Set title"),WS_VISIBLE | WS_CHILD, 20, 100, 80, 30,hwnd, (HMENU) ID_BUTTON, NULL, NULL);//click button
break;
case WM_COMMAND:
//responds to button click
if (HIWORD(wParam) == BN_CLICKED) {
const int maxtextlength = 30;
TCHAR textvalue[maxtextlength] = TEXT("");
//gets value of textbox and stores in 'textvalue'
GetWindowText(hwndEdit, textvalue, maxtextlength);
//changes text in static box to value in 'textvalue'
SetWindowText(hwndStatic, textvalue);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}

Home | API | MFC | C++ | C | Up
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions

Last Updated: 17 September 2022