#include <windows.h> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* 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; /*窗口類*/ /***********第一步:註冊窗口類*************/ /*爲窗口類各個字段賦值*/ 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; switch(message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); /*畫矩形,左上角座標(50,50),右下角座標(150,150)*/ Rectangle(hdc, 50, 50, 150, 150); EndPaint(hwnd, &ps); return 0; /*窗口銷燬消息*/ case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }