// RemoteChannelDlg.cpp : implementation file
//

#include "stdafx.h"
#include "clientdemo5.h"
#include "RemoteChannelDlg.h"
#include "ClientDemo5Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace std;
/////////////////////////////////////////////////////////////////////////////
// CRemoteChannelDlg dialog
static CString dev_type_array[4] = 
{
	_CS("DVR"), _CS("NVR"), _CS("IPC"),  _CS("HVR")
};

#define ALL_PRO_TYPE   100




CRemoteChannelDlg::CRemoteChannelDlg(CWnd* pParent /*= NULL*/)
	: CDialog(CRemoteChannelDlg::IDD, pParent)
	, m_channel_name(_T(""))
	, m_channel_number(1)
	, m_ipaddr(_T(""))
	, m_port(0)
	, m_username(_T(""))
	, m_userpwd(_T(""))
	, m_hSearchHandle(NULL)
	, m_searchLen(0)
	, m_searchNum(0)
	, m_lostfocus(-1)
	, m_cur_confnum(0)
	, m_curporotocol(0)
{
	memset(&m_channel_configV4,0,sizeof(NET_SDK_NetDecorderConfig_V4));


}
CRemoteChannelDlg::~CRemoteChannelDlg()
{
}

void CRemoteChannelDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_SEARCHRESULT, m_listsearch_result);
	DDX_Text(pDX, IDC_EDIT_CHANNELNAME, m_channel_name);
	DDX_Text(pDX, IDC_EDIT_CHANNEL_NUMBER, m_channel_number);
	DDX_Text(pDX, IDC_IPADDRESS_DIGITAL, m_ipaddr);
	DDX_Text(pDX, IDC_EDIT_PORT_DIGITAL, m_port);
	DDX_Text(pDX, IDC_EDIT_USERNAME, m_username);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_userpwd);
	
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CRemoteChannelDlg, CDialog)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_BUTTON_SEARCH, &CRemoteChannelDlg::OnBnClickedButtonSearch)
	ON_WM_TIMER()
	ON_NOTIFY(NM_CLICK, IDC_LIST_SEARCHRESULT, &CRemoteChannelDlg::OnNMClickListSearchresult)
	ON_WM_DESTROY()
	ON_WM_CLOSE()
	ON_NOTIFY(NM_KILLFOCUS, IDC_LIST_SEARCHRESULT, &CRemoteChannelDlg::OnNMKillfocusListSearchresult)
	ON_NOTIFY(NM_SETFOCUS, IDC_LIST_SEARCHRESULT, &CRemoteChannelDlg::OnNMSetfocusListSearchresult)
	ON_CBN_SELCHANGE(IDC_COMBO_PROTOCOL, &CRemoteChannelDlg::OnCbnSelchangeComboProtocol)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_SEARCHRESULT, &CRemoteChannelDlg::OnLvnItemchangedListSearchresult)
	ON_BN_CLICKED(IDOK, &CRemoteChannelDlg::OnBnClickedOk)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRemoteChannelDlg message handlers

BOOL CRemoteChannelDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	_CWndCS(this);
	
	//AfxSetResourceHandle(OldResourceHanlde);//对话框已创建，设置回原来的模块
	
	SetWindowText(_CS(_T("DigitalChannel.RemoteChannel")));
	
	((CEdit*)GetDlgItem(IDC_EDIT_CHANNELNAME))->SetLimitText(64);
	((CEdit*)GetDlgItem(IDC_EDIT_USERNAME))->SetLimitText(64);
	((CEdit*)GetDlgItem(IDC_EDIT_PASSWORD))->SetLimitText(64);
	m_listsearch_result.SetExtendedStyle(m_listsearch_result.GetExtendedStyle() | LVS_EX_DOUBLEBUFFER | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	m_listsearch_result.InsertColumn(0, _CS("ConfigNet.No"), LVCFMT_LEFT, 40, 0);
	m_listsearch_result.InsertColumn(1, _CS("Desktop.DevName"), LVCFMT_LEFT, 130, 3);
	m_listsearch_result.InsertColumn(2, _CS("DigitalChannel.MAC"), LVCFMT_LEFT, 130, 3);
	m_listsearch_result.InsertColumn(3, _CS("Upgrade.IPAdress"), LVCFMT_LEFT, 130, 3);
	m_listsearch_result.InsertColumn(4, _CS("Desktop.Port"), LVCFMT_LEFT, 130, 3);
	CComboBox* pComBox = NULL;
	pComBox = (CComboBox *)GetDlgItem(IDC_COMBO_STREAMTYPE); 
	(CComboBox *)GetDlgItem(IDC_COMBO_PROTOCOL)->EnableWindow(true);
	if (pComBox == NULL)
	{
		return FALSE;
	}
	pComBox->ResetContent();
	int i = 0;
	int nInsert = 0;
	CString strText = _T("");
	for(i = 0; i < 2; ++i)
	{
		switch(i)
		{
		case 0:
			strText =  _CS("Desktop.MainStream");
			break;
		case 1:
			strText =  _CS("Desktop.ExtStream");
			break;
		default: break;
		}
		nInsert = pComBox->AddString(strText);
		pComBox->SetItemData(nInsert, i);
	}
	pComBox->SetCurSel(0);

	pComBox = (CComboBox *)GetDlgItem(IDC_COMBO_DEVTYPE); 
	if (pComBox == NULL)
	{
		return FALSE;
	}
	pComBox->ResetContent();
	nInsert = 0;
	for(i = 0; i < 3; ++i)
	{
		nInsert = pComBox->AddString(dev_type_array[i]);
		pComBox->SetItemData(nInsert, i);
	}
	pComBox->SetCurSel(2);


	pComBox = (CComboBox *)GetDlgItem(IDC_COMBO_PROTOCOL); 
	if (pComBox == NULL)
	{
		return FALSE;
	}
	pComBox->ResetContent();
	nInsert = 0;

	m_buse_old = true;

	
	
	NET_SDK_SEARCHPROTOCOLFUNC* pFunc = ((CClientDemo5Dlg*)AfxGetMainWnd())->GetSearchProtocol();
	m_strDvrIP = ((CClientDemo5Dlg*)AfxGetMainWnd())->m_strDvrIP;
	m_DevLoginID = ((CClientDemo5Dlg*)AfxGetMainWnd())->m_LoginID;
	for(i = 0; i < pFunc->nProNum; ++i)
	{
		std::string strProFunc = std::string(pFunc->vSearchProtocol[i]);
		std::string::size_type pos = strProFunc.find('_');
		if (pos == std::string::npos)
		{
			continue;
		}
		else
		{
			std::string strPro = strProFunc.substr(0, pos);
			std::string strProNo = strProFunc.substr(pos + 1, std::string::npos);
			int iProNo = atoi(strProNo.c_str());
			m_buse_old = false;
			strText.Format("%s", strPro.c_str());
			nInsert = pComBox->AddString(strText);
			pComBox->SetItemData(nInsert, iProNo);
		
		}
	}

		if (!m_buse_old)
		{
			if(nInsert > 0)
			{
				strText = _CS("ConfigDefault.AllDefault");
				nInsert = pComBox->AddString(strText);
				pComBox->SetItemData(nInsert, ALL_PRO_TYPE);
			}
			pComBox->SetCurSel(0);
			pComBox->EnableWindow(TRUE);
			m_curporotocol = pComBox->GetItemData(0);
		}
	

	if (m_buse_old)
	{
		for(i = 0; i < 2; ++i)
		{
			switch(i)
			{
			case  0:
				strText = _CS("Protocol.NETIP");
				break;
			case 1:
				strText = _CS("Protocol.ONVIF");
				break;
			default: break;
			}
			nInsert = pComBox->AddString(strText);
			pComBox->SetItemData(nInsert, i);
		}
		m_curporotocol = 1;
		pComBox->SetCurSel(1);
		pComBox->EnableWindow(TRUE);
	}

	CString no = _T("");
	CString dip = _T("");

	bool blogindev = false;
	for (int i = 0; i < m_searchNum; ++i)	//往结果列表添加
	{
		dip.Format("%d.%d.%d.%d", m_DeviceInfo[i].netcomcfg.HostIP.c[0], m_DeviceInfo[i].netcomcfg.HostIP.c[1], m_DeviceInfo[i].netcomcfg.HostIP.c[2], m_DeviceInfo[i].netcomcfg.HostIP.c[3]);
		no.Format( "%d" , i + 1);
		m_listsearch_result.InsertItem(i , no );
		m_listsearch_result.SetItemText(i, 1, m_DeviceInfo[i].netcomcfg.HostName);
		m_listsearch_result.SetItemText(i, 2, m_DeviceInfo[i].netcomcfg.sMac);
		m_listsearch_result.SetItemText(i, 3, dip);
		no.Format( "%d", m_DeviceInfo[i].netcomcfg.TCPPort);
		m_listsearch_result.SetItemText(i, 4, no);
	}

	return TRUE;
}

BOOL CRemoteChannelDlg::InitWndShow()
{
	
	if (m_cur_confnum > 0)
	{
		m_channel_name.Format("chConfig%02d", m_cur_confnum);
	}
	else
	{
		m_channel_name = m_channel_configV4.ConfName;

		if(m_channel_name.IsEmpty())
		{
			m_channel_name= "chConfig%01";
		}
}
	m_channel_number = m_channel_configV4.Channel + 1;

	m_ipaddr = m_channel_configV4.Address;

	m_port = m_channel_configV4.Port;
	m_username = m_channel_configV4.UserName;
	m_userpwd = m_channel_configV4.PassWord;
	if(m_username.IsEmpty() && m_port == 0)
	{
		((CComboBox *)GetDlgItem(IDC_COMBO_DEVTYPE))->SetCurSel(2);
	}
	else
	{
		((CComboBox *)GetDlgItem(IDC_COMBO_DEVTYPE))->SetCurSel(m_channel_configV4.DevType);
	}

	((CComboBox *)GetDlgItem(IDC_COMBO_STREAMTYPE))->SetCurSel(m_channel_configV4.StreamType);

	GetDlgItem(IDC_COMBO_DEVTYPE)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_CHANNEL_NUMBER)->EnableWindow(FALSE);

	

	if (!m_ipaddr.IsEmpty())
	{
		m_curporotocol = m_channel_configV4.Protocol;
	}
	else
	{
		m_curporotocol = ((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->GetItemData(0);
	}




	int cnt = ((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->GetCount();
	for(int i = 0; i < cnt; ++i)
	{
		int type = ((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->GetItemData(i);

		if (type == m_curporotocol)
		{
			((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->SetCurSel(i);
		}
	}
	

	UpdateData(FALSE);
	return TRUE;
}

void CRemoteChannelDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
	InitWndShow();
	return;
}




void CRemoteChannelDlg::SetConfig(const NET_SDK_NetDecorderConfig_V4& config)
{
	memset(&m_channel_configV4, 0, sizeof(NET_SDK_NetDecorderConfig_V4));
	memcpy(&m_channel_configV4, &config, sizeof(NET_SDK_NetDecorderConfig_V4));
}

bool CRemoteChannelDlg::GetOneCfg(NET_SDK_NetDecorderConfig_V4* netdcf)
{
	memset(netdcf, 0, sizeof(m_channel_configV4));
	memcpy(netdcf, &m_channel_configV4, sizeof(m_channel_configV4));
	return true;
}


void CRemoteChannelDlg::OnOK()
{

	int nIndex =((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->GetCurSel();
	CComboBox* pComBox = (CComboBox *)GetDlgItem(IDC_COMBO_PROTOCOL); 
	int protocol_type = pComBox->GetItemData(nIndex);
	if (protocol_type == ALL_PRO_TYPE)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->SetFocus();
		return;
	}
	if (m_ipaddr == "0.0.0.0")
	{
		GetDlgItem(IDC_IPADDRESS_DIGITAL)->SetFocus();
		return;
	}
	if(m_curporotocol!=17)
	{
		if (m_port <= 0)
		{
			GetDlgItem(IDC_EDIT_PORT_DIGITAL)->SetFocus();
			return;
		}
	}

	
	if (m_username.IsEmpty())
	{
		GetDlgItem(IDC_EDIT_USERNAME)->SetFocus();
		return;
	}
	memset(&m_channel_configV4, 0, sizeof(NET_SDK_NetDecorderConfig_V4));
	memcpy(m_channel_configV4.ConfName, m_channel_name.GetBuffer(0), m_channel_name.GetLength());

	memcpy(m_channel_configV4.Address, m_ipaddr.GetBuffer(0), m_ipaddr.GetLength());
	m_channel_configV4.Port = m_port;
	memcpy(m_channel_configV4.UserName, m_username.GetBuffer(0), m_username.GetLength());
	memcpy(m_channel_configV4.PassWord, m_userpwd.GetBuffer(0), m_userpwd.GetLength());

	m_channel_configV4.DevType = ((CComboBox *)GetDlgItem(IDC_COMBO_DEVTYPE))->GetCurSel();
	m_channel_configV4.Protocol = protocol_type;

	m_channel_configV4.Channel = m_channel_number - 1;
	m_channel_configV4.StreamType = ((CComboBox *)GetDlgItem(IDC_COMBO_STREAMTYPE))->GetCurSel();

	
  	UpdateData(FALSE);
	CDialog::OnOK();
}

void CRemoteChannelDlg::OnBnClickedButtonSearch()
{

	(CComboBox *)GetDlgItem(IDC_COMBO_PROTOCOL)->EnableWindow(FALSE);
	memset(m_DeviceInfo, 0, 64 * sizeof(NET_SDK_SEARCH_DEVINFO));
	memset(m_DevicePro, 0, 64 * sizeof(int));
	m_searchNum = 0;
	m_searchLen = 0;
	m_listsearch_result.DeleteAllItems();
	DWORD dwThreadID;
	if (m_hSearchHandle != NULL)
	{
		WaitForSingleObject(m_hSearchHandle, 2000);
		CloseHandle(m_hSearchHandle);
		m_hSearchHandle = NULL;
	}
	m_hSearchHandle = CreateThread( 0 , 0, (LPTHREAD_START_ROUTINE)SearchDevice, this, 0, &dwThreadID);
	if (m_hSearchHandle != NULL)
	{
		GetDlgItem(IDC_BUTTON_SEARCH)->EnableWindow(FALSE);
		GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(_CS("DigitalChannel.Searching"));
	}
	else
	{
		GetDlgItem(IDC_BUTTON_SEARCH)->EnableWindow(TRUE);
		GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(_CS("DigitalChannel.SearchFailed"));
	}
}

void CRemoteChannelDlg::NetIPSearchDev()
{
	bool bRet = NET_SDK_SearchDevice( (char *)&(m_DeviceInfo), sizeof(m_DeviceInfo), &(m_searchLen), 5000 );
	if (bRet)		//搜索成功
	{
		int nDevNum = m_searchLen / sizeof(NET_SDK_SEARCH_DEVINFO);	
		m_searchNum = nDevNum;
		CString dip = _T("");
		CString devip= m_strDvrIP;
		for (int i = 0; i < nDevNum; ++i)
		{
			dip.Format("%d.%d.%d.%d", m_DeviceInfo[i].netcomcfg.HostIP.c[0], m_DeviceInfo[i].netcomcfg.HostIP.c[1], m_DeviceInfo[i].netcomcfg.HostIP.c[2], m_DeviceInfo[i].netcomcfg.HostIP.c[3]);
			if (dip == devip)
			{
				--(m_searchNum);
				if (i == nDevNum - 1)
				{
					memset(&(m_DeviceInfo[i]), 0, sizeof(NET_SDK_SEARCH_DEVINFO));
				}
				else
				{
					for (int j = i; j < nDevNum - 1; ++j)
					{
						memcpy(&(m_DeviceInfo[j]), &(m_DeviceInfo[j + 1]), sizeof(NET_SDK_SEARCH_DEVINFO));
					}
					memset(&(m_DeviceInfo[nDevNum - 1]), 0, sizeof(NET_SDK_SEARCH_DEVINFO));
				}
				break;
			}
		}
		SetTimer(1, 0, NULL);
	}
	else			//搜索失败
	{
		SetTimer(2, 0, NULL);
	}
}

void CRemoteChannelDlg::OnvifSearchDev()
{
		NET_SDK_SearchByRemote_DeviceAll* pAllDevice = new NET_SDK_SearchByRemote_DeviceAll;
		if (pAllDevice == NULL)
		{
			SetTimer(2, 0, NULL);
			return;
		}

		memset(pAllDevice, 0, sizeof(NET_SDK_SearchByRemote_DeviceAll));

		
		int search_protype = m_curporotocol;
		if(m_curporotocol == ALL_PRO_TYPE)	// 协议数目 >=2 才有这种情况
		{
			search_protype = -1;
		}
		SearchProtocl scon = (SearchProtocl)search_protype;
		 NET_SDK_SearchDeviceByRemote(m_DevLoginID,scon,pAllDevice);
		
		
	
			m_searchNum = pAllDevice->nDevNumber;
			int dev_index = 0;
			CString curdevip = m_strDvrIP;
			CString searchdevip;

			for (int i = 0; i < m_searchNum; ++i)
			{
				searchdevip.Format("%d.%d.%d.%d", pAllDevice->vDeviceAll[i].DevIP.c[0], pAllDevice->vDeviceAll[i].DevIP.c[1], pAllDevice->vDeviceAll[i].DevIP.c[2], pAllDevice->vDeviceAll[i].DevIP.c[3]);
				if (searchdevip == curdevip)
				{
					--m_searchNum;
					continue;
				}
				m_DevicePro[dev_index] = pAllDevice->vDeviceAll[i].nProtocol;

				m_DeviceInfo[dev_index].netcomcfg.HostIP = pAllDevice->vDeviceAll[i].DevIP;
				m_DeviceInfo[dev_index].productinf.deviceType = pAllDevice->vDeviceAll[i].nDevType;
				if(pAllDevice->vDeviceAll[i].nProtocol == 3)
				{
					m_DeviceInfo[dev_index].productinf.deviceType = 2;
				}
				m_DeviceInfo[dev_index].netcomcfg.MonMode = 1;
				m_DeviceInfo[dev_index].netcomcfg.TCPPort = pAllDevice->vDeviceAll[i].nPort;
				memcpy(m_DeviceInfo[dev_index].netcomcfg.HostName, pAllDevice->vDeviceAll[i].sDevName, 32);
				memcpy(m_DeviceInfo[dev_index].productinf.username,  pAllDevice->vDeviceAll[i].sUserName, 32);
				memcpy(m_DeviceInfo[dev_index].productinf.password,  pAllDevice->vDeviceAll[i].sUserPwd, 32);
				memcpy(m_DeviceInfo[dev_index].netcomcfg.sMac, pAllDevice->vDeviceAll[i].sMacAddr, 32);
				++dev_index;
			}

			SetTimer(1, 0, NULL);
		

		delete pAllDevice;
	
	
}

unsigned int CRemoteChannelDlg::SearchDevice(LPVOID parm)
{
	CRemoteChannelDlg* pThis = static_cast<CRemoteChannelDlg*>(parm);
	if(pThis == NULL)
	{
		return 0;
	}

	pThis->OnvifSearchDev();

	return 0;
}

void CRemoteChannelDlg::OnTimer(UINT nIDEvent)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    switch(nIDEvent)
    {
    case  1:
        {
            KillTimer(1);
            if (m_hSearchHandle != NULL)
            {
                CloseHandle(m_hSearchHandle);
                m_hSearchHandle = NULL;
            }

            CString no = _T("");
            CString dip = _T("");
            CString devip = m_strDvrIP;
            bool blogindev = false;
            for (int i = 0; i < m_searchNum; ++i)	//往结果列表添加
            {
                dip.Format("%d.%d.%d.%d", m_DeviceInfo[i].netcomcfg.HostIP.c[0], m_DeviceInfo[i].netcomcfg.HostIP.c[1], m_DeviceInfo[i].netcomcfg.HostIP.c[2], m_DeviceInfo[i].netcomcfg.HostIP.c[3]);
                no.Format( "%d" , i + 1);
                m_listsearch_result.InsertItem(i , no );
                m_listsearch_result.SetItemText(i, 1, m_DeviceInfo[i].netcomcfg.HostName);
                m_listsearch_result.SetItemText(i, 2, m_DeviceInfo[i].netcomcfg.sMac);
                m_listsearch_result.SetItemText(i, 3, dip);
                no.Format( "%d", m_DeviceInfo[i].netcomcfg.TCPPort);
                m_listsearch_result.SetItemText(i, 4, no);
            }
           
            (CComboBox *)GetDlgItem(IDC_COMBO_PROTOCOL)->EnableWindow(TRUE);
            

            GetDlgItem(IDC_BUTTON_SEARCH)->EnableWindow(TRUE);
            GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(_CS("DigitalChannel.SearchFinish"));
        }
        break;
    case 2:
        {
          
            (CComboBox *)GetDlgItem(IDC_COMBO_PROTOCOL)->EnableWindow(TRUE);
            
            GetDlgItem(IDC_BUTTON_SEARCH)->EnableWindow(TRUE);
            KillTimer(2);
            if (m_hSearchHandle != NULL)
            {
                CloseHandle(m_hSearchHandle);
                m_hSearchHandle = NULL;
            }
            GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(_CS("DigitalChannel.SearchFailed"));
        }
        break;
    }

    CDialog::OnTimer(nIDEvent);
}

void CRemoteChannelDlg::OnNMClickListSearchresult(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	int nSelect = m_listsearch_result.GetSelectionMark();
	if( m_listsearch_result.GetItemCount() < 0 || nSelect < 0)
	{
		return;
	}

	((CComboBox*)GetDlgItem(IDC_COMBO_DEVTYPE))->SetCurSel(m_DeviceInfo[nSelect].productinf.deviceType);
	//((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->SetCurSel(m_DeviceInfo[nSelect].netcomcfg.MonMode);
	int prototype = m_DevicePro[nSelect];
	int cnt = ((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->GetCount();
	for(int i = 0; i < cnt; ++i)
	{
		int type = ((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->GetItemData(i);
		
		if (type == prototype)
		{
			((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->SetCurSel(i);
		}
	}
	m_ipaddr.Format("%d.%d.%d.%d", m_DeviceInfo[nSelect].netcomcfg.HostIP.c[0], m_DeviceInfo[nSelect].netcomcfg.HostIP.c[1], m_DeviceInfo[nSelect].netcomcfg.HostIP.c[2], m_DeviceInfo[nSelect].netcomcfg.HostIP.c[3]);
	m_port = m_DeviceInfo[nSelect].netcomcfg.TCPPort;
	m_username = m_DeviceInfo[nSelect].productinf.username;
	m_userpwd = m_DeviceInfo[nSelect].productinf.password;
	UpdateData(FALSE);
	*pResult = 0;
}

void CRemoteChannelDlg::OnDestroy()
{
	CDialog::OnDestroy();
}

void CRemoteChannelDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_hSearchHandle != NULL)
	{
		WaitForSingleObject(m_hSearchHandle, 5000);
	}
	CDialog::OnClose();
}


void CRemoteChannelDlg::OnNMKillfocusListSearchresult(NMHDR *pNMHDR, LRESULT *pResult)
{
	m_lostfocus = m_listsearch_result.GetSelectionMark();
	m_listsearch_result.SetItemState(m_lostfocus, LVIS_DROPHILITED, LVIS_DROPHILITED);

	*pResult = 0;
}

void CRemoteChannelDlg::OnNMSetfocusListSearchresult(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	m_listsearch_result.SetItemState(m_lostfocus, FALSE, LVIF_STATE);
	*pResult = 0;
}

void CRemoteChannelDlg::OnCbnSelchangeComboProtocol()
{
	int nIndex =((CComboBox*)GetDlgItem(IDC_COMBO_PROTOCOL))->GetCurSel();
	CComboBox* pComBox = (CComboBox *)GetDlgItem(IDC_COMBO_PROTOCOL); 
	int protocol_type = pComBox->GetItemData(nIndex);
	if (protocol_type != m_curporotocol)
	{
		m_curporotocol = protocol_type;
		if (protocol_type != 0)
		{
			m_channel_number = 1;
			((CComboBox*)GetDlgItem(IDC_COMBO_DEVTYPE))->SetCurSel(2);
			GetDlgItem(IDC_COMBO_DEVTYPE)->EnableWindow(FALSE);
			GetDlgItem(IDC_EDIT_CHANNEL_NUMBER)->EnableWindow(FALSE);
		}
		else
		{
			GetDlgItem(IDC_COMBO_DEVTYPE)->EnableWindow(FALSE);
			GetDlgItem(IDC_EDIT_CHANNEL_NUMBER)->EnableWindow(FALSE);
		}
	
	}
	
	UpdateData(FALSE);
}

void CRemoteChannelDlg::OnLvnItemchangedListSearchresult(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}

void CRemoteChannelDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	OnOK();
}

