// GDICanvas.cpp: implementation of the CGDICanvas class.
#include "GDICanvas.h"

// Construction/Destruction
CGDICanvas::CGDICanvas()
{
	//initialize all members to NULL or 0
	hdcMem	= NULL;
	hbmNew	= NULL;
	hbmOld	= NULL;
	nWidth	= 0;
	nHeight	= 0;
}
CGDICanvas::~CGDICanvas()
{
	//if the hdcMem has not been destroyed, do so
	if(hdcMem) Destroy();
}
//Creation/Loading
//loads bitmap from a file
void CGDICanvas::Load(HDC hdcCompatible, LPCTSTR lpszFilename)
{
	if(hdcMem) Destroy();																	//if not null, destroy
	hdcMem	= CreateCompatibleDC(hdcCompatible);											//create the memory dc
	hbmNew	= (HBITMAP)LoadImage(NULL, lpszFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);	//load the image
	hbmOld	= (HBITMAP)SelectObject(hdcMem, hbmNew);//select the image into the dc
	BITMAP bmp;	GetObject(hbmNew, sizeof(BITMAP), (LPVOID)&bmp);							//fetch the bitmaps properties
	nWidth	= bmp.bmWidth;																	//assign height and width
	nHeight	= bmp.bmHeight;
}
//creates a blank bitmap
void CGDICanvas::CreateBlank(HDC hdcCompatible, int width, int height)
{
	if(hdcMem) Destroy();																	//if the hdcMem is not null, destroy
	hdcMem	= CreateCompatibleDC(hdcCompatible);											//create the memory dc
	hbmNew	= CreateCompatibleBitmap(hdcCompatible, width, height);							//create the image
	hbmOld	= (HBITMAP)SelectObject(hdcMem, hbmNew);										//select the image into the dc
	nWidth	= width;																		//assign the width and height
	nHeight	= height;
}
//////////////////////////////////////////////////////////////////////
//Clean-up
//////////////////////////////////////////////////////////////////////
//destroys bitmap and dc
void CGDICanvas::Destroy()
{
	SelectObject(hdcMem, hbmOld);	//restore old bitmap
	DeleteObject(hbmNew);			//delete new bitmap
	DeleteDC(hdcMem);				//delete device context
	hdcMem	= NULL;					//set all members to 0 or NULL
	hbmNew	= NULL;
	hbmOld	= NULL;
	nWidth	= 0;
	nHeight	= 0;
}
//////////////////////////////////////////////////////////////////////
//Conversion
//////////////////////////////////////////////////////////////////////
//converts to HDC
CGDICanvas::operator HDC()
{
	//return the hdcMem member
	return(hdcMem);
}
//////////////////////////////////////////////////////////////////////
//Returning information
//////////////////////////////////////////////////////////////////////
//return width
int CGDICanvas::GetWidth()
{
	//return the width
	return(nWidth);
}
//return height
int CGDICanvas::GetHeight()
{
	//return the height
	return(nHeight);
}