C++編寫的Windows窗口Hello,World程序
#include <windows.h> #include <tchar.h> /* 使類名成為全局變量 */ TCHAR szClassName[ ] = TEXT("WindowsApp"); /* 這個函數由Windows函數DispatchMessage()調用 */ LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc ; PAINTSTRUCT ps ; RECT rect ; switch (message) /* 處理信息 */ { case WM_PAINT: hdc = BeginPaint (hWnd, &ps) ; GetClientRect (hWnd, &rect) ; DrawText (hdc, TEXT ("Hello World!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; EndPaint (hWnd, &ps) ; break ; case WM_DESTROY: PostQuitMessage (0); /* 發送WM_QUIT到消息隊列 */ break; default: /* 不想處理的消息 */ return DefWindowProc (hWnd, message, wParam, lParam); } return 0; } int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { HWND hwnd; /* 窗口的句柄 */ MSG messages; /* 用於儲存應用程序的消息 */ WNDCLASSEX wincl; /* 窗口類的數據結構 */ /* 窗口結構 */ wincl.hInstance = hThisInstance; wincl.lpszClassName = szClassName; wincl.lpfnWndProc = WindowProcedure; /* 被Windows調用的函數 */ wincl.style = CS_DBLCLKS; /* 捕獲雙擊事件 */ wincl.cbSize = sizeof (WNDCLASSEX); /* 使用默認的圖表和鼠標指針 */ wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION); wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION); wincl.hCursor = LoadCursor (NULL, IDC_ARROW); wincl.lpszMenuName = NULL; /* 沒有菜單 */ wincl.cbClsExtra = 0; /* 窗口類后面沒有額外的字節 */ wincl.cbWndExtra = 0; /* 窗口實例化結構 */ /* 使用Windows的默認顏色作為窗口的背景色 */ wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND; /* 注冊窗口類,如果失敗,退出程序 */ if (!RegisterClassEx (&wincl)) return 0; /* 如果類被注冊,創建窗口 */ hwnd = CreateWindowEx ( 0, /* 擴展的變化信息 */ szClassName, /* 類名 */"Hello World", /* 標題欄文本 */ WS_OVERLAPPEDWINDOW, /* 默認窗口 */ CW_USEDEFAULT, /* 使用默認的位置 */ CW_USEDEFAULT, /* 使用默認的位置 */ 544, /* 窗口寬度(以像素點為單位) */ 375, /* 窗口高度(以像素點為單位) */ HWND_DESKTOP, /* 此窗口是桌面的字窗口 */ NULL, /* 沒有菜單 */ hThisInstance, /* 程序實例化句柄 */ NULL /* 沒有創建數據的窗口 */ ); /* 顯示窗口 */ ShowWindow (hwnd, nFunsterStil); /* 運行消息循環。它將在GetMessage()返回零的時候退出 */ while (GetMessage (&messages, NULL, 0, 0)) { /* 把虛擬按鍵消息翻譯成字符消息 */ TranslateMessage(&messages); /* 把消息發送到WindowProcedure函數 */ DispatchMessage(&messages); } /* 程序的返回值,由PostQuitMessage()提供。 */ return messages.wParam; }