// Question.cpp : implementation file
//

#include "stdafx.h"
#include "bkohnet.h"
#include "Question.h"
#pragma comment(lib, "gdiplus.lib")

// CQuestion dialog

IMPLEMENT_DYNAMIC(CQuestion, CDialog)

CQuestion::CQuestion(CWnd* pParent /*=NULL*/)
	: CDialog(CQuestion::IDD, pParent)
{
	CreateStreamOnHGlobal(NULL, TRUE, &m_pStream);
	m_pImage = NULL;
}

CQuestion::~CQuestion()
{
	if ( m_pImage )
		delete(m_pImage);
	m_pStream->Release();
}

void CQuestion::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	if ( pDX->m_bSaveAndValidate )
	{
		CString res;
		DDX_Text(pDX, IDC_EDITUSER, res);
		m_wcRes = res[0];
	}
	else
	{	// nacitani do dialogu
		CStatic *pStatic = (CStatic*)GetDlgItem(IDC_PICTURE);
		ULARGE_INTEGER ulnSize;
		LARGE_INTEGER lnOffset;
		ulnSize.QuadPart = m_dwBitmapLen;
		lnOffset.QuadPart = 0;
		m_pStream->SetSize(ulnSize);
		m_pStream->Seek(lnOffset, STREAM_SEEK_SET, NULL);
		m_pStream->Write(m_pBitmap, m_dwBitmapLen, NULL);	
		if ( m_pImage )
			delete(m_pImage);
		m_pImage = new Image(m_pStream);
		
		CString res;
		if ( m_wcRes == (WCHAR)-1 )
			res = L"No data in NN";
		else
			res.Format(L"%c %04x", m_wcRes, (int)m_wcRes);
		DDX_Text(pDX, IDC_STATICACTUAL, res);
		res.Format(L"%c", m_wcRes);
		DDX_Text(pDX, IDC_EDITUSER, res);
	}
}


BEGIN_MESSAGE_MAP(CQuestion, CDialog)
	ON_WM_PAINT()
END_MESSAGE_MAP()


// CQuestion message handlers

void CQuestion::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	CStatic *pPicPlace = (CStatic*)GetDlgItem(IDC_PICTURE);

	RECT rc;
	pPicPlace->GetWindowRect(&rc);
	ScreenToClient(&rc);

	PointF pos;
	pos.X = (REAL)(( rc.right - rc.left ) / 2 + rc.left - (int)m_pImage->GetWidth()/2); 
	pos.Y = (REAL)(( rc.bottom - rc.top ) / 2 + rc.top - (int)m_pImage->GetHeight()/2); 

	Graphics myGraphics(dc.m_hDC);
	myGraphics.DrawImage(m_pImage, pos);
}
