// DlgCamBlack.cpp : implementation file
//

#include "stdafx.h"
#include "Smart_Black.h"
#include "DlgCamBlack.h"
#pragma warning(disable:4786)
#include <set>

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

/////////////////////////////////////////////////////////////////////////////
// CDlgCamBlack dialog


CDlgCamBlack::CDlgCamBlack(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgCamBlack::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgCamBlack)
	m_strarea1 = _T("烟囱1");
	m_strarea10 = _T("烟囱10");
	m_strarea = _T("烟囱");
	m_strarea2 = _T("烟囱2");
	m_strarea3 = _T("烟囱3");
	m_strarea4 = _T("烟囱4");
	m_strarea5 = _T("烟囱5");
	m_strarea6 = _T("烟囱6");
	m_strarea7 = _T("烟囱7");
	m_strarea8 = _T("烟囱8");
	m_strarea9 = _T("烟囱9");
	m_iareal1 = 0;
	m_iarear1 = 0;
	m_iareat1 = 0;
	m_iareab1 = 0;
	m_iareab2 = 0;
	m_iareal2 = 0;
	m_iarear2 = 0;
	m_iareat2 = 0;
	m_iareal3 = 0;
	m_iarear3 = 0;
	m_iareat3 = 0;
	m_iareab4 = 0;
	m_iareal4 = 0;
	m_iarear4 = 0;
	m_iareab3 = 0;
	m_iareat4 = 0;
	m_iareab5 = 0;
	m_iareal5 = 0;
	m_iarear5 = 0;
	m_iareat5 = 0;
	m_iareab6 = 0;
	m_iareal6 = 0;
	m_iarear6 = 0;
	m_iareat6 = 0;
	m_iareab7 = 0;
	m_iareal7 = 0;
	m_iarear7 = 0;
	m_iareat7 = 0;
	m_iareab8 = 0;
	m_iareal8 = 0;
	m_iarear8 = 0;
	m_iareat8 = 0;
	m_iareab9 = 0;
	m_iareal9 = 0;
	m_iarear9 = 0;
	m_iareat9 = 0;
	m_iarear = 0;
	m_iareat = 0;
	m_iareab = 0;
	m_iareal = 0;
	m_iareab10 = 0;
	m_iareal10 = 0;
	m_iarear10 = 0;
	m_iareat10 = 0;
	//}}AFX_DATA_INIT
	m_curArea = -1;
	m_ch = 0;
	m_sername = "";
	m_url = "";
	m_port = 0;
	m_username = "";
	m_password = "";
	m_hchann = -1;
}


void CDlgCamBlack::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgCamBlack)
	DDX_Text(pDX, IDC_EDITNAME1, m_strarea1);
	DDX_Text(pDX, IDC_EDITNAME10, m_strarea10);
	DDX_Text(pDX, IDC_EDITNAME11, m_strarea);
	DDX_Text(pDX, IDC_EDITNAME2, m_strarea2);
	DDX_Text(pDX, IDC_EDITNAME3, m_strarea3);
	DDX_Text(pDX, IDC_EDITNAME4, m_strarea4);
	DDX_Text(pDX, IDC_EDITNAME5, m_strarea5);
	DDX_Text(pDX, IDC_EDITNAME6, m_strarea6);
	DDX_Text(pDX, IDC_EDITNAME7, m_strarea7);
	DDX_Text(pDX, IDC_EDITNAME8, m_strarea8);
	DDX_Text(pDX, IDC_EDITNAME9, m_strarea9);
	DDX_Text(pDX, IDC_EDIT1L, m_iareal1);
	DDX_Text(pDX, IDC_EDIT1R, m_iarear1);
	DDX_Text(pDX, IDC_EDIT1T, m_iareat1);
	DDX_Text(pDX, IDC_EDIT1B, m_iareab1);
	DDX_Text(pDX, IDC_EDIT2B, m_iareab2);
	DDX_Text(pDX, IDC_EDIT2L, m_iareal2);
	DDX_Text(pDX, IDC_EDIT2R, m_iarear2);
	DDX_Text(pDX, IDC_EDIT2T, m_iareat2);
	DDX_Text(pDX, IDC_EDIT3L, m_iareal3);
	DDX_Text(pDX, IDC_EDIT3R, m_iarear3);
	DDX_Text(pDX, IDC_EDIT3T, m_iareat3);
	DDX_Text(pDX, IDC_EDIT4B, m_iareab4);
	DDX_Text(pDX, IDC_EDIT4L, m_iareal4);
	DDX_Text(pDX, IDC_EDIT4R, m_iarear4);
	DDX_Text(pDX, IDC_EDIT3B, m_iareab3);
	DDX_Text(pDX, IDC_EDIT4T, m_iareat4);
	DDX_Text(pDX, IDC_EDIT5B, m_iareab5);
	DDX_Text(pDX, IDC_EDIT5L, m_iareal5);
	DDX_Text(pDX, IDC_EDIT5R, m_iarear5);
	DDX_Text(pDX, IDC_EDIT5T, m_iareat5);
	DDX_Text(pDX, IDC_EDIT6B, m_iareab6);
	DDX_Text(pDX, IDC_EDIT6L, m_iareal6);
	DDX_Text(pDX, IDC_EDIT6R, m_iarear6);
	DDX_Text(pDX, IDC_EDIT6T, m_iareat6);
	DDX_Text(pDX, IDC_EDIT7B, m_iareab7);
	DDX_Text(pDX, IDC_EDIT7L, m_iareal7);
	DDX_Text(pDX, IDC_EDIT7R, m_iarear7);
	DDX_Text(pDX, IDC_EDIT7T, m_iareat7);
	DDX_Text(pDX, IDC_EDIT8B, m_iareab8);
	DDX_Text(pDX, IDC_EDIT8L, m_iareal8);
	DDX_Text(pDX, IDC_EDIT8R, m_iarear8);
	DDX_Text(pDX, IDC_EDIT8T, m_iareat8);
	DDX_Text(pDX, IDC_EDIT9B, m_iareab9);
	DDX_Text(pDX, IDC_EDIT9L, m_iareal9);
	DDX_Text(pDX, IDC_EDIT9R, m_iarear9);
	DDX_Text(pDX, IDC_EDIT9T, m_iareat9);
	DDX_Text(pDX, IDC_EDITR, m_iarear);
	DDX_Text(pDX, IDC_EDITT, m_iareat);
	DDX_Text(pDX, IDC_EDITB, m_iareab);
	DDX_Text(pDX, IDC_EDITL, m_iareal);
	DDX_Text(pDX, IDC_EDIT10B, m_iareab10);
	DDX_Text(pDX, IDC_EDIT10L, m_iareal10);
	DDX_Text(pDX, IDC_EDIT10R, m_iarear10);
	DDX_Text(pDX, IDC_EDIT10T, m_iareat10);
	DDX_Control(pDX, IDC_STATIC_VEDIO, m_videownd);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgCamBlack, CDialog)
	//{{AFX_MSG_MAP(CDlgCamBlack)
	ON_BN_CLICKED(IDC_BTNSET1, OnBtnset1)
	ON_BN_CLICKED(IDC_BTNSET2, OnBtnset2)
	ON_BN_CLICKED(IDC_BTNSET3, OnBtnset3)
	ON_BN_CLICKED(IDC_BTNSET4, OnBtnset4)
	ON_BN_CLICKED(IDC_BTNSET5, OnBtnset5)
	ON_BN_CLICKED(IDC_BTNSET6, OnBtnset6)
	ON_BN_CLICKED(IDC_BTNSET7, OnBtnset7)
	ON_BN_CLICKED(IDC_BTNSET8, OnBtnset8)
	ON_BN_CLICKED(IDC_BTNSET9, OnBtnset9)
	ON_BN_CLICKED(IDC_BTNSET10, OnBtnset10)
	ON_BN_CLICKED(IDC_BTNSET11, OnBtnset11)
	ON_BN_CLICKED(IDC_BTN_DEL1, OnBtnDel1)
	ON_BN_CLICKED(IDC_BTN_DEL2, OnBtnDel2)
	ON_BN_CLICKED(IDC_BTN_DEL3, OnBtnDel3)
	ON_BN_CLICKED(IDC_BTN_DEL4, OnBtnDel4)
	ON_BN_CLICKED(IDC_BTN_DEL5, OnBtnDel5)
	ON_BN_CLICKED(IDC_BTN_DEL6, OnBtnDel6)
	ON_BN_CLICKED(IDC_BTN_DEL7, OnBtnDel7)
	ON_BN_CLICKED(IDC_BTN_DEL8, OnBtnDel8)
	ON_BN_CLICKED(IDC_BTN_DEL9, OnBtnDel9)
	ON_BN_CLICKED(IDC_BTN_DEL10, OnBtnDel10)
	ON_BN_CLICKED(IDC_BTNSAVEALL, OnBtnsaveall)
	ON_BN_CLICKED(IDC_BTN_GET, OnBtnGet)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgCamBlack message handlers

BOOL CDlgCamBlack::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_videownd.SetWindowPos(NULL,20,0,352,288,SWP_NOMOVE);
	GetAllAreas();

	if (m_hchann <= 0)
	{
		CHANNEL_CLIENTINFO m_chinfo = {0};
		m_chinfo.m_buffnum    = 20;
		m_chinfo.m_ch         = 0;
		m_chinfo.m_hChMsgWnd  = m_videownd.m_hWnd;
		m_chinfo.m_hVideohWnd = m_videownd.m_hWnd;
		m_chinfo.m_nChmsgid   = WM_NETCLIENT;
		m_chinfo.m_password   = (char*)m_password.operator LPCTSTR();
		m_chinfo.m_playstart  = TRUE;
		m_chinfo.m_sername    = (char*)m_sername.operator LPCTSTR(); 
		m_chinfo.m_tranType   = 3;
		m_chinfo.m_useoverlay = FALSE;
		m_chinfo.m_username   = (char*)m_username.operator LPCTSTR();
		m_chinfo.nColorKey    = RGB(255,0,255);
		m_chinfo.context      = NULL;
		m_chinfo.m_messagecallback = NULL;
		m_hchann = VSNET_ClientStart((char*)m_url.operator LPCTSTR(),&m_chinfo,m_port, 0);
	}
	else
	{
		
	}
	m_videownd.m_hchann = m_hchann;
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//获取对应的10个区域的坐标值
void CDlgCamBlack::GetAllAreas()
{
	BOOL bRetState1 = FALSE;
	BOOL bRetState2 = FALSE;
    char filename[MAX_PATH];
	CString sername = _T("");
	sprintf(filename, "%s\\sysparam.ini", APPDIR);
	sername = m_sername;
	memset(m_areas.m_region, 0, sizeof(BLACK_LEVEL_AREAS));
	//获取服务器区域参数
    bRetState1 = GetParamFromServer(FALSE);
	for (int i = 0; i < 10; i ++)
	{
		CString temp = _T("");
		CString temp1 = _T("");
		char value[MAX_PATH];
		memset(value, 0, MAX_PATH);
		temp.Format("area%d", i + 1);
		temp1.Format("烟囱%d",i + 1);
		switch (i)
		{
		case 0:
			m_iareal1 = m_areas.m_region[i].m_region_left;
			m_iarear1 = m_areas.m_region[i].m_region_right;
			m_iareat1 = m_areas.m_region[i].m_region_top;
			m_iareab1 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea1.Format("%s", value);
			break;
		case 1:
			m_iareal2 = m_areas.m_region[i].m_region_left;
			m_iarear2 = m_areas.m_region[i].m_region_right;
			m_iareat2 = m_areas.m_region[i].m_region_top;
			m_iareab2 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea2.Format("%s", value);
			break;
		case 2:
			m_iareal3 = m_areas.m_region[i].m_region_left;
			m_iarear3 = m_areas.m_region[i].m_region_right;
			m_iareat3 = m_areas.m_region[i].m_region_top;
			m_iareab3 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea3.Format("%s", value);
			break;
		case 3:
			m_iareal4 = m_areas.m_region[i].m_region_left;
			m_iarear4 = m_areas.m_region[i].m_region_right;
			m_iareat4 = m_areas.m_region[i].m_region_top;
			m_iareab4 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea4.Format("%s", value);
			break;
		case 4:
			m_iareal5 = m_areas.m_region[i].m_region_left;
			m_iarear5 = m_areas.m_region[i].m_region_right;
			m_iareat5 = m_areas.m_region[i].m_region_top;
			m_iareab5 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea5.Format("%s", value);
			break;
		case 5:
			m_iareal6 = m_areas.m_region[i].m_region_left;
			m_iarear6 = m_areas.m_region[i].m_region_right;
			m_iareat6 = m_areas.m_region[i].m_region_top;
			m_iareab6 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea6.Format("%s", value);
			break;
		case 6:
			m_iareal7 = m_areas.m_region[i].m_region_left;
			m_iarear7 = m_areas.m_region[i].m_region_right;
			m_iareat7 = m_areas.m_region[i].m_region_top;
			m_iareab7 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea7.Format("%s", value);
			break;
		case 7:
			m_iareal8 = m_areas.m_region[i].m_region_left;
			m_iarear8 = m_areas.m_region[i].m_region_right;
			m_iareat8 = m_areas.m_region[i].m_region_top;
			m_iareab8 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea8.Format("%s", value);
			break;
		case 8:
			m_iareal9 = m_areas.m_region[i].m_region_left;
			m_iarear9 = m_areas.m_region[i].m_region_right;
			m_iareat9 = m_areas.m_region[i].m_region_top;
			m_iareab9 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea9.Format("%s", value);
			break;
		case 9:
			m_iareal10 = m_areas.m_region[i].m_region_left;
			m_iarear10 = m_areas.m_region[i].m_region_right;
			m_iareat10 = m_areas.m_region[i].m_region_top;
			m_iareab10 = m_areas.m_region[i].m_region_bottom;
			GetPrivateProfileString(sername, temp, temp1, value, MAX_PATH, filename);
			m_strarea10.Format("%s", value);
			break;
		}
	}
	memset(&m_area, 0, sizeof(BLACK_LEVEL_AREA));
	//获取参考区域
	bRetState2 = GetParamFromServer(TRUE);
	m_iareal = m_area.m_region_left;
	m_iarear = m_area.m_region_right;
	m_iareat = m_area.m_region_top;
	m_iareab = m_area.m_region_bottom;
	char stremp[MAX_PATH];
	memset(stremp, 0, MAX_PATH);
    GetPrivateProfileString(sername, _T("area"), _T("烟囱"), stremp, MAX_PATH, filename);
	m_strarea.Format("%s", stremp);
	CString temp = _T("");
	if (bRetState1 && bRetState2)
	{
		temp.LoadString(IDS_STRING158);
		GetDlgItem(IDC_STATIC_STATE)->SetWindowText(temp);
	}
	else
	{
		temp.LoadString(IDS_STRING160);
		GetDlgItem(IDC_STATIC_STATE)->SetWindowText(temp);
	}
	UpdateData(FALSE);
}

void CDlgCamBlack::OnBtnset1() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 1;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset2() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 2;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset3() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 3;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset4() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 4;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset5() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 5;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset6() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 6;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset7() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 7;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset8() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 8;
    PreDrawRect();
}

void CDlgCamBlack::OnBtnset9() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 9;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset10() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 10;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnset11() 
{
	// TODO: Add your control notification handler code here
	m_curArea = 11;
	PreDrawRect();
}

void CDlgCamBlack::OnBtnDel1() 
{
	// TODO: Add your control notification handler code here
	m_iareal1 = 0;
	m_iarear1 = 0;
	m_iareat1 = 0;
	m_iareab1 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel2() 
{
	// TODO: Add your control notification handler code here
	m_iareal2 = 0;
	m_iarear2 = 0;
	m_iareat2 = 0;
	m_iareab2 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel3() 
{
	// TODO: Add your control notification handler code here
	m_iareal3 = 0;
	m_iarear3 = 0;
	m_iareat3 = 0;
	m_iareab3 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
	m_videownd.ClearArea();
}

void CDlgCamBlack::OnBtnDel4() 
{
	// TODO: Add your control notification handler code here
	m_iareal4 = 0;
	m_iarear4 = 0;
	m_iareat4 = 0;
	m_iareab4 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel5() 
{
	// TODO: Add your control notification handler code here
	m_iareal5 = 0;
	m_iarear5 = 0;
	m_iareat5 = 0;
	m_iareab5 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel6() 
{
	// TODO: Add your control notification handler code here
	m_iareal6 = 0;
	m_iarear6 = 0;
	m_iareat6 = 0;
	m_iareab6 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel7() 
{
	// TODO: Add your control notification handler code here
	m_iareal7= 0;
	m_iarear7 = 0;
	m_iareat7 = 0;
	m_iareab7 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel8() 
{
	// TODO: Add your control notification handler code here
	m_iareal8 = 0;
	m_iarear8 = 0;
	m_iareat8 = 0;
	m_iareab8 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel9() 
{
	// TODO: Add your control notification handler code here
	m_iareal9 = 0;
	m_iarear9 = 0;
	m_iareat9 = 0;
	m_iareab9 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnDel10() 
{
	// TODO: Add your control notification handler code here
	m_iareal10 = 0;
	m_iarear10 = 0;
	m_iareat10 = 0;
	m_iareab10 = 0;
	UpdateData(FALSE);
	m_videownd.ClearArea();
	OnBtnsaveall();
	GetAllAreas();
}

void CDlgCamBlack::OnBtnsaveall() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	std::set<int> iAreaSet;
	iAreaSet.clear();
	BOOL bRetState1 = FALSE;
	BOOL bRetState2 = FALSE;
	char filename[MAX_PATH];
	CString sername = _T("");
	sprintf(filename, "%s\\sysparam.ini", APPDIR);
	sername = m_sername;
	char value[4];
	memset(value, 0, 4);
	for (int i = 0; i < 10; i ++)
	{
		CString temp = _T("");
		temp.Format("area%d", i + 1);
		switch (i)
		{
		case 0:
			m_area.m_region_left = m_iareal1;
			m_area.m_region_right = m_iarear1;
			m_area.m_region_top = m_iareat1;
			m_area.m_region_bottom = m_iareab1;
			if (!m_iareal1 && !m_iarear1 && !m_iareat1 && !m_iareab1)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(0);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(0);
			}
			memcpy(&m_areas.m_region[0], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea1, filename);
			break;
		case 1:
			m_area.m_region_left = m_iareal2;
			m_area.m_region_right = m_iarear2;
			m_area.m_region_top = m_iareat2;
			m_area.m_region_bottom = m_iareab2;
			if (!m_iareal2 && !m_iarear2 && !m_iareat2 && !m_iareab2)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(1);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.erase(1);
			}
			memcpy(&m_areas.m_region[1], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea2, filename);
			break;
		case 2:
			m_area.m_region_left = m_iareal3;
			m_area.m_region_right = m_iarear3;
			m_area.m_region_top = m_iareat3;
			m_area.m_region_bottom = m_iareab3;
			if (!m_iareal3 && !m_iarear3 && !m_iareat3 && !m_iareab3)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(2);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(2);
			}
			memcpy(&m_areas.m_region[2], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea3, filename);
			break;
		case 3:
			m_area.m_region_left = m_iareal4;
			m_area.m_region_right = m_iarear4;
			m_area.m_region_top = m_iareat4;
			m_area.m_region_bottom = m_iareab4;
			if (!m_iareal4 && !m_iarear4 && !m_iareat4 && !m_iareab4)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(3);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(3);
			}
			memcpy(&m_areas.m_region[3], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea4, filename);
			break;
		case 4:
			m_area.m_region_left = m_iareal5;
			m_area.m_region_right = m_iarear5;
			m_area.m_region_top = m_iareat5;
			m_area.m_region_bottom = m_iareab5;
			if (!m_iareal5 && !m_iarear5 && !m_iareat5 && !m_iareab5)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(4);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(4);
			}
			memcpy(&m_areas.m_region[4], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea5, filename);
			break;
		case 5:
			m_area.m_region_left = m_iareal6;
			m_area.m_region_right = m_iarear6;
			m_area.m_region_top = m_iareat6;
			m_area.m_region_bottom = m_iareab6;
			if (!m_iareal6 && !m_iarear6 && !m_iareat6 && !m_iareab6)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(5);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(5);
			}
			memcpy(&m_areas.m_region[5], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea6, filename);
			break;
		case 6:
			m_area.m_region_left = m_iareal7;
			m_area.m_region_right = m_iarear7;
			m_area.m_region_top = m_iareat7;
			m_area.m_region_bottom = m_iareab7;
			if (!m_iareal7 && !m_iarear7 && !m_iareat7 && !m_iareab7)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(6);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(6);
			}
			memcpy(&m_areas.m_region[6], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea7, filename);
			break;
		case 7:
			m_area.m_region_left = m_iareal8;
			m_area.m_region_right = m_iarear8;
			m_area.m_region_top = m_iareat8;
			m_area.m_region_bottom = m_iareab8;
			if (!m_iareal8 && !m_iarear8 && !m_iareat8 && !m_iareab8)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(7);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(7);
			}
			memcpy(&m_areas.m_region[7], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea8, filename);
			break;
		case 8:
			m_area.m_region_left = m_iareal9;
			m_area.m_region_right = m_iarear9;
			m_area.m_region_top = m_iareat9;
			m_area.m_region_bottom = m_iareab9;
			if (!m_iareal9 && !m_iarear9 && !m_iareat9 && !m_iareab9)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(8);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(8);
			}
			memcpy(&m_areas.m_region[8], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea9, filename);
			break;
		case 9:
			m_area.m_region_left = m_iareal10;
			m_area.m_region_right = m_iarear10;
			m_area.m_region_top = m_iareat10;
			m_area.m_region_bottom = m_iareab10;
			if (!m_iareal10 && !m_iarear10 && !m_iareat10 && !m_iareab10)
			{
				m_area.m_region_flg = FALSE;
				iAreaSet.erase(9);
			}
			else
			{
				m_area.m_region_flg = TRUE;
				iAreaSet.insert(9);
			}
			memcpy(&m_areas.m_region[9], &m_area, sizeof(BLACK_LEVEL_AREA));
			WritePrivateProfileString(sername, temp, m_strarea10, filename);
			break;
		}
	}
	CString temp = _T("");

	//保存区域到服务器
	memset(&m_area, 0 ,sizeof(BLACK_LEVEL_AREA));
	m_area.m_region_left = m_iareal;
	m_area.m_region_right = m_iarear;
	m_area.m_region_top = m_iareat;
	m_area.m_region_bottom = m_iareab;
	m_area.m_region_flg = TRUE;
	if ((!m_iareal && !m_iarear && !m_iareat && !m_iareab))
	{
		temp.LoadString(IDS_STRING161);
		GetDlgItem(IDC_STATIC_STATE)->SetWindowText(temp);
		AfxMessageBox(IDS_STRING184);
		return;
	}
	bRetState1 = SaveParamToServer(FALSE);
	WritePrivateProfileString(sername, _T("area"), m_strarea, filename);
	//保存参考区域到服务器
	bRetState2 = SaveParamToServer(TRUE);
	if (bRetState1 && bRetState2)
	{	
		temp.LoadString(IDS_STRING159);
		GetDlgItem(IDC_STATIC_STATE)->SetWindowText(temp);
	}
	else
	{
		temp.LoadString(IDS_STRING161);
		GetDlgItem(IDC_STATIC_STATE)->SetWindowText(temp);
	}
}

void CDlgCamBlack::OnBtnGet() 
{
	// TODO: Add your control notification handler code here
	
}

void CDlgCamBlack::PreDrawRect()
{
	m_videownd.m_bSetArea = TRUE;
	m_videownd.ClearArea();
	CRect rc(0,0,0,0);
	m_videownd.GetWindowRect(&rc);
	m_videownd.DrawArea(rc.CenterPoint());
	GetCurPosValue();
}

void CDlgCamBlack::GetCurPosValue()
{
   CRect rect;
   char lpstr[4];
   memset(lpstr, 0, 4);
   switch (m_curArea)
   {
   case 1:
       GetDlgItem(IDC_EDIT1L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT1R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT1T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT1B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 2:
	   GetDlgItem(IDC_EDIT2L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT2R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT2T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT2B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 3:
	   GetDlgItem(IDC_EDIT3L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT3R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT3T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT3B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 4:
	   GetDlgItem(IDC_EDIT4L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT4R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT4T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT4B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 5:
	   GetDlgItem(IDC_EDIT5L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT5R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT5T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT5B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 6:
	   GetDlgItem(IDC_EDIT6L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT6R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT6T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT6B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 7:
	   GetDlgItem(IDC_EDIT7L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT7R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT7T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT7B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 8:
	   GetDlgItem(IDC_EDIT8L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT8R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT8T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT8B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 9:
	   GetDlgItem(IDC_EDIT9L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT9R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT9T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT9B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 10:
	   GetDlgItem(IDC_EDIT10L)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDIT10R)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDIT10T)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDIT10B)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   case 11:
	   GetDlgItem(IDC_EDITL)->GetWindowText(lpstr, 4);
	   rect.left = atoi(lpstr);
	   GetDlgItem(IDC_EDITR)->GetWindowText(lpstr, 4);
	   rect.right = atoi(lpstr);
	   GetDlgItem(IDC_EDITT)->GetWindowText(lpstr, 4);
	   rect.top = atoi(lpstr);
	   GetDlgItem(IDC_EDITB)->GetWindowText(lpstr, 4);
	   rect.bottom = atoi(lpstr);
   	   break;
   default:
	   break;
   }
   m_videownd.m_objectarea = rect;
}

BOOL CDlgCamBlack::SaveParamToServer(BOOL bSet)
{
	int state = -1;
	BOOL  iRet = FALSE;
    LONG  hParam;
	m_hMutexSet.Lock();
	hParam = VSNET_ClientMessageOpen((char*)m_sername.operator LPCTSTR(),  (char*)m_url.operator LPCTSTR(), 
		(char*)m_username.operator LPCTSTR(), (char*)m_password.operator LPCTSTR(), m_port);
	try
	{
		if (!hParam)
		{
			m_hMutexSet.Unlock();
			return FALSE;
		}
		if(!bSet)
		{
			state = VSNET_ClientMessageOpt(hParam, MESSAGE_CMD_SETBLACKREGIONTARGET, m_ch, &m_areas);
		}
		else
		{
			state = VSNET_ClientMessageOpt(hParam, MESSAGE_CMD_SETBLACKREGIONREFER, m_ch, &m_area);
		}
		if (state <= 0)
		{
			m_hMutexSet.Unlock();
			return FALSE;
		}
		VSNET_ClientMessageClose(hParam);
	}
	catch (...) {}
	m_hMutexSet.Unlock();
	return TRUE;
}

BOOL CDlgCamBlack::GetParamFromServer(BOOL bSet)
{   
	int state = -1;
	BOOL  iRet = FALSE;
    LONG  hParam;
	m_hMutexSet.Lock();
	hParam = VSNET_ClientMessageOpen((char*)m_sername.operator LPCTSTR(),  (char*)m_url.operator LPCTSTR(), 
		(char*)m_username.operator LPCTSTR(), (char*)m_password.operator LPCTSTR(), m_port);
	try
	{
		if (!hParam)
		{
			m_hMutexSet.Unlock();
			return FALSE;
		}
		if(!bSet)
		{
			state = VSNET_ClientMessageOpt(hParam, MESSAGE_CMD_GETBLACKREGIONTARGET, m_ch, &m_areas);
		}
		else
		{
			state = VSNET_ClientMessageOpt(hParam, MESSAGE_CMD_GETBLACKREGIONREFER, m_ch, &m_area);
		}
		if (state <= 0)
		{
			m_hMutexSet.Unlock();
			return FALSE;
		}
		VSNET_ClientMessageClose(hParam);
	}
	catch (...) {}
	m_hMutexSet.Unlock();
	return TRUE;
}

void CDlgCamBlack::GetIllegalRect(CRect &rect)
{
	int temp = rect.left;
	if (temp > rect.right)
	{
		rect.left = rect.right;
		rect.right = temp;
	}
	temp = rect.top;
	if (temp > rect.bottom)
	{
		rect.top = rect.bottom;
		rect.bottom = temp;
	}
}

void CDlgCamBlack::SetCurPosValue(CRect& rect)
{
   CString lpstr = _T("");
   GetIllegalRect(rect);
   switch (m_curArea)
   {
   case 1:
	   lpstr.Format("%d", rect.left);
	   GetDlgItem(IDC_EDIT1L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT1R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT1T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT1B)->SetWindowText(lpstr);
	   break;
   case 2:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT2L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT2R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT2T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT2B)->SetWindowText(lpstr);
   	   break;
   case 3:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT3L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT3R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT3T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT3B)->SetWindowText(lpstr);
   	   break;
   case 4:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT4L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT4R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT4T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT4B)->SetWindowText(lpstr);
   	   break;
   case 5:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT5L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT5R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT5T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT5B)->SetWindowText(lpstr);
   	   break;
   case 6:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT6L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT6R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT6T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT6B)->SetWindowText(lpstr);
   	   break;
   case 7:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT7L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT7R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT7T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT7B)->SetWindowText(lpstr);
   	   break;
   case 8:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT8L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT8R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT8T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT8B)->SetWindowText(lpstr);
   	   break;
   case 9:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT9L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT9R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT9T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT9B)->SetWindowText(lpstr);
   	   break;
   case 10:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDIT10L)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDIT10R)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDIT10T)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDIT10B)->SetWindowText(lpstr);
   	   break;
   case 11:
	   lpstr.Format("%d", rect.left);
       GetDlgItem(IDC_EDITL)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.right);
	   GetDlgItem(IDC_EDITR)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.top);
	   GetDlgItem(IDC_EDITT)->SetWindowText(lpstr);
	   lpstr.Format("%d", rect.bottom);
	   GetDlgItem(IDC_EDITB)->SetWindowText(lpstr);
   	   break;
   default:
	   break;
   }
}

void CDlgCamBlack::OnDestroy() 
{
	if (m_hchann > 0)
	{
		VSNET_ClientStop(m_hchann);
		m_hchann = -1;
	}
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	
}
