#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); HINSTANCE hInst; /* The 'main' function of Win32 GUI programs: this is where execution starts */ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { static TCHAR szClassName[] = TEXT("HelloWin"); /*窗口類名*/ HWND hwnd; /*窗口句柄*/ MSG msg; /*消息*/ WNDCLASS wndclass; /*窗口類*/ hInst = hInstance; /***********第一步:註冊窗口類*************/ /*爲窗口類各個字段賦值*/ wndclass.style = CS_HREDRAW | CS_VREDRAW; /*窗口風格*/ wndclass.lpfnWndProc = WndProc; /*窗口過程*/ wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; /*當前窗口句柄*/ wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); /*窗口圖標*/ wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);/*鼠標樣式*/ wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH);/*窗口背景畫刷*/ wndclass.lpszMenuName = NULL; /*窗口菜單*/ wndclass.lpszClassName= szClassName; /*窗口類名*/ /*註冊窗口*/ RegisterClass(&wndclass); /*************第二步:建立窗口(並讓窗口顯示出來)***************/ hwnd = CreateWindow( szClassName, /*窗口名字*/ TEXT("Welcome"), /*窗口標題*/ WS_OVERLAPPEDWINDOW, /*窗口風格*/ CW_USEDEFAULT, /*初始化x軸的位置*/ CW_USEDEFAULT, /*初始化y軸的位置*/ 640, /*窗口寬度*/ 480, /*窗口高度*/ NULL, /*父窗口句柄*/ NULL, /*窗口菜單句柄*/ hInstance, /*當前窗口句柄*/ NULL /*不使用該值*/ ); if(hwnd == NULL) { MessageBox(NULL, "建立窗口出錯!", "Error", MB_OK); return -1; } /*顯示窗口*/ ShowWindow(hwnd, nCmdShow); /*更新(繪製)窗口*/ UpdateWindow(hwnd); /*************第三步:消息循環*************/ while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); /*翻譯消息*/ DispatchMessage(&msg); /*分派消息*/ } return msg.wParam; /*當GetMessage程序返回FALSE是程序結束*/ } /*************第四步:窗口過程*****************/ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; static HFONT hFont; //聲明一個邏輯字體句柄 static HWND labExample; //在初期,咱們將其聲明爲靜態變量 switch(message) { case WM_CREATE: { hFont = CreateFont( -20/*高度*/, -10/*寬度*/,0,0,400/*通常設值爲400*/, FALSE/*不帶斜體*/, FALSE/*不帶下劃線*/,FALSE/*不帶刪除線*/, DEFAULT_CHARSET, //使用默認字符集 OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, //默認輸出質量 FF_DONTCARE, //不指定字體族 "微軟雅黑" //字體名 ); //建立靜態文本控件 labExample = CreateWindow( "static", //靜態文本框的類名 "Hello, Welcome to C world!", //控件文本 WS_CHILD /*子窗口*/ | WS_VISIBLE /*建立時顯示*/ | WS_BORDER /*帶邊框*/, 60/*X座標*/, 60/*Y座標*/, 150/*寬度*/, 100/*高度*/, hWnd, //父窗口句柄 (HMENU)1, //爲控件指定一個惟一的標識符 hInst, //當前實例句柄 NULL ); //設置控件字體 SendMessage( labExample, //欲設置字體的控件句柄 WM_SETFONT, //消息名(消息類型) (WPARAM)hFont, //字體句柄 NULL ); break; } case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); //Ellipse(hdc, 0, 100, 200, 200); EndPaint(hWnd, &ps); break; } /*窗口銷燬消息*/ case WM_DESTROY: { /*處理WM_DESTROY消息時刪除以前咱們建立的一切GDI對象*/ DeleteObject(hFont); //刪除建立的字體 PostQuitMessage(0); break; } default: { return DefWindowProc(hWnd, message, wParam, lParam); } } return 0; }