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


// 
//	Script player main window 
// 
//		Copyright (c) 2000-2001 Chihiro.SAKAMOTO (HyperWorks) 
// 
#include "StdAfx.h" 
#include  
#include "Sample.h" 
#include "Window.h" 
#include "MainWin.h" 
#include "resource.h" 
 
// 
// 產生視窗的前置處理 
// 
//	設定樣式與大小 
// 
BOOL CMainWin::PreCreateWindow(CREATESTRUCT &cs) 
{ 
	cs.dwExStyle = WS_EX_CLIENTEDGE; 
	cs.style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; 
 
	// 求出視窗大小 
	CRect	rect(0, 0, 640, 480); 
	::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_CLOSE:			// 關閉視窗 
		OnClose(); 
		break; 
 
	  case WM_ERASEBKGND:		// 去背景 
		return FALSE;			// 無動作 
 
	  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, 640, 480)) { 
		MessageBox("記憶體不能確保影像區域\n" 
			"請關閉其他開啟的應用程式再重新啟動"); 
		return FALSE; 
	} 
 
	{ 
		CFile	file("map.bmp");		// 開啟範例CG圖檔 
		if (!file || !Back.LoadBMP(file)) { 
			MessageBox("無法讀取CG圖檔。"); 
			return FALSE; 
		} 
	} 
	{ 
		CFile	file("sprite.bmp");	// 開啟要重疊貼圖的零件式CG圖檔 
		if (!file || !SpriteImage.LoadBMP(file)) { 
			MessageBox("無法讀取要用來重疊貼圖的CG圖檔。"); 
			return FALSE; 
		} 
	} 
 
	CSize size = SpriteImage.Size(); 
	Sprite[0].Set(&SpriteImage, CPoint(100, 100), size); 
	Sprite[1].Set(&SpriteImage, CPoint(150, 100), size); 
	Sprite[2].Set(&SpriteImage, CPoint(100, 150), size); 
	Sprite[3].Set(&SpriteImage, CPoint(150, 150), size); 
 
	// 背景複製 
	ViewImage.Copy(&Back); 
	// 重疊 
	for (int i=0; i<4; i++) 
		Sprite[i].Draw(&ViewImage); 
 
	return TRUE; 
} 
 
// 
// WM_CLOSE 的處理 
// 
void CMainWin::OnClose() 
{ 
	if (MessageBox("要結束程式了嗎?", "Sample", 
		MB_ICONQUESTION|MB_OKCANCEL) == IDOK) { 
		::DestroyWindow(hWnd); 
	} 
} 
 
// 
// WM_PAINT 的處理(重新繪製) 
// 
void CMainWin::OnPaint() 
{ 
	CPaintDC	dc(this); 
	ViewImage.Draw(dc, dc.ps.rcPaint); 
} 
 
// 
// WM_COMMAND 的處理(處理功能表選單) 
// 
void CMainWin::OnCommand(UINT notifyCode, UINT id, HWND ctrl) 
{ 
	switch (id) { 
	  case ID_APP_EXIT:				// 結束 
		SendMessage(WM_CLOSE); 
		break; 
 
	  default: 
		break; 
	} 
}