#include <windows.h>
#define IDC_BTN 103
#define IDC_EDIT 104
#define IDC_TEXT 105
WNDCLASSEX WndClassEx;
wchar_t pszClassName[] = L"My Class Name";
HWND hWnd;//窗口句柄
MSG Msg;//消息结构体
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam
)
{
PAINTSTRUCT ps;
HPEN hp;
HDC hdc;
RECT rect;
//button
static HWND hBtn = NULL;
//edit
static HWND hEdit = NULL;
//text
static HWND hText = NULL;
switch (message)
{
case WM_CREATE:
hText = CreateWindow(L"static", L"your name",
WS_CHILD | WS_VISIBLE | SS_LEFT,
50, 50, 80, 20,
hwnd, (HMENU)IDC_TEXT, WndClassEx.hInstance, NULL);
hEdit = CreateWindow(L"edit", L" ",
WS_CHILD | WS_VISIBLE | ES_LEFT | WS_BORDER,
130, 50, 200, 20,
hwnd, (HMENU)IDC_EDIT, WndClassEx.hInstance, NULL);
hBtn = CreateWindow(L"button", L"start game",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
330, 50, 100, 20,
hwnd, (HMENU)IDC_BTN, WndClassEx.hInstance, NULL);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
PostQuitMessage(NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
DrawText(hdc, L"a example by huidong", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hwnd, &ps);
break;
case WM_COMMAND: //ctrls message
switch (LOWORD(wParam))
{
//when chick the button
case IDC_BTN:
{
//save edit ctrl text
wchar_t chEditCtrlText[512] = { 0 };
//get edit ctrl text
GetWindowText(hEdit, chEditCtrlText, 512);
//show text.
MessageBox(NULL, chEditCtrlText, L"your name", MB_OK);
}
break;
}
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hThisInst,
HINSTANCE hPrevInst,
LPSTR lpszCmdLine,
int nCmdShow)
{
//填写结构体
WndClassEx.cbSize = sizeof(WNDCLASSEX);
WndClassEx.style = CS_VREDRAW | CS_HREDRAW;
WndClassEx.lpfnWndProc = WndProc;
WndClassEx.cbClsExtra = 0;
WndClassEx.cbWndExtra = 0;
WndClassEx.hInstance = hThisInst;
WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClassEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClassEx.lpszMenuName = NULL;
WndClassEx.lpszClassName = pszClassName;
WndClassEx.hIconSm = NULL;
//注册窗口类
RegisterClassEx(&WndClassEx);
//创建窗口
hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, pszClassName, L"which is the best title ?", WS_OVERLAPPEDWINDOW, 200, 200, 500, 300, NULL, NULL, hThisInst, NULL);
//显示窗口
ShowWindow(hWnd, SW_SHOWNORMAL);
//更新窗口
UpdateWindow(hWnd);
//循环获得消息
while (GetMessage(&Msg, NULL, NULL, NULL))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}