//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "GlobalUnit.h"
#include "QRcodePanel.h"

#define		LEN_BKG_EXTERNAL_ROUND        1
#define		LEN_BKG_EXTERNAL_FRMAE        1
const COLORREF CLR_BKG_EXTERNAL_ROUND      =  RGB(107, 115, 126),
			   CLR_BKG_EXTERNAL_FRMAE      =  RGB(145, 156, 170),
			   CLR_BKG_INTERNAL 	       =  RGB(255, 255, 255 );
//---------------------------------------------------------------------------
#pragma package(smart_init)

__fastcall TQRcodePanel::TQRcodePanel(TComponent* AOwner, String strTitle )
	: TPanel(AOwner)
{
	m_strTitle = strTitle;
	m_nQRwidth = 0;
	m_pQRdata = NULL;
}
//---------------------------------------------------------------------------

__fastcall TQRcodePanel::~TQRcodePanel()
{

}
//---------------------------------------------------------------------------

void __fastcall TQRcodePanel::CreateWnd(void)
{
	Vcl::Extctrls::TPanel::CreateWnd();
}
//---------------------------------------------------------------------------

void __fastcall TQRcodePanel::WMEraseBkgnd(Winapi::Messages::TWMEraseBkgnd &Message)
{
	TRect rect = GetClientRect();

	TCanvas* tempCanvas = new TCanvas;
	tempCanvas->Handle = Message.DC;

	TRect rectFill = rect;
	rectFill.Top    = rectFill.Top    + LEN_BKG_EXTERNAL_ROUND;
	rectFill.Left   = rectFill.Left   + LEN_BKG_EXTERNAL_ROUND;
	rectFill.Right  = rectFill.Right  - LEN_BKG_EXTERNAL_ROUND;
	rectFill.Bottom = rectFill.Bottom - LEN_BKG_EXTERNAL_ROUND;

	tempCanvas->Brush->Color = CLR_BKG_INTERNAL;

	tempCanvas->FillRect( rectFill );

	//==========画圆角
	int X,Y;
	//上面的
	Y         				 = 0;
	X            			 = 0;
	tempCanvas->Pixels[X][Y] = CLR_BKG_EXTERNAL_ROUND;
	X         				 = Width - LEN_BKG_EXTERNAL_ROUND;
	tempCanvas->Pixels[X][Y] = CLR_BKG_EXTERNAL_ROUND;

	//下面的
	Y            			 = Height - LEN_BKG_EXTERNAL_ROUND;

	X         				 = 0;
	tempCanvas->Pixels[X][Y] = CLR_BKG_EXTERNAL_ROUND;
	X         				 = Width - LEN_BKG_EXTERNAL_ROUND;
	tempCanvas->Pixels[X][Y] = CLR_BKG_EXTERNAL_ROUND;

	//===========画边框
	tempCanvas->Pen->Color   = CLR_BKG_EXTERNAL_FRMAE;

	tempCanvas->MoveTo( LEN_BKG_EXTERNAL_FRMAE, 	  0 );
	tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, 0 );

	tempCanvas->MoveTo( LEN_BKG_EXTERNAL_FRMAE,       Height-LEN_BKG_EXTERNAL_FRMAE );
	tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, Height-LEN_BKG_EXTERNAL_FRMAE );

	tempCanvas->MoveTo( 0, 					          LEN_BKG_EXTERNAL_FRMAE );
	tempCanvas->LineTo( 0, 					          Height-LEN_BKG_EXTERNAL_FRMAE );

	tempCanvas->MoveTo( Width-LEN_BKG_EXTERNAL_FRMAE, LEN_BKG_EXTERNAL_FRMAE );
	tempCanvas->LineTo( Width-LEN_BKG_EXTERNAL_FRMAE, Height-LEN_BKG_EXTERNAL_FRMAE );

	//绘制标题
	tempCanvas->Font->Size = g_szFont9;
	tempCanvas->Font->Color = clBlack;

	TSize size = tempCanvas->TextExtent( m_strTitle );
	int x = (Width - size.Width) / 2,
		y = Height - 5 - size.Height;

	tempCanvas->TextOut( x, y, m_strTitle );

	delete tempCanvas;

	Message.Result = true;
}
//---------------------------------------------------------------------------

void __fastcall TQRcodePanel::Paint()
{
	int scale = (Width - 16) / m_nQRwidth;

	int left = ( Width - m_nQRwidth * scale ) / 2,
		top = ( Height - 16 - m_nQRwidth * scale ) / 2;

	unsigned char *pSrcData = m_pQRdata;

	for( int i=0; i<m_nQRwidth; i++ ){
		for( int j=0; j<m_nQRwidth; j++ ){
			int y = top + i * scale;
			int x = left + j * scale;

			if( *pSrcData & 1 ){
				for( int m=0; m<scale; m++ ){
					for( int n=0; n<scale; n++ ){
						Canvas->Pixels[x+n][y+m] = clBlack;
					}
				}
			}
			pSrcData++;
		}
	}
}
//---------------------------------------------------------------------------

