#include <Windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
truestatic TCHAR szAppName[] = TEXT("HelloWin");
trueHWND hwnd;
trueMSG msg;
truetruetruetrue
truetruetruetruetruetypedef struct tagMSG {
truetruetruetruetruetrueHWND hwnd;
truetruetruetruetruetrueUINT message;
truetruetruetruetruetrueWPARAM wParam; // 一个32位元的讯息参数
truetruetruetruetruetrueLPARAM lParam; // 一个32位元的讯息参数
truetruetruetruetruetrueDWORD time; // 讯息放入讯息伫列的时间
truetruetruetruetruetruePOINT pt; // 讯息放入讯息伫列时的滑鼠坐标
truetruetruetruetrue} MSG, *PMSG;
truetruetruetruetruetypedef struct tagPOINT {
truetruetruetruetruetrueLONG x;
truetruetruetruetruetrueLONG y;
truetruetruetruetrue} POINT, *PPOINT;
truetruetruetrue*/
trueWNDCLASS wndclass;
truetruetruetrue
truetruetruetruetrue// Unicode版本
truetruetruetruetruetypedef struct tagWNDCLASSW {
truetruetruetruetruetrueUINT style;
truetruetruetruetruetrueWNDPROC lpfnWndProc;
truetruetruetruetruetrueint cbClsExtra;
truetruetruetruetruetrueint cbWndExtra;
truetruetruetruetruetrueHINSTANCE hInstance;
truetruetruetruetruetrueHICON hIcon;
truetruetruetruetruetrueHCURSOR hCursor;
truetruetruetruetruetrueHBRUSH hbrBackground;
truetruetruetruetruetrueLPCWSTR lpszMenuName;
truetruetruetruetruetrueLPCWSTR lpszClassName;
truetruetruetruetrue} WNDCLASSW, *PWNDCLASSW, NEAR *NPWNDCLASSW, FAR *LPWNDCLASSW;
truetruetruetrue*/
truewndclass.style = CS_HREDRAW | CS_VREDRAW;
true
truewndclass.lpfnWndProc = WndProc;
truewndclass.cbClsExtra = 0;
truewndclass.cbWndExtra = 0;
truewndclass.hInstance = hInstance;
truewndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
truewndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
truewndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
truewndclass.lpszMenuName = NULL;
truewndclass.lpszClassName = szAppName;
trueif (!RegisterClass(&wndclass)) {
truetrueMessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
truetruereturn 0;
true}
true
truehwnd = CreateWindow( szAppName,
TEXT("The Hello Program"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL,
true);
trueShowWindow(hwnd, iCmdShow);
trueUpdateWindow(hwnd);
true
truewhile (GetMessage(&msg, NULL, 0, 0)) {
truetrueTranslateMessage(&msg);
truetrueDispatchMessage(&msg);
true}
truereturn msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
trueHDC hdc;
truePAINTSTRUCT ps;
truetruetruetrue
truetruetruetruetruetypedef struct tagPAINTSTRUCT {
truetruetruetruetruetrue// 使用者程序使用
truetruetruetruetruetrueHDC hdc;
truetruetruetruetruetrueBOOL fErase; // 大多数时候为FALSE(0),意味着已经擦除了无效矩形的背景
truetruetruetruetruetrueRECT rcPaint; // 无效边框的边界
truetruetruetruetruetrue// windows内部使用
truetruetruetruetruetrueBOOL fRestore;
truetruetruetruetruetrueBOOL fIncUpdate;
truetruetruetruetruetrueBYTE rgbReserved[32];
truetruetruetruetrue} PAINTSTRUCT;
truetruetruetrue*/
true
trueRECT rect;
truestatic int i = 0;
trueswitch(message) {
truecase WM_CREATE:
truetruereturn 0;
truecase WM_PAINT:
truetruehdc = BeginPaint(hwnd, &ps);
truetrueGetClientRect(hwnd, &rect);
truetruestatic TCHAR szBuffer[40];
truetrueDrawText(hdc, TEXT("Hello, Windows 7!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
truetrueTextOut(hdc, 40, 60, szBuffer, wsprintf(szBuffer, TEXT("Hello, Windows %d"), i++));
truetrueEndPaint(hwnd, &ps);
truetruereturn 0;
truecase WM_DESTROY:
truetruePostQuitMessage(0);
truetruereturn 0;
true}
truereturn DefWindowProc(hwnd, message, wParam, lParam);
}