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; 

//7種畫刷
HBRUSH	g_hBrush[7]={0}; 

//6種畫刷樣式
int g_iBrushStyle[6] = {HS_VERTICAL,HS_HORIZONTAL,HS_CROSS,HS_DIAGCROSS,HS_FDIAGONAL,HS_BDIAGONAL}; 


//點陣圖
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);  

	//1種預設畫刷,6種樣式畫刷
	for(int i=0;i<=6;i++)
	{
		if(i==6)
			g_hBrush[i] = CreateSolidBrush(RGB(135,206,250));
		else
			g_hBrush[i] = CreateHatchBrush(g_iBrushStyle[i],RGB(135,206,250));
	}
 
	//繪製
	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);  

	//建立字型
	HFONT hFont=CreateFont(30,0,0,0,0,0,0,0,GB2312_CHARSET,0,0,0,0,L"微軟雅黑"); 

	//在環境裝置中選擇字型
	SelectObject(g_hdc,hFont); 

	//設定文字背景為透明
	SetBkMode(g_hdc, TRANSPARENT);  

	//定義三段文字
	wchar_t text1[]=L"新天龍八部:";
	wchar_t text2[]=L"高山流水覓知音,畫地為牢心無悔。 ";
	wchar_t text3[]=L"--------逍遙派";

	//定義門派
	wchar_t emei[]=L"峨眉派";
	wchar_t xiaoyao[]=L"逍遙派";
	wchar_t guigu[]=L"鬼谷派";
	wchar_t tangmen[]=L"唐門派";
	wchar_t wudang[]=L"武當派";
	wchar_t mingjiao[]=L"明教派";
	wchar_t xingxiu[]=L"星宿派";

	//設定文字顏色並輸出第一段文字
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,30,150,text1,wcslen(text1));

	//設定文字顏色並輸出第二段文字
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,30,200,text2,wcslen(text2));

	//設定文字顏色並輸出第三段文字
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,300,250,text3,wcslen(text3));

	//輸出“峨眉派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,95,70,emei,wcslen(emei));

	//輸出“逍遙派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,245,70,xiaoyao,wcslen(xiaoyao));

	//輸出“鬼谷派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,395,70,guigu,wcslen(guigu));

	//輸出“唐門派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,545,70,tangmen,wcslen(tangmen));

	//輸出“武當派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,695,70,wudang,wcslen(wudang));

	//輸出“明教派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,845,70,mingjiao,wcslen(mingjiao));

	//輸出“星宿派”
	SetTextColor(g_hdc,RGB(135,206,250));
	TextOut(g_hdc,995,70,xingxiu,wcslen(xingxiu));

	//釋放字型物件
	DeleteObject(hFont);

	//定義兩個x座標值
	int x1 = 100;
	int x2 = 150;

	//用7種不同的畫刷填充矩形
	for(int i=0;i<=6;i++)
	{
		//選用畫刷
		SelectObject(g_hdc,g_hBrush[i]); 

		//畫出一個封閉的矩形 ,矩形左上角座標為(x1,70),右下角座標為(x2,y)
		Rectangle(g_hdc,x1,0,x2,70);	

		//矩形直接的間隔
		x1 += 150;
		x2 += 150;
	}
}
 
//清理資源
BOOL Game_CleanUp( HWND hwnd )
{
	//釋放點陣圖
	DeleteObject(g_hBackGround);
 
	//釋放記憶體裝置
	DeleteDC(g_mdc);

	//釋放畫刷
	for (int i=0;i<=6;i++)
	{
		DeleteObject(g_hBrush[i]);
	}
 
	return TRUE;
}