highscoredlg.cpp

来自「该程序功能:用JAVa实现的彩色五珠棋小游戏」· C++ 代码 · 共 287 行

CPP
287
字号
// HighScoreDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FivePlus.h"
#include "HighScoreDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHighScoreDlg dialog


CHighScoreDlg::CHighScoreDlg(CWnd* pParent /*=NULL*/, CHighScoreModel* highScore)
	: CDialog(CHighScoreDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHighScoreDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_scoreModel = highScore;
}


void CHighScoreDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHighScoreDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CHighScoreDlg, CDialog)
	//{{AFX_MSG_MAP(CHighScoreDlg)
	ON_BN_CLICKED(ID_DOWNLOAD_SCORE, OnDownloadScore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHighScoreDlg message handlers

BOOL CHighScoreDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	LoadScore();
	m_bDownload = TRUE;
	m_bHasConnect = FALSE;
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CHighScoreDlg::OnDownloadScore() 
{
	// TODO: Add your control notification handler code here
	Connect("http://mssoapsampleserver/FiveSharp/ScoreBoard.WSDL",
		"http://mssoapsampleserver/FiveSharp/ScoreBoardClient.WSML");
	if ( m_bDownload )
	{
		BOOL canUpload = DownloadScore();
		m_bDownload = FALSE;
		CWnd* btnUpload = this->GetDlgItem(ID_DOWNLOAD_SCORE);
		btnUpload->SetWindowText("上载你的分数");		
		btnUpload->EnableWindow(canUpload);
	}
	else
	{
		this->UploadScore();
		CWnd* btnUpload = this->GetDlgItem(ID_DOWNLOAD_SCORE);		
		btnUpload->EnableWindow(FALSE);
	}	
	
}

void CHighScoreDlg::LoadScore()
{
	if ( m_scoreModel ){
		CWnd* localScore = GetDlgItem(IDC_STATIC_LOCAL);
		localScore->SetWindowText(m_scoreModel
			->GetString());
	}
}

BOOL CHighScoreDlg::DownloadScore()
{
	VARIANTARG	result;
	HRESULT		hr;
	WCHAR		*pName = L"Download";
	DISPID		dispidFn;
	DISPPARAMS	parms;	
	CString		xmlFileName;

	xmlFileName = "testdownload.xml";
	if (xmlFileName == "")	 // user cancellation 
		return FALSE;

	VariantInit(&result);
	parms.cArgs = 0;
	parms.rgvarg = NULL;
	parms.cNamedArgs = 0;
	parms.rgdispidNamedArgs = NULL;


	if ( FAILED( hr	= m_pSoapClient->GetIDsOfNames(IID_NULL, &pName, 1, LOCALE_SYSTEM_DEFAULT, &dispidFn)))
	{
        DisplayHResult(_T("Getting function ID failed"), hr);
	}

	hr = m_pSoapClient->Invoke(2, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
		&parms, &result, 0, 0);
    //hr	= m_pSoapClient->Invoke(dispidFn, IID_NULL, LOCALE_SYSTEM_DEFAULT, 
	//DISPATCH_METHOD, &parms, &result, 0, 0);
	if(FAILED(hr)) 
	{
		DisplayHResult(_T("Invoke of Download service failed."), hr);
	}
	else
	{
		// Check type of result vs. expected type
		if ((VARENUM)V_VT(&result) != ((VARENUM)VT_ARRAY | (VARENUM)VT_DISPATCH))
			DisplayFault(_T("Result of Export method is not of expected type (Array of IDispatch) !"));
		else
		{
			IXMLDOMDocument2	*DDoc = NULL;
			IXMLDOMNodePtr		pNode, psubNode, psubsubNode; 
			VARIANTARG			subresult[C_SCORERECORD_PROPERTIES];
			DISPID				dispidPrs[C_SCORERECORD_PROPERTIES];
			IDispatch			*pID = NULL;
			SAFEARRAY			*parr; // Array of Addresses


			if FAILED(hr = CoCreateInstance(__uuidof(FreeThreadedDOMDocument30), NULL, CLSCTX_INPROC_SERVER,
											__uuidof(IXMLDOMDocument2), (void **)&DDoc)) 
			{
				DisplayHResult(_T("Cannot create DOMDocument."), hr);
				return FALSE;
			}
			DDoc->documentElement = DDoc->createElement("ScoreBoard");

			parr = result.parray;
			
			// Process Addresses one by one
			//		First access properties
			//		then create corresponding Document elements
			if ( parr ) // if returned array has elements
			{
				for (unsigned int i = 0; i < parr->rgsabound->cElements; i++) 
				{
					SafeArrayGetElement(parr, (LONG*)&i, &pID);
					pID->AddRef();

					for (int i = 0; i < C_SCORERECORD_PROPERTIES; i++) {
						VariantInit(&subresult[i]);
						if ( FAILED( hr	= pID->GetIDsOfNames(IID_NULL, (unsigned short**)&ScoreRecordPropertyNames[i],
															 1, LOCALE_SYSTEM_DEFAULT, &dispidPrs[i])))
						{
							DisplayHResult("Getting function ID out of returned VTDispatch failed", hr);
						}
						else 
						{
							if ( FAILED(hr	= pID->Invoke(dispidPrs[i], IID_NULL, LOCALE_SYSTEM_DEFAULT, 
											  DISPATCH_PROPERTYGET, &parms, &subresult[i], 0, 0)) )
								DisplayHResult("Invoke for PropGet failed.", hr);
						}
					}

					pNode = DDoc->createElement("ScoreRecord");

					psubNode = DDoc->createElement("Score");
					VariantChangeType(&subresult[0], &subresult[0], 0, VT_BSTR);					
					psubNode->put_text(V_BSTR(&subresult[0]));	
					pNode->appendChild(psubNode);
			
					psubNode = DDoc->createElement("Name");
					psubNode->put_text(V_BSTR(&subresult[1]));	
					pNode->appendChild(psubNode);

										

					DDoc->documentElement->appendChild(pNode);
				
					pID->Release();
					for (i = 0; i < C_SCORERECORD_PROPERTIES; i++)
						VariantClear(&subresult[i]);
				}
			} // if ( parr )

			DDoc->save((LPCTSTR) xmlFileName);
			DDoc->Release();
		}
	}

	VariantClear(&result);	
	return TRUE;
}

BOOL CHighScoreDlg::UploadScore()
{
	return TRUE;
}

BOOL CHighScoreDlg::Connect(CString WSDLStr, CString WSMLStr)
{
	if ( !m_bHasConnect )
	{
		HRESULT hr;
		if (m_pSoapClient)
			m_pSoapClient.Release();
		hr = m_pSoapClient.CreateInstance(__uuidof(SoapClient));
		if(FAILED(hr))
		{
			DisplayHResult(_T("Cannot create SoapClient."), hr);
			return false;
		}
		try {
			hr = m_pSoapClient->mssoapinit((LPCTSTR)WSDLStr, _T(""), _T(""), (LPCTSTR)WSMLStr);
		
			if(FAILED(hr))
			{
				DisplayHResult(_T("Cannot initialize SoapClient. "), hr);
				return false;
			}			
		}
		catch(_com_error x){
			DisplayFault(_T(x.Description()));
			return false;
		}
		
	}
	return TRUE;
}

void CHighScoreDlg::DisplayHResult(LPCTSTR pMessage, HRESULT hr)
{
	if (hr != 0)
	{
		LPVOID lpMsgBuf;
		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			hr,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
			(LPTSTR) &lpMsgBuf,
			0,
			NULL 
		);
		CString Msg = pMessage;
		Msg += " ";
		Msg += (LPCTSTR)lpMsgBuf;
		MessageBox(Msg, "Error", MB_OK | MB_ICONEXCLAMATION);
		LocalFree(lpMsgBuf);
	}
	else
	{
		MessageBox(pMessage, "Error", MB_OK | MB_ICONEXCLAMATION);
	}
}

void CHighScoreDlg::DisplayFault(LPCTSTR pMessage)
{
	HRESULT	hr;
	BSTR	FaultString;
	
	hr = m_pSoapClient->get_faultstring(&FaultString);
	if( FAILED(hr) )
	{
		DisplayHResult(_T("Cannot get FaultString."), hr);
		return;
	}

	CString Msg = pMessage;
	Msg += " ";
	Msg += FaultString;

	SysFreeString(FaultString);

	MessageBox(Msg, "Error", MB_OK | MB_ICONEXCLAMATION);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?