// HbxAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "HbxApp.h"
#include "HbxAppDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define    MYWM_NOTIFY    WM_USER+177
#define    MYWM_NOTIFY1   WM_USER+178
 HINSTANCE hDllInst;     
typedef int (__stdcall* XGetInput)(int * pHeader,char * pCount);
typedef int (__stdcall* XOpen)(HWND hparent);
typedef void (__stdcall* Xinit)(void);
typedef int (__stdcall* XSendOutput)( BYTE* pMapData, BYTE* pLength);
typedef void (__stdcall* XClose)(void);
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
int KeyPara;
BYTE swkey;
BYTE newkeycnt;
BYTE oldkey;
BYTE  TestIconFlag;
BYTE  TestCnt;
BYTE  TestCnt1;
CString strKeyInfo;
int sumWhel;
	short m_scrollpos;
	#define BITMAPLENGTH1 30
	#define MAXLENGTH      56
BYTE const bitmap1[BITMAPLENGTH1]={
	0x00,0x40,0x20,0xF8,0x07,0x00,0xC8,0x08,0x09,0x0E,0x08,0xE8,0x08,0x00,0x00,0x00,
  0x00,0x00,0x3F,0x00,0x20,0x20,0x27,0x20,0x30,0x2E,0x21,0x20,0x20,0x00,
	};
	#define BITMAPLENGTH2 48
	BYTE const bitmap2[BITMAPLENGTH2]={
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x10,0x10,0x00,0x00,
0x07,0x08,0x10,0x10,0x10,0x08,0x07,0x00,0x00,0x00,0x00,0x19,0x18,0x00,0x00,0x00,
	};
	
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()

/////////////////////////////////////////////////////////////////////////////
// CHbxAppDlg dialog

CHbxAppDlg::CHbxAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHbxAppDlg::IDD, pParent)
	, m_strlin2(_T("2"))
	, m_strlin3(_T("3"))
	, m_strlin4(_T("4"))
{
	//{{AFX_DATA_INIT(CHbxAppDlg)
	m_strLine1 = "1";
	m_strUsb = _T("");
	newkeycnt=0;
	oldkey=0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHbxAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHbxAppDlg)
	DDX_Text(pDX, IDC_STATICBT, m_strbut);
	DDX_Text(pDX, IDC_EDITLED, m_strLine1);
	DDX_Text(pDX, IDC_STATICUSB, m_strUsb);
 
	//}}AFX_DATA_MAP

	DDX_Text(pDX, IDC_EDITLED2, m_strlin2);
	DDX_Text(pDX, IDC_EDITLED3, m_strlin3);
	DDX_Text(pDX, IDC_EDITLED4, m_strlin4);
}

BEGIN_MESSAGE_MAP(CHbxAppDlg, CDialog)
	//{{AFX_MSG_MAP(CHbxAppDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_MESSAGE(MYWM_NOTIFY,OnTaskKeyPress)
	ON_MESSAGE(MYWM_NOTIFY1,OnUSbScan)
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON1, OnButtonDw)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON4, &CHbxAppDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &CHbxAppDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CHbxAppDlg::OnBnClickedButton6)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHbxAppDlg message handlers

BOOL CHbxAppDlg::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);
		}
	}
 swkey=0;
 sumWhel=0;
 TestIconFlag=0;
 TestCnt=0;
 TestCnt1=0;
  HB_OpenFlag=FALSE;
  KeyPara=0;
  if (NULL == hDllInst)
			      hDllInst= LoadLibrary("PHB02C.dll");    
	if(hDllInst!=NULL)		   
{		   
		  XOpen  hbopen;
		  Xinit  hbinit; 
		  hbinit = (Xinit)GetProcAddress(hDllInst, "Xinit");
		  if(hbinit)
		     hbinit();
		 
		  hbopen = (XOpen)GetProcAddress(hDllInst, "XOpen");
		  if(hbopen) 
		  {	
		      if(!hbopen(m_hWnd))
		 	      HB_OpenFlag=TRUE;
		  }
}
 
	// 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
	HB_OpenSta=HB_OpenFlag;

	
	
	//timerOn=0;
            if(!HB_OpenFlag)
		        {
	              m_strUsb="请插入PHB02C 控制器！";
					  }else
					  {
					  		m_strUsb="PHB02C控制器工作正常！";
	          }
	   UpdateData(FALSE); 
	  	htimer=SetTimer(17,200,NULL);
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}
  LRESULT   CHbxAppDlg::OnTaskKeyPress(WPARAM   wParam,LPARAM   lParam)  
  {  

            /*
			//===================================================================================
			//user could call XGetInput  function to get data from device in timer or while loop
			//===================================================================================
            int getbufer[3];
            char  getcnt;
            if(hDllInst!=NULL)		   
            {		   
            
						  XGetInput  xhcreadkey;
						  xhcreadkey = (XGetInput)GetProcAddress(hDllInst, "XGetInput");
						  if(xhcreadkey)
						  {
						        getcnt=2;
		                xhcreadkey(getbufer,&getcnt);
		                lParam=getbufer[0];             
		          }
		        }
				*/
            
            
            
            BYTE key1=lParam&0xff;
            BYTE  updatfalg=0;
            BYTE  key2=lParam>>8;
            	    CRect rt;  	
            CString keyinfo;

            m_strbut="";
		      	keyinfo="";
		       if(key1)
		        {
					 if(oldkey==key1)
						 {
						      newkeycnt++;
							  updatfalg=1;
						 }else
						 {
						      newkeycnt=0;
							  oldkey=key1;
						 }
		        		 //m_strbut.Format("Num1:%d keypressed Times:%d",key1,newkeycnt);
		        		 m_strbut.Format("Num1:%d keypressed",key1);
		        		 m_strUsb=m_strbut;
						
		        }else
			   {
				 newkeycnt=0;
				}
		         if(key2)
		        {
		        		 keyinfo.Format(" Num2:%d keypressed",key2);
		        		 m_strUsb+=keyinfo;
		        }
		        m_strbut+=keyinfo;  
			
           if(strKeyInfo!=m_strbut)
			    {
			        strKeyInfo= m_strbut;
			      
			    	  
			    }
			    if(KeyPara!=lParam)
			    {
			    		KeyPara=lParam;
			    		    updatfalg=1;	
			    }
			   //===============================================================================
			

			   //======================================================================================

			   //==========================================================================

							  if(updatfalg)
							  	UpdateData(FALSE); 
			   return  TRUE;
  } 
   LRESULT   CHbxAppDlg::OnUSbScan(WPARAM   wParam,LPARAM   lParam)  
  {  
            if(HB_OpenFlag==FALSE)
					 	{
					 		  XClose  hbclose;
							  hbclose = (XClose)GetProcAddress(hDllInst, "XClose");
							  if(hbclose)
							     hbclose();
					 		  XOpen     hbopen;		
					 			hbopen = (XOpen)GetProcAddress(hDllInst, "XOpen");
					      if(hbopen)
					      {
								 if(!hbopen(m_hWnd))
								   	HB_OpenFlag=TRUE;
								   else
								  {
									   HB_OpenFlag=0;							   
								   }	
						}						
					  }
					  if(!HB_OpenFlag)
		        {
	              m_strUsb="please insert USB controller！";
					  }else
					  {
						   if(lParam==1)
                           
							   m_strUsb="Download Ok！";
						   else

					  		m_strUsb="PHB02C Job Ok！";
						       UpdateData(FALSE); 
	          }
					  if(HB_OpenSta!=HB_OpenFlag)
					  {
					  		HB_OpenSta=HB_OpenFlag;					  		
			               UpdateData(FALSE); 
					  }					  
			      return  TRUE;
  } 
  
  
void CHbxAppDlg::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 CHbxAppDlg::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();
	}
}

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

void CHbxAppDlg:: DownloadLed() 
{
  	BYTE downbuffer[62];
  	
	// TODO: Add your control notification handler code here
	  if (NULL == hDllInst)
			      hDllInst= LoadLibrary("PHB02C.dll");    
	 if (NULL == hDllInst)
	 {
	 		MessageBox("load DLL Error!");
	 	  return;
	 }
  if(HB_OpenFlag==FALSE)
 	{
 		  XClose  hbclose;
		  hbclose = (XClose)GetProcAddress(hDllInst, "XClose");
		  if(hbclose)
		     hbclose();
 		  XOpen     hbopen;		
 			hbopen = (XOpen)GetProcAddress(hDllInst, "XOpen");
    if(hbopen)
    {
			 if(!hbopen(m_hWnd))
			 	HB_OpenFlag=TRUE;
		}	
  }
  if(HB_OpenFlag==FALSE)
  {
  		MessageBox("Open Device Error!");
      return;  		
  }
//  short strDw;
  UpdateData(TRUE); 
 
 // strDw=m_IntLed;//1YES+1NO+0+1HIGHSPEED+4bit reserved;8bit step led
 
    XSendOutput hbwt;

		hbwt = (XSendOutput)GetProcAddress(hDllInst, "XSendOutput");
    if(hbwt)
    {
		  BYTE sendcnt;
		   BYTE sendlength;
		   BYTE i;;
		  char *pBuff;
		  sendcnt=0;
		  sendlength=m_strLine1.GetLength();
		  if(sendlength>11)
			  sendlength=11;
		   pBuff=m_strLine1.GetBuffer(0);
		  for(i=0;i<sendlength;i++)
		  {
		  	downbuffer[sendcnt++]=pBuff[i];		  
		  }
		   for(;i<11;i++)//first line only 12 character
		  {
		  	downbuffer[sendcnt++]=0x20;		  
		  }
//================================================
   pBuff=m_strlin2.GetBuffer(0);
    sendlength=m_strlin2.GetLength();  //second line 16 character
		  if(sendlength>16)
			  sendlength=16;
		  for(i=0;i<sendlength;i++)
		  {
		  	downbuffer[sendcnt++]=pBuff[i];		  
		  }
		   for(;i<16;i++)
		  {
		  	downbuffer[sendcnt++]=0x20;		  
		  }
//======================================================
        pBuff=m_strlin3.GetBuffer(0);
		     sendlength=m_strlin3.GetLength();  //third line 16 character
		  if(sendlength>16)
			  sendlength=16;
		  for(i=0;i<sendlength;i++)
		  {
		  	downbuffer[sendcnt++]=pBuff[i];		  
		  }
		   for(;i<16;i++)
		  {
		  	downbuffer[sendcnt++]=0x20;		  
		  }	 
		  
		  //================================================= 	
		  
		  //======================================================
        pBuff=m_strlin4.GetBuffer(0);
		     sendlength=m_strlin4.GetLength();  //fourth line 16 character
		  if(sendlength>16)
			  sendlength=16;
		  for(i=0;i<sendlength;i++)
		  {
		  	downbuffer[sendcnt++]=pBuff[i];		  
		  }
		   for(;i<16;i++)
		  {
		  	downbuffer[sendcnt++]=0x20;		  
		  }	 
		  
		  //================================================= 	
		  
		  
          if(!hbwt(downbuffer,&sendcnt))
		  {
		  		//MessageBox("Download OK!");
			  //	m_strUsb="Download OK!";
			  	::PostMessage(m_hWnd,MYWM_NOTIFY1,0,1);	
		  }
		  else
		  {
		  		//MessageBox("Download Error!");
		  		HB_OpenFlag=FALSE;
		  }
    }else
    {
    		//MessageBox("load DLL Function Error!");
    }

}

void CHbxAppDlg::OnDestroy() 
{
	CDialog::OnDestroy();
if(htimer)	
		KillTimer(htimer);
	// TODO: Add your message handler code here 
	if(hDllInst!=NULL)		   
  {		   
		  XClose  hbclose;

		  hbclose = (XClose)GetProcAddress(hDllInst, "XClose");
		  if(hbclose)
		     hbclose();
	}	     
}
/*
void CHbxAppDlg::OnChangeEditmx() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
UpdateData(TRUE); 
}

void CHbxAppDlg::OnChangeEditmy() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE); 
}

void CHbxAppDlg::OnChangeEditmz() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE); 
}

void CHbxAppDlg::OnChangeEditstep() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE); 
}

void CHbxAppDlg::OnChangeEditwx() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE); 
}

void CHbxAppDlg::OnChangeEditwy() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE); 
}

void CHbxAppDlg::OnChangeEditwz() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE); 
}
*/
void CHbxAppDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	 if(!HB_OpenSta)
	 {
	 		
	 		::PostMessage(m_hWnd,MYWM_NOTIFY1,0,0);	
	 }
	 if(TestIconFlag)
	 {
	 		TestCnt++;
	 		TestCnt1++;
	 		if(TestCnt==2)
	 		{
	 				ClrbitMap(0);
	 		}else if(TestCnt==1)
	 		{
	 				LigntMAP(0);
	 		}
	 		else if(TestCnt==3)
	 		{
	 				LigntMAP(1);
	 		}else if(TestCnt==4)
	 		{
	 				ClrbitMap(1);
	 		}
	 	  if(TestCnt>=6)
	 		{
	 				TestCnt=0;
	 		}
	 		if(TestCnt1>100)
	 		{
	 				TestIconFlag=0;
	 				DownloadLed();
	 		}
	 		
	 }
	 CDialog::OnTimer(nIDEvent);
}

void CHbxAppDlg::OnButtonDw() 
{
	// TODO: Add your control notification handler code here
	 UpdateData(TRUE); 
	 DownloadLed();
}
void CHbxAppDlg::ClrbitMap(BYTE lightw) 
{
	// TODO: Add your control notification handler code here
		BYTE downbuffer[62];
  	
	// TODO: Add your control notification handler code here
	  if (NULL == hDllInst)
			      hDllInst= LoadLibrary("PHB02C.dll");    
	 if (NULL == hDllInst)
	 {
	 		MessageBox("load DLL Error!");
	 	  return;
	 }
  if(HB_OpenFlag==FALSE)
 	{
 		  XClose  hbclose;
		  hbclose = (XClose)GetProcAddress(hDllInst, "XClose");
		  if(hbclose)
		     hbclose();
 		  XOpen     hbopen;		
 			hbopen = (XOpen)GetProcAddress(hDllInst, "XOpen");
    if(hbopen)
    {
			 if(!hbopen(m_hWnd))
			 	HB_OpenFlag=TRUE;
		}	
  }
  if(HB_OpenFlag==FALSE)
  {
  		MessageBox("Open Device Error!");
      return;  		
  }
//  short strDw;
  UpdateData(TRUE); 
 
 // strDw=m_IntLed;//1YES+1NO+0+1HIGHSPEED+4bit reserved;8bit step led
 
    XSendOutput hbwt;

		hbwt = (XSendOutput)GetProcAddress(hDllInst, "XSendOutput");
    if(hbwt)
    {
		  BYTE sendcnt;
		   BYTE sendlength;
		   BYTE i;
		  char *pBuff;
 
		  
      downbuffer[0]=0x1A;//bitmap
         if(lightw==0)
        downbuffer[1]=0x06;//row and col
      else  
        downbuffer[1]=0x08;//row and col  
      downbuffer[2]=0;//length
      sendcnt=3;
		  
		  //================================================= 	
		  
		  
          if(!hbwt(downbuffer,&sendcnt))
		  {
		  		//MessageBox("Download OK!");
			  //	m_strUsb="Download OK!";
			  	::PostMessage(m_hWnd,MYWM_NOTIFY1,0,1);	
		  }
		  else
		  {
		  		//MessageBox("Download Error!");
		  		HB_OpenFlag=FALSE;
		  }
    }else
    {
    		//MessageBox("load DLL Function Error!");
    }
}
void CHbxAppDlg::LigntMAP(BYTE lightw)
{
	// TODO: 在此添加控件通知处理程序代码

	BYTE downbuffer[62];
  	
	// TODO: Add your control notification handler code here
	  if (NULL == hDllInst)
			      hDllInst= LoadLibrary("PHB02C.dll");    
	 if (NULL == hDllInst)
	 {
	 		MessageBox("load DLL Error!");
	 	  return;
	 }
  if(HB_OpenFlag==FALSE)
 	{
 		  XClose  hbclose;
		  hbclose = (XClose)GetProcAddress(hDllInst, "XClose");
		  if(hbclose)
		     hbclose();
 		  XOpen     hbopen;		
 			hbopen = (XOpen)GetProcAddress(hDllInst, "XOpen");
    if(hbopen)
    {
			 if(!hbopen(m_hWnd))
			 	HB_OpenFlag=TRUE;
		}	
  }
  if(HB_OpenFlag==FALSE)
  {
  		MessageBox("Open Device Error!");
      return;  		
  }
//  short strDw;
  UpdateData(TRUE); 
 
 // strDw=m_IntLed;//1YES+1NO+0+1HIGHSPEED+4bit reserved;8bit step led
 
    XSendOutput hbwt;

		hbwt = (XSendOutput)GetProcAddress(hDllInst, "XSendOutput");
    if(hbwt)
    {
		  BYTE sendcnt;
		   BYTE sendlength;
		   BYTE i;;
		  char *pBuff;
 
		  
      downbuffer[0]=0x1A;//bitmap
      if(lightw==0)
        downbuffer[1]=0x06;//row and col
      else  
        downbuffer[1]=0x08;//row and col  
      downbuffer[2]=MAXLENGTH;//MAXLENGTH LIGHT
      sendcnt=3;
  
		  //================================================= 	
		  
		  
          if(!hbwt(downbuffer,&sendcnt))
		  {
		  		//MessageBox("Download OK!");
			  //	m_strUsb="Download OK!";
			  	::PostMessage(m_hWnd,MYWM_NOTIFY1,0,1);	
		  }
		  else
		  {
		  		//MessageBox("Download Error!");
		  		HB_OpenFlag=FALSE;
		  }
    }else
    {
    		//MessageBox("load DLL Function Error!");
    }

}

void CHbxAppDlg::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码

	BYTE downbuffer[62];
  	
	// TODO: Add your control notification handler code here
	  if (NULL == hDllInst)
			      hDllInst= LoadLibrary("PHB02C.dll");    
	 if (NULL == hDllInst)
	 {
	 		MessageBox("load DLL Error!");
	 	  return;
	 }
  if(HB_OpenFlag==FALSE)
 	{
 		  XClose  hbclose;
		  hbclose = (XClose)GetProcAddress(hDllInst, "XClose");
		  if(hbclose)
		     hbclose();
 		  XOpen     hbopen;		
 			hbopen = (XOpen)GetProcAddress(hDllInst, "XOpen");
    if(hbopen)
    {
			 if(!hbopen(m_hWnd))
			 	HB_OpenFlag=TRUE;
		}	
  }
  if(HB_OpenFlag==FALSE)
  {
  		MessageBox("Open Device Error!");
      return;  		
  }
//  short strDw;
  UpdateData(TRUE); 
 
 // strDw=m_IntLed;//1YES+1NO+0+1HIGHSPEED+4bit reserved;8bit step led
 
    XSendOutput hbwt;

		hbwt = (XSendOutput)GetProcAddress(hDllInst, "XSendOutput");
    if(hbwt)
    {
		  BYTE sendcnt;
		   BYTE sendlength;
		   BYTE i;;
		  char *pBuff;
 
		  
      downbuffer[0]=0x1A;//bitmap
      downbuffer[1]=0x06;//row and col
      downbuffer[2]=BITMAPLENGTH1;//row and col
      sendcnt=3;
 
      for(i=0;i<BITMAPLENGTH1;i++)
		  {
		  	downbuffer[sendcnt++]=bitmap1[i];		  
		  }	 
		   for(i=sendcnt;i<59;i++)
		  {
		  	downbuffer[sendcnt++]=0x20;		  
		  }	 
		  
		  //================================================= 	
		  
		  
          if(!hbwt(downbuffer,&sendcnt))
		  {
		  		//MessageBox("Download OK!");
			  //	m_strUsb="Download OK!";
			  	::PostMessage(m_hWnd,MYWM_NOTIFY1,0,1);	
		  }
		  else
		  {
		  		//MessageBox("Download Error!");
		  		HB_OpenFlag=FALSE;
		  }
    }else
    {
    		//MessageBox("load DLL Function Error!");
    }

}


void CHbxAppDlg::OnBnClickedButton5()//test 
{
	// TODO: 在此添加控件通知处理程序代码
	TestIconFlag=1;
	TestCnt=0;
	TestCnt1=0;
}


void CHbxAppDlg::OnBnClickedButton6()
{
	// TODO: 在此添加控件通知处理程序代码

BYTE downbuffer[62];
  	
	// TODO: Add your control notification handler code here
	  if (NULL == hDllInst)
			      hDllInst= LoadLibrary("PHB02C.dll");    
	 if (NULL == hDllInst)
	 {
	 		MessageBox("load DLL Error!");
	 	  return;
	 }
  if(HB_OpenFlag==FALSE)
 	{
 		  XClose  hbclose;
		  hbclose = (XClose)GetProcAddress(hDllInst, "XClose");
		  if(hbclose)
		     hbclose();
 		  XOpen     hbopen;		
 			hbopen = (XOpen)GetProcAddress(hDllInst, "XOpen");
    if(hbopen)
    {
			 if(!hbopen(m_hWnd))
			 	HB_OpenFlag=TRUE;
		}	
  }
  if(HB_OpenFlag==FALSE)
  {
  		MessageBox("Open Device Error!");
      return;  		
  }
//  short strDw;
  UpdateData(TRUE); 
 
 // strDw=m_IntLed;//1YES+1NO+0+1HIGHSPEED+4bit reserved;8bit step led
 
    XSendOutput hbwt;

		hbwt = (XSendOutput)GetProcAddress(hDllInst, "XSendOutput");
    if(hbwt)
    {
		  BYTE sendcnt;
		   BYTE sendlength;
		   BYTE i;;
		  char *pBuff;
 
		  
      downbuffer[0]=0x1A;//bitmap
      downbuffer[1]=0x08;//row and col
      downbuffer[2]=BITMAPLENGTH2;//row and col
      sendcnt=3;
 
      for(i=0;i<BITMAPLENGTH2;i++)
		  {
		  	downbuffer[sendcnt++]=bitmap2[i];		  
		  }	 
		   for(i=sendcnt;i<59;i++)
		  {
		  	downbuffer[sendcnt++]=0x20;		  
		  }	 
		  
		  //================================================= 	
		  
		  
          if(!hbwt(downbuffer,&sendcnt))
		  {
		  		//MessageBox("Download OK!");
			  //	m_strUsb="Download OK!";
			  	::PostMessage(m_hWnd,MYWM_NOTIFY1,0,1);	
		  }
		  else
		  {
		  		//MessageBox("Download Error!");
		  		HB_OpenFlag=FALSE;
		  }
    }else
    {
    		//MessageBox("load DLL Function Error!");
    }
}
