// LiveSampleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LiveSample.h"
#include "LiveSampleDlg.h"

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

CPTZButton::CPTZButton()
{

}

CPTZButton::~CPTZButton()
{
}


BEGIN_MESSAGE_MAP(CPTZButton, CButton)
	//{{AFX_MSG_MAP(CPTZButton)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CPTZButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
    int ctrlId = GetDlgCtrlID();

    CLiveSampleDlg* pPanelDlg = (CLiveSampleDlg *)GetParent();

    if (pPanelDlg != NULL)
    {
        pPanelDlg->ButtonDown(ctrlId);
    }

    CButton::OnLButtonDown(nFlags, point);
}

void CPTZButton::OnLButtonUp(UINT nFlags, CPoint point) 
{	
    int ctrlId = GetDlgCtrlID();
    
    CLiveSampleDlg* pPanelDlg = (CLiveSampleDlg *)GetParent();

    if (pPanelDlg != NULL)
    {
        pPanelDlg->ButtonUp(ctrlId);
    }

    CButton::OnLButtonUp(nFlags, point);
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLiveSampleDlg dialog

CLiveSampleDlg::CLiveSampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLiveSampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLiveSampleDlg)
	m_Message = _T("");
	m_CameraView = 0;
	m_IPAddress = _T("localhost:5150");
	m_PresetSet = _T("");
	m_DIIndex = 0;
	m_DOIndex = 0;
	m_Password = _T("");
	m_UserName = _T("admin");
	m_bCameraName = FALSE;
    m_bCameraBitrate = FALSE; 
	m_bDate = FALSE;
	m_bTime = FALSE;
	m_bBold = FALSE;
	m_bHasEdge = FALSE;
	m_FontSize = 9;
	m_Transparency = 0;
	m_BackB = 0;
	m_BackG = 0;
	m_BackR = 0;
	m_FontB = 0;
	m_FontG = 0;
	m_FontR = 0;
	m_ActiveView = 0;
	m_SnapPath = _T("");
	m_Height = 0;
	m_Width = 0;
	m_FontName = _T("Tahoma");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	iframeCount = 0;
}

CLiveSampleDlg::~CLiveSampleDlg()
{

}


void CLiveSampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLiveSampleDlg)
	DDX_Control(pDX, IDC_COMBO_PRESET, m_PresetName);
	DDX_Control(pDX, IDC_COMBO_CAMERA, m_CameraList);
	DDX_Control(pDX, IDC_LIVEACTIVEXCTRL1, m_LiveActievX);
	DDX_Text(pDX, IDC_STATIC1, m_Message);
	DDX_Text(pDX, IDC_EDIT_CAMERAVIEW, m_CameraView);
	DDV_MinMaxInt(pDX, m_CameraView, 0, 15);
	DDX_Control(pDX, IDC_DOWN, m_Down);
	DDX_Control(pDX, IDC_UP, m_Up);
	DDX_Control(pDX, IDC_RIGHTUP, m_RightUp);
	DDX_Control(pDX, IDC_RIGHTDOWN, m_RightDown);
	DDX_Control(pDX, IDC_RIGHT, m_Right);
	DDX_Control(pDX, IDC_LEFTUP, m_LeftUp);
	DDX_Control(pDX, IDC_LEFTDOWN, m_LeftDown);
	DDX_Control(pDX, IDC_LEFT, m_Left);
	DDX_Control(pDX, IDC_BUTTON_ZOOMOUT, m_ZoomOut);
	DDX_Control(pDX, IDC_BUTTON_ZOOMIN, m_ZoomIN);
	DDX_Text(pDX, IDC_EDIT_IPADDRESS, m_IPAddress);
	DDX_Text(pDX, IDC_PRESETNAME, m_PresetSet);
	DDX_Text(pDX, IDC_EDIT_DIINDEX, m_DIIndex);
	DDX_Text(pDX, IDC_EDIT_DOINDEX, m_DOIndex);
	DDX_Text(pDX, IDC_EDIT_PASS, m_Password);
	DDX_Text(pDX, IDC_EDIT_USER, m_UserName);
	DDX_Check(pDX, IDC_CHECK_CAMERANAME, m_bCameraName);
	DDX_Check(pDX, IDC_CHECK_CAMERA_BITRATE, m_bCameraBitrate);
	DDX_Check(pDX, IDC_CHECK_DATE, m_bDate);
	DDX_Check(pDX, IDC_CHECK_TIME, m_bTime);
	DDX_Check(pDX, IDC_CHECK_Bold, m_bBold);
	DDX_Check(pDX, IDC_CHECK_EDGE, m_bHasEdge);
	DDX_Text(pDX, IDC_EDIT_FONTSIZE, m_FontSize);
	DDX_Text(pDX, IDC_EDIT_TRANSPARENCY, m_Transparency);
	DDX_Text(pDX, IDC_EDIT_BACKB, m_BackB);
	DDX_Text(pDX, IDC_EDIT_BACKG, m_BackG);
	DDX_Text(pDX, IDC_EDIT_BACKR, m_BackR);
	DDX_Text(pDX, IDC_EDIT_FONTB, m_FontB);
	DDX_Text(pDX, IDC_EDIT_FONTG, m_FontG);
	DDX_Text(pDX, IDC_EDIT_FONTR, m_FontR);
	DDX_Text(pDX, IDC_EDIT_ACTIVEVIEW, m_ActiveView);
	DDX_Text(pDX, IDC_EDIT_SNAPSHOT, m_SnapPath);
	DDX_Text(pDX, IDC_EDIT_HEIGHT, m_Height);
	DDX_Text(pDX, IDC_EDIT_WIDTH, m_Width);
	DDX_Text(pDX, IDC_EDIT_FONTNAME, m_FontName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLiveSampleDlg, CDialog)
	//{{AFX_MSG_MAP(CLiveSampleDlg)
	ON_WM_SYSCOMMAND()
    ON_WM_KEYUP()
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_CAMERALIST, OnButtonCameralist)
	ON_BN_CLICKED(IDC_BUTTON_CONNECT_CAMERA, OnConnectCamera)
	ON_BN_CLICKED(IDC_BUTTON_DICOUNT, OnButtonDicount)
	ON_BN_CLICKED(IDC_BUTTON_DINAME, OnButtonDiname)
	ON_BN_CLICKED(IDC_BUTTON_DISTATUS, OnButtonDistatus)
	ON_BN_CLICKED(IDC_BUTTON_1X1, OnButton1x1)
	ON_BN_CLICKED(IDC_BUTTON_2X2, OnButton2x2)
	ON_BN_CLICKED(IDC_BUTTON_3X3, OnButton3x3)
	ON_BN_CLICKED(IDC_BUTTON_4X4, OnButton4x4)
	ON_BN_CLICKED(IDC_BUTTON_SNAPSHOT, OnButtonSnapshot)
	ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
	ON_BN_CLICKED(IDC_BUTTON_SETACTIVE, OnButtonSetactive)
	ON_BN_CLICKED(IDC_BUTTON_GETACTIVE, OnButtonGetactive)
    ON_BN_CLICKED(IDC_BUTTON_ENABLE_AUDIO, OnButtonEnableAudioAndSetActive)
    ON_BN_CLICKED(IDC_BUTTON_DISABLE_AUDIO, OnButtonDisableAudioAndSetActive)
	ON_BN_CLICKED(IDC_BUTTON_RAWIMAGE, OnButtonRawimage)
	ON_BN_CLICKED(IDC_BUTTON_SETOSD, OnButtonSetosd)
	ON_BN_CLICKED(IDC_BUTTON_DOCOUNT, OnButtonDocount)
	ON_BN_CLICKED(IDC_BUTTON_DONAME, OnButtonDoname)
	ON_BN_CLICKED(IDC_BUTTON_DOSTATUS, OnButtonDostatus)
	ON_BN_CLICKED(IDC_BUTTON_SETDO, OnButtonSetdo)
	ON_BN_CLICKED(IDC_BUTTON_FULLSCREEN, OnButtonFullscreen)
	ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_DROP, OnButtonDrop)
	ON_BN_CLICKED(IDC_GETPRESET, OnGetpreset)
	ON_BN_CLICKED(IDC_SETPRESET, OnSetpreset)
	ON_BN_CLICKED(IDC_DELETEPRESET, OnDeletepreset)
    ON_CBN_SELCHANGE(IDC_COMBO_PRESET, OnSelchangeComboPreset)
	ON_BN_CLICKED(IDC_BUTTON_RAWIMAGE2, OnButtonRawimage2)
	ON_BN_CLICKED(IDC_BUTTON_FOCUSNEAR, OnButtonFocusnear)
	ON_BN_CLICKED(IDC_BUTTON_FOCUSFAR, OnButtonFocusfar)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_HOME, OnHome)
	ON_BN_CLICKED(IDC_BUTTON_GETINDEXFROMVIEW, OnButtonGetCamerIndexFromView)
	ON_BN_CLICKED(IDC_BUTTON_DUPLICATE_CAMERA, OnDuplicateCamera)
	ON_BN_CLICKED(IDC_BUTTON_REMOVE_DUPLICATE_CAMERA, OnRemoveDuplicateCamera)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLiveSampleDlg message handlers

BOOL CLiveSampleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	GetDlgItem(IDC_UP)->EnableWindow(FALSE);
	GetDlgItem(IDC_DOWN)->EnableWindow(FALSE);
	GetDlgItem(IDC_RIGHT)->EnableWindow(FALSE);
	GetDlgItem(IDC_LEFT)->EnableWindow(FALSE);
	GetDlgItem(IDC_RIGHTUP)->EnableWindow(FALSE);
	GetDlgItem(IDC_RIGHTDOWN)->EnableWindow(FALSE);
	GetDlgItem(IDC_LEFTUP)->EnableWindow(FALSE);
	GetDlgItem(IDC_LEFTDOWN)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_ZOOMIN)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_ZOOMOUT)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_FOCUSFAR)->EnableWindow(FALSE);
	GetDlgItem(IDC_BUTTON_FOCUSNEAR)->EnableWindow(FALSE);
	GetDlgItem(IDC_HOME)->EnableWindow(FALSE);
	GetDlgItem(IDC_GETPRESET)->EnableWindow(FALSE);
	GetDlgItem(IDC_COMBO_PRESET)->EnableWindow(FALSE);
	GetDlgItem(IDC_SETPRESET)->EnableWindow(FALSE);
	GetDlgItem(IDC_PRESETNAME)->EnableWindow(FALSE);
	GetDlgItem(IDC_DELETEPRESET)->EnableWindow(FALSE);

    // Change Z-order of the activeX control to be below all other controls, to prevent covering others after changing to larger resolution
    CWnd* pChild = GetTopWindow();
    while(pChild->GetNextWindow() != NULL)
    {
        pChild = pChild->GetNextWindow();
    }
    if((HWND)(*pChild) != (HWND)m_LiveActievX)
    {
        m_LiveActievX.SetWindowPos(pChild, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
    }

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLiveSampleDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if (nChar == VK_ESCAPE)
    {
        return;
    }
	CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
}

void CLiveSampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLiveSampleDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

void CLiveSampleDlg::OnTimer(UINT_PTR nIDEvent)
{
    // TODO: Add your message handler code here and/or call default
	switch(nIDEvent)
	{
	case TIMER_UPDATE_UI:
		{
			UpdatePTZUI();
			break;
		}
	}
    CDialog::OnTimer(nIDEvent);
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CLiveSampleDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLiveSampleDlg::OnButtonConnect() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
    m_LiveActievX.SetIPAddress(m_IPAddress);
    m_LiveActievX.SetUserName(m_UserName);
    m_LiveActievX.SetPassword(m_Password);
    m_LiveActievX.Connect();

}

void CLiveSampleDlg::OnButtonDisconnect() 
{
	// TODO: Add your control notification handler code here
    m_LiveActievX.DisConnect();	
}

void CLiveSampleDlg::OnButtonCameralist() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	UpdateCameraList();
    UpdateData(FALSE);
}

void CLiveSampleDlg::OnConnectCamera() 
{
    UpdateData(TRUE);
	// TODO: Add your control notification handler code here
    m_LiveActievX.ConnectToCamera(m_CameraView,m_CameraList.GetCurSel());	
}

void CLiveSampleDlg::OnButtonDicount() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	int count = m_LiveActievX.GetDICount();
    m_Message.Format(_T("Digital Input Count: %d"), count);
    UpdateData(FALSE);
}

void CLiveSampleDlg::OnButtonDiname() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
    m_Message = m_LiveActievX.GetDIName(m_DIIndex);
    UpdateData(FALSE);
}

void CLiveSampleDlg::OnButtonDistatus() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	int status = m_LiveActievX.GetDIStatus(m_DIIndex);
    if (status > 0 )
    {
        status = 1;
    }
    m_Message.Format(_T("Digital Input Status: %d"), status);	
    UpdateData(FALSE);
}

void CLiveSampleDlg::OnButtonDocount() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	int count = m_LiveActievX.GetDOCount();
    m_Message.Format(_T("Digital Output Count: %d"), count);
    UpdateData(FALSE);	
}

void CLiveSampleDlg::OnButtonDoname() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
    m_Message = m_LiveActievX.GetDOName(m_DOIndex);
    UpdateData(FALSE);	
}

void CLiveSampleDlg::OnButtonDostatus() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	int status = m_LiveActievX.GetDOStatus(m_DOIndex);
    if (status > 0 )
    {
        status = 1;
    }
    m_Message.Format(_T("Digital Output Status: %d"), status);	
    UpdateData(FALSE);	
}


void CLiveSampleDlg::OnButtonSetdo() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	m_LiveActievX.SetDOStatus(m_DOIndex,0);
}


void CLiveSampleDlg::OnButton1x1() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.SetView1x1();
}

void CLiveSampleDlg::OnButton2x2() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.SetView2x2();
}

void CLiveSampleDlg::OnButton3x3() 
{
	// TODO: Add your control notification handler code here
    m_LiveActievX.SetView3x3();	
}

void CLiveSampleDlg::OnButton4x4() 
{
	// TODO: Add your control notification handler code here
    m_LiveActievX.SetView4x4();	
}

void CLiveSampleDlg::OnButtonFullscreen() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.SetViewFullScreen();
}

void CLiveSampleDlg::OnButtonGetCamerIndexFromView()
{
	UpdateData(TRUE);
	int CameraIndex = m_LiveActievX.GetCameraIndexFromView(m_CameraView);
	m_Message.Format(_T("Camera Index : %d"), CameraIndex);
	UpdateData(FALSE);
}

void CLiveSampleDlg::OnButtonSnapshot() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	m_LiveActievX.Snapshot(m_LiveActievX.GetActiveView(),m_SnapPath);
}

void CLiveSampleDlg::OnButtonSetactive() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	m_LiveActievX.SetActiveView(m_ActiveView);
}

void CLiveSampleDlg::OnButtonGetactive() 
{
	// TODO: Add your control notification handler code here
    int activeview  = m_LiveActievX.GetActiveView();
    m_Message.Format(_T(" Active View : %d"), activeview);
	//int CameraIndex = m_LiveActievX.GetCameraIndexFromView(m_ActiveView);
	//m_Message.Format("Camera Index : %d", CameraIndex);
    UpdateData(FALSE);
}

void CLiveSampleDlg::OnButtonEnableAudioAndSetActive() 
{
    // TODO: Add your control notification handler code here
    UpdateData(TRUE);
    m_LiveActievX.EnableAudio(m_ActiveView, true);
}

void CLiveSampleDlg::OnButtonDisableAudioAndSetActive() 
{
    // TODO: Add your control notification handler code here
    UpdateData(TRUE);
    m_LiveActievX.EnableAudio(m_ActiveView, false);
}

void CLiveSampleDlg::OnButtonRawimage() 
{
	// TODO: Add your control notification handler code here
    /*CString s = m_LiveActievX.GetRawImage(0,0);
    int length = s.GetLength();


    BYTE*  buff = new BYTE[width*height*3];
    buff = (BYTE*)s.GetBuffer(width*height*3);*/
    int ActiveView = m_LiveActievX.GetActiveView();
    int width = m_LiveActievX.GetRawImageWidth(ActiveView);
    int height = m_LiveActievX.GetRawImageHeight(ActiveView);
    VARIANT vaResult = m_LiveActievX.GetRawImage(ActiveView,0);
    		FILE *pf;
			char filename[256];
			sprintf(filename, "C:\\%dx%d_YUV12.raw",width,height);
			pf = fopen(filename, "wb");
			fwrite(vaResult.parray, 1, width*height*3, pf);
			fclose(pf); 

}

void CLiveSampleDlg::OnButtonRawimage2() 
{
	// TODO: Add your control notification handler code here
   /* CString s = m_LiveActievX.GetRawImage(0,1);
    int length = s.GetLength();
    int width = m_LiveActievX.GetRawImageWidth(0);
    int height = m_LiveActievX.GetRawImageHeight(0);

    BYTE*  buff1 = new BYTE[width*height*3];
    buff1 = (BYTE*)s.GetBuffer(width*height*3);
	*/
    //delete[] buff1;   
    int ActiveView = m_LiveActievX.GetActiveView();
    int width = m_LiveActievX.GetRawImageWidth(ActiveView);
    int height = m_LiveActievX.GetRawImageHeight(ActiveView);
    VARIANT vaResult = m_LiveActievX.GetRawImage(ActiveView,1);    
			FILE *pf;
			char filename[256];
			sprintf(filename, "C:\\%dx%d_RGB.raw",width,height);
			pf = fopen(filename, "wb");
			fwrite(vaResult.parray, 1, width*height*3, pf);
			fclose(pf);
}

void CLiveSampleDlg::OnButtonSetosd() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
    int icmd = 0;
    if (m_bDate)
    {
        icmd +=1;
    }
    if (m_bTime)
    {
        icmd +=2;
    }
    if (m_bCameraName)
    {
        icmd +=8;
    }
    if (m_bCameraBitrate)
    {
        icmd +=16;
    }
    int View = m_LiveActievX.GetActiveView();
	m_LiveActievX.SetOSDConfig(icmd,m_FontName,m_FontSize,m_FontR,m_FontG,m_FontB,m_BackR,m_BackG,m_BackB,m_bHasEdge,m_bBold,m_Transparency);
}

void CLiveSampleDlg::OnButtonPlay() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.Play();
}

void CLiveSampleDlg::OnButtonStop() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.Stop();
}

void CLiveSampleDlg::OnButtonDrop() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.Drop();
}

void CLiveSampleDlg::ButtonUp(int ctrlId)
{
    switch (ctrlId)
    {
    case IDC_BUTTON_ZOOMIN:
        m_LiveActievX.PTZZoomTele(0);
        break;
    case IDC_BUTTON_ZOOMOUT:
        m_LiveActievX.PTZZoomWide(0);
        break;
    case IDC_UP:
        m_LiveActievX.PTZUp(0);
        break;
    case IDC_DOWN:
        m_LiveActievX.PTZDown(0);
        break;
    case IDC_RIGHT:
        m_LiveActievX.PTZRight(0);
        break;
    case IDC_LEFT:
        m_LiveActievX.PTZLeft(0);
        break;
    case IDC_LEFTUP:
        m_LiveActievX.PTZUpLeft(0);
        break;
    case IDC_LEFTDOWN:
        m_LiveActievX.PTZDownLeft(0);
        break;
    case IDC_RIGHTUP:
        m_LiveActievX.PTZUpRight(0);
        break;
    case IDC_RIGHTDOWN:
        m_LiveActievX.PTZDownRight(0);
        break;
    }
}

void CLiveSampleDlg::ButtonDown(int ctrlId)
{
    switch (ctrlId)
    {
    case IDC_BUTTON_ZOOMIN:
        m_LiveActievX.PTZZoomTele(1);
        break;
    case IDC_BUTTON_ZOOMOUT:
        m_LiveActievX.PTZZoomWide(1);
        break;
    case IDC_UP:
        m_LiveActievX.PTZUp(1);
        break;
    case IDC_DOWN:
        m_LiveActievX.PTZDown(1);
        break;
    case IDC_RIGHT:
        m_LiveActievX.PTZRight(1);
        break;
    case IDC_LEFT:
        m_LiveActievX.PTZLeft(1);
        break;
    case IDC_LEFTUP:
        m_LiveActievX.PTZUpLeft(1);
        break;
    case IDC_LEFTDOWN:
        m_LiveActievX.PTZDownLeft(1);
        break;
    case IDC_RIGHTUP:
        m_LiveActievX.PTZUpRight(1);
        break;
    case IDC_RIGHTDOWN:
        m_LiveActievX.PTZDownRight(1);
        break;
    }
}

void CLiveSampleDlg::OnGetpreset() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
    int view = m_LiveActievX.GetActiveView();
    int Count = m_LiveActievX.GetPresetCount(view);
	//m_CameraCount.Format("%d", Count);
    CString PresetName;
    m_PresetName.ResetContent();
    int j=0;
    for ( int i = 0 ; i < 16 ; i++)
    {
        PresetName = m_LiveActievX.GetPresetName(view,i);
        if ( PresetName != "")
        {
            m_PresetName.InsertString(j, PresetName);
            m_PresetName.SetItemData(j, i);
            j++;
        }
    }
    m_PresetName.SetCurSel(0);
    UpdateData(FALSE);		
}

void CLiveSampleDlg::OnSelchangeComboPreset() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
    int view = m_LiveActievX.GetActiveView();
    m_LiveActievX.PTZPresetGO(view,m_PresetName.GetItemData(m_PresetName.GetCurSel()));
}

void CLiveSampleDlg::OnSetpreset() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    int view = m_LiveActievX.GetActiveView();
    m_LiveActievX.SetPreset(view, 0, m_PresetSet);
}

void CLiveSampleDlg::OnDeletepreset() 
{
	// TODO: Add your control notification handler code here
    int view = m_LiveActievX.GetActiveView();
	m_LiveActievX.SetPreset(view,-1,NULL);
}


BEGIN_EVENTSINK_MAP(CLiveSampleDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CLiveSampleDlg)
	ON_EVENT(CLiveSampleDlg, IDC_LIVEACTIVEXCTRL1, 1 /* OnEvent */, OnOnEventLiveactivexctrl1, VTS_I4 VTS_I4)
	ON_EVENT(CLiveSampleDlg, IDC_LIVEACTIVEXCTRL1, 2, OnCreateComplete, NULL)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CLiveSampleDlg::OnOnEventLiveactivexctrl1(long iEventID, long iParameter) 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
    switch (iEventID)
    {
        case 0:
            m_Message.Format(_T("Event : OnConnectSuccess"));
			UpdateCameraList();
            break;
        case 1:
            m_Message.Format(_T("Event : OnConnectFailed"));
            break;
        case 2:
            m_Message.Format(_T("Event : OnDisconnect"));
            break;
        case 3:
            m_Message.Format(_T("Event : OnCameraConencted"));
            break;
        case 4:
            m_Message.Format(_T("Event : OnCameraStopped"));
            break;
        case 5:
            m_Message.Format(_T("Event : OnCameraDropped"));
            break;
        case 6:
            m_Message.Format(_T("Event : OnNewFrameReceived : %d  frameCount : %d"), iParameter, iframeCount);
			iframeCount ++;
            break;
        default:
            ASSERT(0);
            break;
    }
    UpdateData(FALSE);
}

void CLiveSampleDlg::OnCreateComplete()
{
	SetTimer(TIMER_UPDATE_UI, 1000 / 30, NULL);
}

void CLiveSampleDlg::OnButtonFocusnear() 
{
	// TODO: Add your control notification handler code here
    m_LiveActievX.PTZFocusNear(TRUE);	
}

void CLiveSampleDlg::OnButtonFocusfar() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.PTZFocusFar(TRUE);
}

void CLiveSampleDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
    m_LiveActievX.ChangeResolution(m_Width,m_Height);
}

void CLiveSampleDlg::OnHome() 
{
	// TODO: Add your control notification handler code here
	m_LiveActievX.PTZHome();
}

void CLiveSampleDlg::OnDuplicateCamera()
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	m_LiveActievX.DuplicateCamera(m_CameraList.GetCurSel());
	OnButtonCameralist();
}

void CLiveSampleDlg::OnRemoveDuplicateCamera()
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	m_LiveActievX.RemoveDuplicateCamera(m_CameraList.GetCurSel());
	OnButtonCameralist();
}

void CLiveSampleDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CLiveSampleDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

BOOL CLiveSampleDlg::PreTranslateMessage(MSG* pMsg) 
{
    if (m_LiveActievX.ISFullScreen())
    {
	    if (pMsg->message == WM_CHAR)
        {
            return TRUE;
        }
    }
    
	return CDialog::PreTranslateMessage(pMsg);
}

void CLiveSampleDlg::UpdatePTZUI()
{
	bool bEnable = 0;
	bool bPane = 0;
	bool bTilt = 0;
	bool bZoom = 0;
	bool bFocus = 0;
	int viewIndex = m_LiveActievX.GetActiveView();
	int iCameraIndex = m_LiveActievX.GetCameraIndexFromView(viewIndex);
	if (iCameraIndex >= 0)
	{
		bEnable =  m_LiveActievX.GetPTZCapability(iCameraIndex, kPTZCapIndexEnable);
		bPane = m_LiveActievX.GetPTZCapability(iCameraIndex, kPTZCapIndexPane);
		bTilt = m_LiveActievX.GetPTZCapability(iCameraIndex, kPTZCapIndexTilt);
		bZoom = m_LiveActievX.GetPTZCapability(iCameraIndex, kPTZCapIndexZoom);
		bFocus = m_LiveActievX.GetPTZCapability(iCameraIndex, kPTZCapIndexFocus);
	}

	GetDlgItem(IDC_UP)->EnableWindow(bTilt);
	GetDlgItem(IDC_DOWN)->EnableWindow(bTilt);
	GetDlgItem(IDC_RIGHT)->EnableWindow(bPane);
	GetDlgItem(IDC_LEFT)->EnableWindow(bPane);
	GetDlgItem(IDC_RIGHTUP)->EnableWindow(bPane&&bTilt);
	GetDlgItem(IDC_RIGHTDOWN)->EnableWindow(bPane&&bTilt);
	GetDlgItem(IDC_LEFTUP)->EnableWindow(bPane&&bTilt);
	GetDlgItem(IDC_LEFTDOWN)->EnableWindow(bPane&&bTilt);
	GetDlgItem(IDC_BUTTON_ZOOMIN)->EnableWindow(bZoom);
	GetDlgItem(IDC_BUTTON_ZOOMOUT)->EnableWindow(bZoom);
	GetDlgItem(IDC_BUTTON_FOCUSFAR)->EnableWindow(bFocus);
	GetDlgItem(IDC_BUTTON_FOCUSNEAR)->EnableWindow(bFocus);
	GetDlgItem(IDC_HOME)->EnableWindow(bEnable);
	GetDlgItem(IDC_GETPRESET)->EnableWindow(bEnable);
	GetDlgItem(IDC_COMBO_PRESET)->EnableWindow(bEnable);
	GetDlgItem(IDC_SETPRESET)->EnableWindow(bEnable);
	GetDlgItem(IDC_PRESETNAME)->EnableWindow(bEnable);
	GetDlgItem(IDC_DELETEPRESET)->EnableWindow(bEnable);
}

void CLiveSampleDlg::UpdateCameraList()
{
    int CameraCount;
	CameraCount = m_LiveActievX.GetCameraCount();
    m_CameraList.ResetContent();    
	
    for (int i = 0 ; i < CameraCount ; i++)
    {
        CString CameraName;
        CameraName.Format(_T("CAM %d %s"), i, m_LiveActievX.GetCameraName(i));
        m_CameraList.InsertString(i,CameraName);
    }
    m_CameraList.SetCurSel(0);
}