// MyRuleStatic.cpp : implementation file
//

#include "stdafx.h"
#include "Smart_Black.h"
#include "MyRuleStatic.h"
#include "DlgCamBlack.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void WINAPI DrawCallBack(HDC hDC,void *context)
{
    CMyRuleStatic* pWnd = (CMyRuleStatic*)context;
	if (pWnd->IsWindowVisible())
    {
		pWnd->DrawRules(hDC);
    }
}
/////////////////////////////////////////////////////////////////////////////
// CMyRuleStatic

CMyRuleStatic::CMyRuleStatic()
{
	m_pen_object_area.CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
	m_brush_rule_region.CreateHatchBrush(HS_BDIAGONAL, RGB(0, 0, 255));
	m_hCursorCross = AfxGetApp()->LoadCursor(IDC_CURSOR_CROSS);
	m_hchann = -1;
	m_bSetArea = FALSE;
}

CMyRuleStatic::~CMyRuleStatic()
{
	m_brush_rule_region.DeleteObject();
	m_pen_object_area.DeleteObject();
}


BEGIN_MESSAGE_MAP(CMyRuleStatic, CStatic)
	//{{AFX_MSG_MAP(CMyRuleStatic)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_CREATE()
	ON_MESSAGE(WM_NETCLIENT,OnNetClientMsg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyRuleStatic message handlers

void CMyRuleStatic::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CRect rc;
	GetWindowRect(&rc);
	ClipCursor(&rc);
	m_objectarea = CRect(point, point);
    SetFocus();
	CStatic::OnLButtonDown(nFlags, point);
}

void CMyRuleStatic::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	//m_bSetArea = FALSE;
	ClipCursor(NULL);
	m_objectarea.right  = point.x;
    m_objectarea.bottom = point.y;
	if (m_objectarea.left != m_objectarea.right && m_bSetArea)
	{
		((CDlgCamBlack*)GetParent())->SetCurPosValue(m_objectarea);
	}
	m_bSetArea = FALSE;
	SetFocus();
	CStatic::OnLButtonUp(nFlags, point);
}

void CMyRuleStatic::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    if (GetKeyState(VK_LBUTTON) >= 0)
    {
		return;
    }
	m_objectarea.right  = point.x;
    m_objectarea.bottom = point.y;
	SetFocus();
	CStatic::OnMouseMove(nFlags, point);
}

int CMyRuleStatic::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CStatic::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	m_hCursorCross = AfxGetApp()->LoadCursor(IDC_CURSOR_CROSS);
	::SetCursor(m_hCursorCross);
	return 0;
}

void CMyRuleStatic::DrawRules(HDC hDC)
{
    CRect     rc;
    CDC*      pDC = CDC::FromHandle( hDC );
	HBRUSH  hOldBrush;
	CPen    *pOldPen;
	if (m_bSetArea)
	{
		pOldPen   = pDC->SelectObject(&m_pen_object_area);
	    hOldBrush = (HBRUSH)pDC->SelectObject(GetStockObject(NULL_BRUSH));
	    pDC->Rectangle(m_objectarea);
	    pDC->SelectObject(&pOldPen);
        pDC->SelectObject(hOldBrush);
	}
}
void CMyRuleStatic::OnNetClientMsg(WPARAM wParam,LPARAM lParam)
{
    if( wParam == LAUMSG_LINKMSG )
    {
        if( lParam == 0 )
        {
			VSNET_ClientDrawCallBack(m_hchann, DrawCallBack, this);
        }
    }
}

void CMyRuleStatic::ClearArea()
{
	m_objectarea.SetRectEmpty();
}

void CMyRuleStatic::DrawArea(CPoint begin)//,UINT nFlags)
{
	ScreenToClient(&begin);
	OnLButtonDown(0,begin);
	begin.Offset(30,30);
	OnMouseMove(0,begin);
	ClientToScreen(&begin);
	SetCursorPos(begin.x,begin.y);
}