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

#include "BKGMemoUnit.h"
#include "GlobalUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
static const COLORREF
			 CLR_BKG 	        =  RGB(228, 232, 237);//168, 180, 196);
//---------------------------------------------------------------------------
__fastcall BKGMemoUnit::TMemo::TMemo(System::Classes::TComponent* AOwner)
 : Vcl::Stdctrls::TMemo(AOwner)
{
	ControlStyle >> csOpaque;

//	FTransparent = true;
	m_pbmpBkg    = new TBitmap();

	//ÉèÖÃ±ÊË¢£¬ÓÃ×÷±³¾°
	m_pbmpBkg->Width  = 2;
	m_pbmpBkg->Height = 2;

	m_pbmpBkg->Canvas->Brush->Color = CLR_BKG;
	m_pbmpBkg->Canvas->Pen->Color   = CLR_BKG;
	m_pbmpBkg->Canvas->Rectangle(0, 0, m_pbmpBkg->Width, m_pbmpBkg->Height);

	Brush->Bitmap = m_pbmpBkg;

}

//---------------------------------------------------------------------------
__fastcall BKGMemoUnit::TMemo::~TMemo()
{
	if(m_pbmpBkg)
		delete m_pbmpBkg;
}
//---------------------------------------------------------------------------

void __fastcall BKGMemoUnit::TMemo::CreateWnd(void)
{
	Vcl::Stdctrls::TMemo::CreateWnd();

	if( FTransparent ){
		SetWindowLong( Parent->Handle, GWL_STYLE,
			GetWindowLong(Parent->Handle, GWL_STYLE) & ~WS_CLIPCHILDREN  );
	}
}

//---------------------------------------------------------------------------
void __fastcall BKGMemoUnit::TMemo::CreateParams(Vcl::Controls::TCreateParams &Params)
{
	Vcl::Stdctrls::TMemo::CreateParams( Params );
	Params.ExStyle |= WS_EX_TRANSPARENT;
}
//---------------------------------------------------------------------------

void __fastcall BKGMemoUnit::TMemo::Invalidate()
{
	if( FTransparent )
		SendMessage( Handle, UM_TEINVALIDATE, 0, 0 );
	else
		Vcl::Stdctrls::TMemo::Invalidate();
}
//---------------------------------------------------------------------------

void __fastcall BKGMemoUnit::TMemo::WMEraseBkgnd(TWMEraseBkgnd &Message)
{

	if( Parent && FTransparent )
		PostMessage( Handle, UM_TEINVALIDATE, 0, 0 );
	else
		DefaultHandler( &Message );

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

void __fastcall BKGMemoUnit::TMemo::CNCtlColorEdit(TWMCtlColorEdit &Message)
{
	if( FTransparent ){

        SetBkColor(Message.ChildDC, CLR_BKG);
//		SetBkMode( Message.ChildDC, TRANSPARENT);
		Font->Size   = 8;
	  	Message.Result = (int)Brush->Handle;
	}
	else
		DefaultHandler( &Message );
}

//---------------------------------------------------------------------------
void __fastcall BKGMemoUnit::TMemo::TEInvalidate(TMessage &Message)
{
	if( Parent && FTransparent ){
		TRect r = GetClientRect();
		TPoint& pTL = r.TopLeft();
		pTL = Parent->ScreenToClient( ClientToScreen(pTL) );
		TPoint& pBR = r.BottomRight();
		pBR = Parent->ScreenToClient( ClientToScreen(pBR) );

		RedrawWindow( Handle, &r, 0, RDW_FRAME|RDW_INVALIDATE );
	}
	else
		Vcl::Stdctrls::TMemo::Invalidate();
}

//---------------------------------------------------------------------------

