/************************************************************************ 
* ��������    ����QQ������ 
* �����ˣ�    ���ƽ, 2011��04��11��
* �汾�ţ�    1.0 
************************************************************************/ 
// SkinEdit.cpp : ʵ���ļ�
//

#include "stdafx.h"
#include "SkinEdit.h"


// CSkinEdit

IMPLEMENT_DYNAMIC(CSkinEdit, CEdit)

CSkinEdit::CSkinEdit()
{

}

CSkinEdit::~CSkinEdit()
{
}


BEGIN_MESSAGE_MAP(CSkinEdit, CEdit)
	ON_WM_PAINT()
	ON_WM_NCCALCSIZE()
	ON_WM_NCLBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_WM_NCHITTEST()
END_MESSAGE_MAP()


void CSkinEdit::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	CRect rcClient;
	GetClientRect(&rcClient);
	rcClient.left -= 15;
	rcClient.right += 33;
	rcClient.top -= 5;
	rcClient.bottom += 5;

	if (m_bmpEdit.m_hObject !=0)
		m_bmpEdit.ExtendDraw(&dc,rcClient,m_nX,m_nY); 

	CRect rc=rcClient;
	rc.left = 0;
	CString strText;
	GetWindowText(strText);
	DrawText(&dc,rc,strText);
}


// CSkinEdit ��Ϣ��������
void CSkinEdit::LoadBitmap(LPCTSTR szImagePath)
{
	m_bmpEdit.LoadImage(szImagePath); 
}

void CSkinEdit::SetText(CString strText, int nStart,COLORREF cr)
{
	m_strText=strText;
	m_nTextStart=nStart;
	m_crText=cr;
	SetWindowText(strText);
}


BOOL CSkinEdit::DrawText(CDC *pDC,CRect rc ,CString strText)
{
	CRect r;
	CString str;
	CRect rcButton;
	if (strText.GetLength() )
	{
		CFont *ofont;
		ofont = pDC->SelectObject( GetParent()->GetFont() );

		pDC->SetTextColor(m_crText);
		pDC->SetBkMode(TRANSPARENT);
		pDC->DrawText( strText, rc, DT_SINGLELINE | DT_VCENTER );
		pDC->SelectObject(ofont);
	}

	return TRUE;
}

CRect CSkinEdit::GetRectInParent()
{
	CRect rcWindowParent;
	GetParent()->GetWindowRect(rcWindowParent);//client
	CRect rcWindow;
	GetWindowRect(&rcWindow);
	CRect rect;
	rect.left = rcWindow.left-rcWindowParent.left;
	rect.top = rcWindow.top-rcWindowParent.top;
	rect.right = rcWindow.right-rcWindowParent.left;
	rect.bottom = rcWindow.bottom-rcWindowParent.top;

	return rect;
}

void CSkinEdit::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
{
	// TODO: �ڴ�������Ϣ������������/�����Ĭ��ֵ
	lpncsp->rgrc->left += 15;
	lpncsp->rgrc->right -= 33;
	lpncsp->rgrc->top += 5;
	lpncsp->rgrc->bottom -= 5;

	CEdit::OnNcCalcSize(bCalcValidRects, lpncsp);
}

void CSkinEdit::OnNcLButtonDown(UINT nHitTest, CPoint point)
{
	// TODO: �ڴ�������Ϣ������������/�����Ĭ��ֵ

	//POINT pt = {point.x,param.mousept.y};
	SetWindowText(_T(""));
	RECT rc;
	GetWindowRect(&rc);

	//rc.left = rc.right - 14;
	//rc.right = rc.left + 8;

	//if(PtInRect(&rc,point))
	//{
	//	if(m_rm)
	//		m_rm->Popup(m_hWnd);
	//}

	CEdit::OnNcLButtonDown(nHitTest, point);
}

void CSkinEdit::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: �ڴ�������Ϣ������������/�����Ĭ��ֵ

	//CRect	rect;
	//GetWindowRect(&rect);	
	//if (!rect.PtInRect(point))
	//{
	//	ReleaseCapture();
	//}
	CEdit::OnMouseMove(nFlags, point);
}

void CSkinEdit::OnSetFocus(CWnd* pOldWnd)
{
	CEdit::OnSetFocus(pOldWnd);
	CString str;
	GetWindowText(str);
	if(str == m_strText)
	{
		SetWindowText(_T(""));
		//SetTextColor(RGB(0,0,0));
	}
	// TODO: �ڴ˴�������Ϣ�����������
}

void CSkinEdit::OnKillFocus(CWnd* pNewWnd)
{
	CEdit::OnKillFocus(pNewWnd);
	CString str;
	GetWindowText(str);

	if(str.IsEmpty())
	{
		SetWindowText(m_strText);
	}
	// TODO: �ڴ˴�������Ϣ�����������
}

LRESULT CSkinEdit::OnNcHitTest(CPoint point)
{
	// TODO: �ڴ�������Ϣ������������/�����Ĭ��ֵ
	//LRESULT res = 0;
	//if(res != HTCLIENT)
	//	res = HTCAPTION;
	//return res;
	return CEdit::OnNcHitTest(point);
}

BOOL CSkinEdit::PreTranslateMessage(MSG* pMsg)
{
	// TODO: �ڴ�����ר�ô����/����û���
	if(pMsg->wParam == WM_LBUTTONDOWN && pMsg->hwnd != m_hWnd)
	{
		pMsg->wParam = HTCAPTION;
	}
	//if (pMsg->message == WM_LBUTTONDOWN && pMsg->hwnd == m_hWnd)
	//{
	//	pMsg->message = WM_NCLBUTTONDOWN;
	//	pMsg->wParam = HTCAPTION;
	//}
	return CEdit::PreTranslateMessage(pMsg);
}