【遊戲程式設計】顯示點陣圖
阿新 • • 發佈:2018-12-11
執行結果:
原始碼:
#include <windows.h> #pragma comment(lib, "winmm.lib") //呼叫PlaySound函式所需庫檔案t #define WINDOW_WIDTH 600 #define WINDOW_HEIGHT 450 #define WINDOW_TITLE L"顯示點陣圖" HINSTANCE hInst; HDC hdc; HDC mdc; HBITMAP hbmp; //需要載入的點陣圖DC int MyWindowClass(HINSTANCE); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void MyDraw(HDC); /***************************************************************************************************************** 在不同的應用程式中,在此處新增相關的全域性變數 ******************************************************************************************************************/ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPreInstace, LPSTR lpCmdLine, int nCmdShow) { MyWindowClass(hInstance); PlaySound(L"sound.wav", NULL, SND_FILENAME| SND_ASYNC| SND_LOOP); //迴圈播放背景音樂 if(!InitInstance(hInstance, nCmdShow)) return FALSE; MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } int MyWindowClass(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = L"gamebase"; wcex.hIconSm = NULL; return RegisterClassEx(&wcex); } BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; HWND hwnd = CreateWindow(L"gamebase", WINDOW_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); if(!hwnd) return FALSE; MoveWindow(hwnd, 10, 10, WINDOW_WIDTH, WINDOW_HEIGHT, true); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); hdc = GetDC(hwnd); //建立與視窗DC相容的記憶體DC mdc = CreateCompatibleDC(hdc); //從檔案中載入點陣圖 hbmp = (HBITMAP)LoadImage(NULL, L"bg.bmp", IMAGE_BITMAP, 600, 450, LR_LOADFROMFILE); //選用點陣圖物件 SelectObject(mdc, hbmp); MyDraw(hdc); ReleaseDC(hwnd, hdc); return TRUE; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; switch(message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); MyDraw(hdc); EndPaint(hwnd, &ps); break; /************************************************************************************************************** 在退出程式前,往往在此處刪除建立的相關資源 ***************************************************************************************************************/ case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } /*************************************************************************************************************** 在函式MyDraw()中進行相關繪製工作 ****************************************************************************************************************/ void MyDraw(HDC hdc) { //將記憶體DC的內容對映到視窗DC BitBlt(hdc, 0, 0, 600, 450, mdc, 0, 0, SRCCOPY); }