#include <common/drawbmp.hpp>

DrawBitmap::DrawBitmap(void)
{
}

DrawBitmap::~DrawBitmap()
{
}

void DrawBitmap::drawBitmap(HBITMAP hBitmap)const
{
	if(!hBitmap)return;
	HWND hFocusWnd(::GetDesktopWindow());
	drawBitmap(hFocusWnd,hBitmap);
}

void DrawBitmap::drawBitmap(HWND hDrawWindow,HBITMAP hBitmap)const
{
	HDC hDC;
	RECT windowRect;
	BITMAP bm;

	if(!hBitmap||!hDrawWindow)return;
	hDC=::GetDC(hDrawWindow);
	::GetObject(hBitmap,sizeof(BITMAP),(LPSTR)&bm);
	::GetWindowRect(hDrawWindow,(RECT FAR *)&windowRect);
	windowRect.left=(((windowRect.right-windowRect.left))/2)-(bm.bmWidth/2)-1;
	windowRect.top=(((windowRect.bottom-windowRect.top))/2)-(bm.bmHeight/2)-1;
	windowRect.right=bm.bmWidth;
	windowRect.bottom=bm.bmHeight;
	drawBitmap(hDC,hBitmap,windowRect,FALSE);
	::ReleaseDC(hDrawWindow,hDC);
}

void DrawBitmap::drawBitmap(HDC hDC,HBITMAP hBitmap,RECT &drawRect,int initRect)const
{
	HDC hMemDC;

	if(!hBitmap)return;
	if(initRect)
	{
		BITMAP bm;
		::GetObject(hBitmap,sizeof(BITMAP),(LPSTR)&bm);
    drawRect.left=0;
		drawRect.top=0;
		drawRect.right=bm.bmWidth;
		drawRect.bottom=bm.bmHeight;
  }
	hMemDC=::CreateCompatibleDC(hDC);
	::SelectObject(hMemDC,hBitmap);
	::BitBlt(hDC,drawRect.left,drawRect.top,drawRect.right,drawRect.bottom,hMemDC,0,0,SRCCOPY);
	::DeleteDC(hMemDC);
	return;
}

void DrawBitmap::centerRect(HBITMAP hBitmap,RECT &windowRect)const
{
	BITMAP bm;

	if(!hBitmap)return;
	::GetObject(hBitmap,sizeof(bm),(LPSTR)&bm);
	::GetWindowRect(::GetDesktopWindow(),(RECT FAR *)&windowRect);
	windowRect.left=(((windowRect.right-windowRect.left))/2)-(bm.bmWidth/2)-1;
	windowRect.top=(((windowRect.bottom-windowRect.top))/2)-(bm.bmHeight/2)-1;
	windowRect.right=bm.bmWidth+2;
	windowRect.bottom=bm.bmHeight+2;
}
