www.pudn.com > goodchessGame.zip > FloorDialog.cpp


// FloorDialog.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "FiveInOne.h" 
#include "FloorDialog.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFloorDialog dialog 
 
typedef BOOL (WINAPI *lpfn) (HWND hWnd, COLORREF cr, BYTE bAlpha, DWORD dwFlags); 
lpfn g_pSetLayeredWindowAttributes; 
 
CFloorDialog::CFloorDialog(CWnd* pParent /*=NULL*/) 
	: CDialog(CFloorDialog::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CFloorDialog) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
 
	m_pStartPoints = NULL; 
	Create(IDD, pParent); 
} 
 
CFloorDialog::~CFloorDialog() 
{ 
	POSITION pos = m_ThrownCardList.GetHeadPosition(); 
	while (pos != NULL) 
	{ 
		delete m_ThrownCardList.GetNext(pos); 
	} 
	m_ThrownCardList.RemoveAll(); 
 
	POSITION pos2 = m_BoolList.GetHeadPosition(); 
	while (pos2 != NULL) 
	{ 
		delete m_BoolList.GetNext(pos2); 
	} 
	m_BoolList.RemoveAll(); 
 
	CleanUp(); 
} 
 
void CFloorDialog::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CFloorDialog) 
	DDX_Control(pDX, IDC_SLIDER, m_SliderCtrl); 
	//}}AFX_DATA_MAP 
	DDX_Control(pDX, IDC_EDITMSGBOX, m_EditMsgBox); 
} 
 
 
BEGIN_MESSAGE_MAP(CFloorDialog, CDialog) 
	//{{AFX_MSG_MAP(CFloorDialog) 
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER, OnReleasedcaptureSlider) 
	//}}AFX_MSG_MAP 
	ON_MESSAGE(DM_GETDEFID, OnGetDefID) 
	ON_WM_PAINT() 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFloorDialog message handlers 
 
BOOL CFloorDialog::OnInitDialog()  
{ 
	CDialog::OnInitDialog(); 
	 
	// TODO: Add extra initialization here 
 
	m_VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 
	GetVersionEx(&m_VersionInfo); 
	if(m_VersionInfo.dwMajorVersion > 4) 
	{ 
        //  get the function pointer for SetLayeredWindowAttributes in User32.dll 
		HMODULE hUser32 = GetModuleHandle(_T("USER32.DLL")); 
		g_pSetLayeredWindowAttributes = (lpfn)GetProcAddress(hUser32, "SetLayeredWindowAttributes"); 
	} 
	//  initialize the slider control 
    m_SliderCtrl.SetRange(127, 255); 
    m_SliderCtrl.SetPos(127); 
 
	if(m_VersionInfo.dwMajorVersion > 4) 
	{ 
        ::SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) ^ WS_EX_LAYERED); 
		g_pSetLayeredWindowAttributes(m_hWnd, 0, (BYTE)m_SliderCtrl.GetPos(), LWA_ALPHA); 
	} 
 
	//::RedrawWindow(m_hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN); 
    CenterWindow(); 
	 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void CFloorDialog::OnReleasedcaptureSlider(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
    if(m_VersionInfo.dwMajorVersion > 4) 
	{ 
        ::SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED); 
		g_pSetLayeredWindowAttributes(m_hWnd, 0, (BYTE)m_SliderCtrl.GetPos(), LWA_ALPHA); 
	} 
    *pResult = 0; 
} 
 
LRESULT CFloorDialog::OnGetDefID(WPARAM wp, LPARAM lp)  
{ 
    return MAKELONG(0,DC_HASDEFID);  
} 
void CFloorDialog::OnCancel()  
{ 
	// TODO: Add extra cleanup here 
	DestroyWindow(); 
} 
 
void CFloorDialog::PostNcDestroy() 
{ 
	// TODO: Add your specialized code here and/or call the base class 
 
	delete this; 
 
	CDialog::PostNcDestroy(); 
} 
 
void CFloorDialog::AddMessage(DataPacket &dp) 
{ 
	CString message; 
	message.Format("%d%s%d%s%s%s%d%s%s",dp.pkt.sGameType, " ", dp.pkt.nBitmapIndex, " ", dp.pkt.sPlayerName, " ", dp.pkt.sService, " ", dp.pkt.sData); 
	message += "\r\n"; 
 
	int iTotalLength = m_EditMsgBox.GetWindowTextLength(); 
	m_EditMsgBox.SetSel(iTotalLength, iTotalLength); 
	m_EditMsgBox.ReplaceSel(message); 
	m_EditMsgBox.LineScroll(1); 
} 
 
void CFloorDialog::AddThrownCard(CPlayer *pPlayer, int BitmapIndex) 
{ 
	ThrownCardStruct *pStruct = new ThrownCardStruct; 
	pStruct->s_Bitmap.LoadBitmap(BitmapIndex); 
	pStruct->s_PlayerID = pPlayer->GetPlayerID(); 
	 
	POSITION pos = m_ThrownCardList.GetHeadPosition(); 
 
	while (pos != NULL) 
	{ 
		POSITION posint = pos; 
		ThrownCardStruct *pIntStruct = m_ThrownCardList.GetNext(pos); 
		if(pPlayer->GetPlayerID() == pIntStruct->s_PlayerID) 
		{ 
			delete pIntStruct; 
			m_ThrownCardList.RemoveAt(posint); 
		} 
	} 
	m_ThrownCardList.AddHead(pStruct); 
	InvalidateRect(CRect(m_pStartPoints[pStruct->s_PlayerID - 1], CSize(64, 64))); 
	UpdateWindow(); 
} 
void CFloorDialog::OnPaint() 
{ 
	CPaintDC dc(this); // device context for painting 
	// TODO: Add your message handler code here 
	// Do not call CDialog::OnPaint() for painting messages 
 
	POSITION pos = m_ThrownCardList.GetHeadPosition(); 
	while (pos != NULL) 
	{ 
		ThrownCardStruct *pStruct = m_ThrownCardList.GetNext(pos); 
 
		BITMAP bm; 
		pStruct->s_Bitmap.GetBitmap(&bm); 
		CDC memDC; 
		memDC.CreateCompatibleDC(&dc); 
		memDC.SelectObject(&pStruct->s_Bitmap); 
		dc.StretchBlt(m_pStartPoints[pStruct->s_PlayerID-1].x, m_pStartPoints[pStruct->s_PlayerID-1].y, 64, 64, &memDC, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY);  
		ReleaseDC(&memDC); 
	} 
 
	POSITION pos2 = m_BoolList.GetHeadPosition(); 
	while (pos2 != NULL) 
	{ 
		StatBool *pBool = m_BoolList.GetNext(pos2); 
		dc.SetBkMode(TRANSPARENT); 
		CRect Rect(0,0,0,0); 
		Rect.left = m_pStartPoints[pBool->s_PlayerID-1].x; 
		Rect.top = m_pStartPoints[pBool->s_PlayerID-1].y - 20; 
		CString str; 
		if(pBool->s_Bool) 
			str = _T("Drawn"); 
		else 
			str = _T("Picked"); 
         
		dc.TextOut(Rect.left, Rect.top, str); 
    } 
} 
 
void CFloorDialog::SetStartingPoints(int nNumOfPlayers) 
{ 
	m_nNumOfPlayers = nNumOfPlayers; 
	m_pStartPoints = new CPoint[m_nNumOfPlayers]; 
 
	switch(m_nNumOfPlayers) 
	{ 
	case 2: 
		{ 
			m_pStartPoints[0].x = MARGIN; 
			m_pStartPoints[0].y = MARGIN; 
			m_pStartPoints[1].x = m_pStartPoints[0].x + 64 + MARGIN; 
			m_pStartPoints[1].y = m_pStartPoints[0].y; 
		} 
		break; 
	case 3: 
		{ 
			m_pStartPoints[0].x = MARGIN; 
			m_pStartPoints[0].y = MARGIN; 
			m_pStartPoints[1].x = m_pStartPoints[0].x + 64 + MARGIN; 
			m_pStartPoints[1].y = m_pStartPoints[0].y; 
			m_pStartPoints[2].x = 3*MARGIN + 96; 
			m_pStartPoints[2].y = m_pStartPoints[0].y + 64 + MARGIN; 
		} 
		break; 
	case 4: 
		{ 
			m_pStartPoints[0].x = MARGIN; 
			m_pStartPoints[0].y = MARGIN; 
			m_pStartPoints[1].x = m_pStartPoints[0].x + 64 + MARGIN; 
			m_pStartPoints[1].y = m_pStartPoints[0].y; 
			m_pStartPoints[2].x = MARGIN; 
			m_pStartPoints[2].y = m_pStartPoints[0].y + 64 + MARGIN; 
			m_pStartPoints[3].x = m_pStartPoints[0].x + 64 + MARGIN; 
			m_pStartPoints[3].y = m_pStartPoints[0].y + 64 + MARGIN; 
		} 
		break; 
	} 
} 
 
void CFloorDialog::Refresh() 
{ 
	POSITION pos = m_ThrownCardList.GetHeadPosition(); 
 
	while (pos != NULL) 
	{ 
		delete m_ThrownCardList.GetNext(pos); 
	} 
	m_ThrownCardList.RemoveAll(); 
 
	POSITION pos2 = m_BoolList.GetHeadPosition(); 
 
	while (pos2 != NULL) 
	{ 
		delete m_BoolList.GetNext(pos2); 
	} 
	m_BoolList.RemoveAll(); 
	Invalidate(); 
	UpdateWindow(); 
} 
 
void CFloorDialog::ShowStat(CPlayer *pPlayer, BOOL bDrawn) 
{ 
	StatBool *pBool = new StatBool; 
	pBool->s_Bool = bDrawn; 
	pBool->s_PlayerID = pPlayer->GetPlayerID(); 
	 
	POSITION pos = m_BoolList.GetHeadPosition(); 
 
	while (pos != NULL) 
	{ 
		POSITION posint = pos; 
		StatBool *pIntBool = m_BoolList.GetNext(pos); 
		if(pPlayer->GetPlayerID() == pIntBool->s_PlayerID) 
		{ 
			delete pIntBool; 
			m_BoolList.RemoveAt(posint); 
		} 
	} 
	m_BoolList.AddHead(pBool); 
	CRect Rect(0,0,0,0); 
	Rect.left = m_pStartPoints[pPlayer->GetPlayerID()-1].x; 
	Rect.top = m_pStartPoints[pPlayer->GetPlayerID()-1].y - 20; 
	Rect.right = Rect.left + 50; 
	Rect.bottom = Rect.top + 20; 
	InvalidateRect(&Rect); 
	UpdateWindow(); 
}