www.pudn.com > PG20101.zip > MainWin.cpp


// 
// Main Window 
// 
//		Copyright (c) 2000-2001 Chihiro.SAKAMOTO (HyperWorks) 
// 
#include "StdAfx.h" 
#include "Sample.h" 
#include "Window.h" 
#include "MainWin.h" 
#include "AboutDlg.h" 
#include "Battle.h" 
#include "Misc.h" 
#include "dc.h" 
#include "resource.h" 
 
// 
// 建構式 
// 
//	變數的初始化 
//	當window由OnCreate初始化時,不會帶出任何東西 
// 
CMainWin::CMainWin() 
{ 
	Action = new CAction(this);	// 虛擬動作(dummy action) 
 
	TIMECAPS	timeCaps; 
	timeGetDevCaps(&timeCaps, sizeof(timeCaps)); 
	TimePeriod = max(timeCaps.wPeriodMin, 1U); 
	timeBeginPeriod(TimePeriod); 
} 
 
// 
// 解構式 
// 
CMainWin::~CMainWin() 
{ 
	timeEndPeriod(TimePeriod); 
 
	DeleteAllAction(); 
} 
 
// 
// window產生的前置處理 
// 
//	指定樣式和大小 
// 
BOOL CMainWin::PreCreateWindow(CREATESTRUCT &cs) 
{ 
	cs.dwExStyle = WS_EX_CLIENTEDGE; 
	cs.style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; 
 
	CRect	rect(0, 0, WindowWidth, WindowHeight); 
	::AdjustWindowRectEx(&rect, cs.style, TRUE, cs.dwExStyle); 
 
	int width = rect.Width(); 
	int height = rect.Height(); 
 
	CRect rcArea; 
	SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcArea, NULL); 
 
	int	x = rcArea.left + (rcArea.Width() - width) / 2; 
	int	y = rcArea.top + (rcArea.Height() - height) / 2; 
 
	cs.x = x; 
	cs.y = y; 
	cs.cx = width; 
	cs.cy = height; 
	cs.lpszClass = "MainWindow"; 
 
	if (!Application->RegisterWndClass(cs.lpszClass, 
		CS_VREDRAW | CS_HREDRAW | CS_OWNDC, LoadCursor(NULL, IDC_ARROW), 
		(HBRUSH)::GetStockObject(BLACK_BRUSH), Application->LoadIcon(IDC_APPICON))) 
		return FALSE; 
	return TRUE; 
} 
 
// 
// 訊息處理 
// 
LRESULT CMainWin::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
	switch (uMsg) { 
	  case WM_FIRSTACTION:		// 最初傳送的訊息 
		OnFirstAction(); 
		break; 
 
	  case WM_CLOSE:			// 關閉視窗 
		OnClose(); 
		break; 
 
	  case WM_ERASEBKGND:		// 刪除背景 
		return FALSE;			// 無動作 
 
	  case WM_LBUTTONDOWN:		// 按下滑鼠左鍵 
		Action->LButtonDown(wParam, CPoint(lParam)); 
		break; 
 
	  case WM_LBUTTONUP:		// 放開滑鼠左鍵 
		Action->LButtonUp(wParam, CPoint(lParam)); 
		break; 
 
	  case WM_RBUTTONDOWN:		// 按下滑鼠右鍵 
		Action->RButtonDown(wParam, CPoint(lParam)); 
		break; 
 
	  case WM_RBUTTONUP:		// 放開滑鼠右鍵 
		Action->RButtonUp(wParam, CPoint(lParam)); 
		break; 
 
	  case WM_MOUSEMOVE:		// 滑鼠指標移動 
		Action->MouseMove(wParam, CPoint(lParam)); 
		break; 
 
	  case WM_KEYDOWN:		// 按下鍵盤 
		Action->KeyDown(wParam); 
		break; 
 
	  case WM_TIMER:		// 計時器到期 
		OnTimer(wParam); 
		break; 
 
	  default: 
		return CWindow::WindowProc(uMsg, wParam, lParam); 
	} 
	return 0L; 
} 
 
// 
// WM_CREATE的處理 
// 
BOOL CMainWin::OnCreate(CREATESTRUCT *cs) 
{ 
	LoadAccelTable(IDC_APPACCEL); 
 
	CClientDC	dc(this); 
 
	// 確認影像空間 
	if (!ViewImage.Create(dc, WindowWidth, WindowHeight)) { 
		MessageBox("沒有足夠的記憶體空間。/n" 
			"請先結束其他使用中的應用程式後,再重新啟動。"); 
		return FALSE; 
	} 
 
	// 清除顯示用影像 
	ViewImage.Clear(); 
 
	// 送出觸發的第一個訊息 
	while (!PostMessage(WM_FIRSTACTION)) { 
		// 若佇列已滿,則PostMessage會發生error,故反覆傳送 
#ifdef	_DEBUG 
		TRACE("PostMessage Error code = %d\n", GetLastError()); 
#endif 
		::Sleep(110);	// 重送訊息的動作再稍等一下 
	} 
 
	return TRUE; 
} 
 
// 
// 遊戲的「第一個」動作 
// 
void CMainWin::OnFirstAction() 
{ 
	Battle();		// 戰鬥 
} 
 
// 
// WM_CLOSE的處理 
// 
void CMainWin::OnClose() 
{ 
	if (MessageBox("確定要結束程式了嗎?", ApplicationTitle, 
		MB_ICONQUESTION|MB_OKCANCEL) == IDOK) { 
		::DestroyWindow(hWnd); 
	} 
} 
 
// 
// WM_PAINT的處理(重新繪製) 
// 
void CMainWin::OnPaint() 
{ 
	CPaintDC	dc(this); 
	ViewImage.Draw(dc, dc.ps.rcPaint); 
} 
 
// 
// WM_TIMER的處理 
// 
void CMainWin::OnTimer(int id) 
{ 
	if (Action->TimedOut(id)) 
		KillTimer(id); 
} 
 
// 
// WM_COMMAND的處理(選單處理) 
// 
void CMainWin::OnCommand(UINT notifyCode, UINT id, HWND ctrl) 
{ 
	switch (id) { 
	  case ID_APP_EXIT:			// 結束 
		SendMessage(WM_CLOSE); 
		break; 
 
	  case ID_APP_ABOUT:			// 版本資訊
		CAboutDlg().DoModal(IDD_ABOUT, hWnd); 
		break; 
 
	  default: 
		break; 
	} 
} 
 
// 
// 刪除動作 
// 
void CMainWin::DeleteAllAction() 
{ 
	while (Action) { 
		CAction *action = Action->GetOldAction(); 
		delete Action; 
		Action = action; 
	} 
} 
 
// 
// 戰鬥 
// 
BOOL CMainWin::Battle() 
{ 
	CBattleAction *action = new CBattleAction(this, Action); 
	if (!action->Init()) { 
		delete action; 
		return false; 
	} 
	Action = action; 
	PostMessage(WM_KICKIDLE);	// 為了安全起見,傳送空訊息 
	return true; 
}