#include "StdAfx.h"
#include "mappeduserinterface.h"
CMappedUserInterface::CMappedUserInterface(UINT nVisBmpID, UINT nTemplateBmpID, CWnd* pDraw)
{
VisibleBMP.LoadBitmap(nVisBmpID);
VisibleBMP.GetBitmap(&bmpVisible);
TemplateBMP.LoadBitmap(nTemplateBmpID);
TemplateBMP.GetBitmap(&bmpTmpl);
bShowTemplate = FALSE;
pDrawWnd = pDraw;
}
CMappedUserInterface::~CMappedUserInterface(void)
{
VisibleBMP.DeleteObject();
TemplateBMP.DeleteObject();
for (int i=0; i<locationList.GetCount(); i++)
{
CMUILocation* pLocation = (CMUILocation*) locationList.GetAt(i);
delete pLocation;
pLocation = NULL;
}
locationList.RemoveAll();
locationList.FreeExtra();
}
void CMappedUserInterface::DrawMUI(CDC* pDC)
{
CDC srcDC;
CBitmap* pOldBmp = NULL;
CBitmap* pOldSrcBmp = NULL;
CBitmap newBmp;
int iOldStretchBltMode;
CDC templateDC;
CDC visibleDC;
CRect rect;
pDrawWnd->GetClientRect(&rect);
// create compatible DCs
templateDC.CreateCompatibleDC(pDC);
visibleDC.CreateCompatibleDC(pDC);
srcDC.CreateCompatibleDC(pDC);
// create a sized bitmap for the memory DCs
newBmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
if (!bShowTemplate)
{
// stretch the visible BMP onto the DC
pOldBmp = visibleDC.SelectObject(&newBmp);
pOldSrcBmp = srcDC.SelectObject(&VisibleBMP);
iOldStretchBltMode = visibleDC.SetStretchBltMode(HALFTONE);
visibleDC.StretchBlt(0, 0, rect.Width(), rect.Height(), &srcDC, 0, 0, bmpVisible.bmWidth,
bmpVisible.bmHeight, SRCCOPY);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &visibleDC, 0, 0, SRCCOPY);
visibleDC.SetStretchBltMode(iOldStretchBltMode);
srcDC.SelectObject(pOldSrcBmp);
visibleDC.SelectObject(pOldBmp);
}
else
{
// stretch the template BMP onto the DC
pOldBmp = templateDC.SelectObject(&newBmp);
pOldSrcBmp = srcDC.SelectObject(&TemplateBMP);
iOldStretchBltMode = templateDC.SetStretchBltMode(HALFTONE);
templateDC.StretchBlt(0, 0, rect.Width(), rect.Height(), &srcDC, 0, 0, bmpTmpl.bmWidth,
bmpTmpl.bmHeight, SRCCOPY);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &templateDC, 0, 0, SRCCOPY);
templateDC.SetStretchBltMode(iOldStretchBltMode);
srcDC.SelectObject(pOldSrcBmp);
templateDC.SelectObject(pOldBmp);
}
// delete/release stuff
newBmp.DeleteObject();
templateDC.DeleteDC();
visibleDC.DeleteDC();
srcDC.DeleteDC();
}
CMUILocation* CMappedUserInterface::GetLocationFromColor(COLORREF crColor)
{
CMUILocation* pLocation = NULL;
// loop through each location in our list and find the matching color
for (int i = 0; i < locationList.GetSize(); i++)
{
pLocation = (CMUILocation*) locationList.GetAt(i);
if (pLocation->GetColor() == crColor)
break;
else
pLocation = NULL;
}
return pLocation;
}
COLORREF CMappedUserInterface::GetTemplateBMPColorAtPoint(CPoint pt)
{
CDC* pDC = NULL;
CDC memDC;
CDC srcDC;
COLORREF crColorAtPoint = 0;
CBitmap* pOldBmp = NULL;
CBitmap* pOldSrcBmp = NULL;
CBitmap newBmp;
CRect rect;
int iOldStretchBltMode;
pDrawWnd->GetClientRect(&rect);
pDC = pDrawWnd->GetDC();
memDC.CreateCompatibleDC(pDC);
srcDC.CreateCompatibleDC(pDC);
// create a sized bitmap for the memory DC
newBmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
pOldBmp = memDC.SelectObject(&newBmp);
pOldSrcBmp = srcDC.SelectObject(&TemplateBMP);
// stretch the template bitmap onto a compatible DC
iOldStretchBltMode = memDC.SetStretchBltMode(HALFTONE);
memDC.StretchBlt(0, 0, rect.Width(), rect.Height(), &srcDC, 0, 0,
bmpTmpl.bmWidth, bmpTmpl.bmHeight, SRCCOPY);
memDC.SetStretchBltMode(iOldStretchBltMode);
// get the color on the template at given coordinates
crColorAtPoint = GetPixel(memDC.m_hDC, pt.x, pt.y);
srcDC.SelectObject(pOldSrcBmp);
memDC.SelectObject(pOldBmp);
newBmp.DeleteObject();
memDC.DeleteDC();
srcDC.DeleteDC();
pDrawWnd->ReleaseDC(pDC);
return crColorAtPoint;
}