#include <windows.h> /*窗口函數的函數原型*/ LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { char szClassName[] = "MainWClass"; WNDCLASSEX wndclass; /*用描述主窗口的參數填充WNDCLASSEX結構*/ wndclass.cbSize = sizeof(wndclass); /*結構的大小*/ wndclass.style = CS_HREDRAW | CS_VREDRAW; /*指定若是大小改變就重畫*/ wndclass.lpfnWndProc = MainWndProc; /*窗口函數指針*/ 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; /*窗口類的名稱*/ wndclass.hIconSm = NULL; /*沒有類的小圖標*/ /*註冊這個窗口*/ RegisterClassEx(&wndclass); /*建立主窗口*/ HWND hwnd = CreateWindowEx( 0, /*dwExStyle, 擴展樣式*/ szClassName, /*lpClassName, 類名*/ "My first Window!", /*lpWindowName, 標題*/ WS_OVERLAPPEDWINDOW, /*dwStyle, 窗口風格*/ CW_USEDEFAULT, /*X, 初始X座標*/ CW_USEDEFAULT, /*Y, 初始Y座標*/ CW_USEDEFAULT, /*nWidth,寬度*/ CW_USEDEFAULT, /*nHeight, 高度*/ NULL, /*hWndParent, 父窗口句柄*/ NULL, /*hMenu, 菜單句柄*/ hInstance, /*hInstance, 程序實例句柄*/ NULL); /*lpParam, 用戶數據*/ if(hwnd == NULL) { MessageBox(NULL, "Error build the Window!", "Error", MB_OK); return -1; } /*顯示窗口,刷新窗口客戶區*/ ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); /*從消息隊列中取出消息,交給窗戶函數處理,直接到GetMessage返回FALSE,結束消息循環*/ MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { /*轉化鍵盤消息*/ TranslateMessage(&msg); /*將消息發送到相應的窗口函數*/ DispatchMessage(&msg); } /*當GetMessage返回FALSE時程序結束*/ return msg.wParam; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { char szText[] = "最簡單的窗口程序!"; switch(message) { case WM_PAINT: /*窗口客戶區須要重畫*/ { HDC hdc; PAINTSTRUCT ps; /*使無效的客戶區變得有效,並取得設備環境句柄*/ hdc = BeginPaint(hwnd, &ps); /*顯示文字*/ TextOut(hdc, 10, 10, szText, strlen(szText)); EndPaint(hwnd, &ps); return 0; } case WM_DESTROY: /*正在銷燬窗口*/ /*向消息隊列投遞一個WM_QUIT消息,促使GetMessage函數返回0,結束消息循環*/ PostQuitMessage(0); return 0; } /*將咱們不處理的消息交給系統作默認處理*/ return DefWindowProc(hwnd, message, wParam, lParam); }
另外一個相似的例子:windows
#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; RECT rect; switch(message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText( hdc, TEXT("你好,歡迎來到地球。"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER ); EndPaint(hwnd, &ps); return 0; /*窗口銷燬消息*/ case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }