一个Windows程序的开发步骤
#include <windows.h> //1.窗口类需要一个窗口过程,所以先声明一个窗口过程函数 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd ) { static TCHAR app_name[] = TEXT("HelloWin"); //2.注册一个窗口类 WNDCLASS wndclass = { CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hInstance, LoadIcon(nullptr, IDI_APPLICATION), LoadCursor(nullptr, IDC_ARROW), (HBRUSH)GetStockObject(WHITE_BRUSH), nullptr, app_name }; if(!RegisterClass(&wndclass)) { return 0; } //3.创建窗口并显示 HWND hwnd = CreateWindow(app_name, TEXT("The First Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr); ShowWindow(hwnd, nShowCmd); UpdateWindow(hwnd); //4.循环消息队列 MSG msg; //消息结构 while(GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg);//转换某些键盘消息 DispatchMessage(&msg); //将消息发送给窗口过程 } return msg.wParam; } //5.窗口过程 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps;//绘图结构 RECT rect; switch(message) { case WM_CREATE: //PlaySound(TEXT("hellowin.wav"), nullptr, SND_FILENAME|SND_ASYNC); return 0; case WM_PAINT: { hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); DrawText(hdc, TEXT("Hello, Windows7!"), -1, &rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER); EndPaint(hwnd, &ps); return 0; } case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }