1. 程式人生 > >【遊戲開發】遊戲視窗實現自定義貼圖

【遊戲開發】遊戲視窗實現自定義貼圖

//Windows視窗標頭檔案
#include <windows.h>

//PlaySound函式包含的標頭檔案
#pragma comment(lib,"winmm.lib") 

//視窗寬度
#define WINDOW_WIDTH 1132	

//視窗高度
#define WINDOW_HEIGHT 708			
				
//視窗標題
#define WINDOW_TITLE L"【遊戲開發】自定義貼圖"

//全域性環境裝置和全域性記憶體裝置
HDC	g_hdc=NULL,g_mdc=NULL; 

//點陣圖
HBITMAP g_hBackGround; 

//視窗過程函式
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );

//初始化
BOOL Game_Init(HWND hwnd);		

//繪圖
VOID Game_Paint( HWND hwnd);		

//釋放資源
BOOL Game_CleanUp(HWND hwnd );

//主函式
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd)
{
	//定義視窗類
	WNDCLASSEX wndClass = { 0 };
 
	//結構體的位元組數大小
	wndClass.cbSize = sizeof( WNDCLASSEX ) ;	
 
	//視窗的樣式
	wndClass.style = CS_HREDRAW | CS_VREDRAW;	
 
	//指向視窗過程函式的指標
	wndClass.lpfnWndProc = WndProc;		
 
	//視窗類的附加記憶體,取0
	wndClass.cbClsExtra		= 0;		
 
	//視窗的附加記憶體,依然取0
	wndClass.cbWndExtra		= 0;		
 
	//視窗過程的程式的例項控制代碼
	wndClass.hInstance = hInstance;						
 
	//ico圖示
	wndClass.hIcon=(HICON)::LoadImage(NULL,L"天龍八部圖示.ico",IMAGE_ICON,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);
 
	//游標控制代碼
	wndClass.hCursor = LoadCursor( NULL, IDC_ARROW ); 
 
	//畫刷控制代碼
	wndClass.hbrBackground=(HBRUSH)CreateSolidBrush(RGB(255,255,255));  
 
	//指定選單資源的名字
	wndClass.lpszMenuName = NULL;					
 
	//指定視窗類的名字
	wndClass.lpszClassName = L"TWJDWQ";
 
	//註冊視窗類
	if( !RegisterClassEx( &wndClass ) )			
		return -1;		
 
	//建立視窗
	HWND hwnd = CreateWindow( L"TWJDWQ",WINDOW_TITLE,	
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH,
		WINDOW_HEIGHT, NULL, NULL, hInstance, NULL );
 
	//視窗顯示的位置
	MoveWindow(hwnd,250,80,WINDOW_WIDTH,WINDOW_HEIGHT,true);
	
	//顯示視窗
	ShowWindow( hwnd, nShowCmd );
 
	//更新視窗
	UpdateWindow(hwnd);	

	//初始化
	if (!Game_Init (hwnd)) 
	{
		//訊息視窗
		MessageBox(hwnd, L"資源初始化失敗", L"訊息視窗", 0);
		return FALSE;
	}

	//迴圈播放背景音樂
	PlaySound(L"我行即我道.wav", NULL, SND_FILENAME | SND_ASYNC|SND_LOOP); 

	//定義並初始化msg
	MSG msg = { 0 };			

	//訊息迴圈
	while( msg.message != WM_QUIT )			
	{
		//檢視並派發訊息
		if( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
		{
			//將虛擬鍵訊息轉換為字元訊息
			TranslateMessage( &msg );		
 
			//捕捉訊息給視窗
			DispatchMessage( &msg );
		}
	}
 
	//登出視窗類
	UnregisterClass(L"TWJDWQ", wndClass.hInstance); 

	return 0;  
}

//處理視窗訊息
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )      
{
	//記錄繪製資訊
	PAINTSTRUCT paintStruct;			

	switch( message )						
	{
	//繪製
	case WM_PAINT:		

		//指定視窗開始繪製(將和繪圖有關的資訊填充到paintStruct結構體中)
		g_hdc = BeginPaint( hwnd, &paintStruct ); 

		//呼叫繪製函式
		Game_Paint( hwnd);

		//結束繪製
		EndPaint( hwnd, &paintStruct );			

		//更新視窗
		ValidateRect(hwnd, NULL);		

		break;									

	//按鍵
	case WM_KEYDOWN:	

		//按鍵為ESC,銷燬視窗,併發送一條WM_DESTROY訊息
		if (wParam == VK_ESCAPE)    
			DestroyWindow(hwnd);	

		break;									

	//銷燬
	case WM_DESTROY:

		//清理資源
		Game_CleanUp(hwnd);	

		//請求終止,併發送一條WM_DESTROY訊息
		PostQuitMessage( 0 );			

		break;									

	//預設
	default:										
		return DefWindowProc( hwnd, message, wParam, lParam );		
	}

	return 0;						
}

//初始化
BOOL Game_Init( HWND hwnd )
{
	//環境裝置
	g_hdc = GetDC(hwnd); 

	//載入1張點陣圖
	g_hBackGround = (HBITMAP)LoadImage(NULL,L"天龍八部桌布.bmp",IMAGE_BITMAP,WINDOW_WIDTH,WINDOW_HEIGHT,LR_LOADFROMFILE);   

	//記憶體裝置
	g_mdc = CreateCompatibleDC(g_hdc);  

	//繪製
	Game_Paint(hwnd);

	//釋放環境裝置
	ReleaseDC(hwnd,g_hdc);

	return TRUE;
}

//繪製
VOID Game_Paint( HWND hwnd )
{
	//選擇圖片
	SelectObject(g_mdc,g_hBackGround);

	//貼背景圖
	BitBlt(g_hdc,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,g_mdc,0,0,SRCCOPY);  
}

//清理資源
BOOL Game_CleanUp( HWND hwnd )
{
	//釋放點陣圖
	DeleteObject(g_hBackGround);

	//釋放記憶體裝置
	DeleteDC(g_mdc);

	return TRUE;
}