<span style="font-size:12px;">//------------------------------------------------
int i = 1;
int j = 1;
//------------------------------------------------
void CTabPage2::OnStartDraw()
{
// TODO: Add your control notification handler code here
KillTimer(2);
//---------------------------------------------------
//设置Timer1
//---------------------------------------------------
SetTimer(1, 1000, NULL);
}
void CTabPage2::OnDrawLine()
{
// TODO: Add your control notification handler code here
KillTimer(1);
SetTimer(2, 2000, NULL);
}
void CTabPage2::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch (nIDEvent)
{
case 1: //Timer1的实际操作——画圆
{
SI_STATUS status = SI_SUCCESS;
// Confirm an error hasn't already occurred
if ( (m_bReadError == TRUE) || (m_bWriteError == TRUE) )
{
// Call base timer
CDialog::OnTimer(nIDEvent);
// Further processing not possible
return;
}
//-----------------------------------------------------
//发送指令给MCU
//-----------------------------------------------------
m_IObuffer.USB_Buffer[0] = i;
m_IObuffer.USB_Buffer[1] = i; //change led status
m_IObuffer.USB_Buffer[3] = 1; //send I2C read command
//USB data transmission
DWORD dwBytesSucceed = 0;
DWORD dwBytesWriteRequest = (sizeof(USB_iobuf)-4);
DWORD dwBytesReadRequest = (sizeof(USB_iobuf)-4);
// Init. before write
dwBytesSucceed = 0;
// Perform USB Data Transfer
// Write transfer packet
status = SI_Write(m_hUSBDevice, &m_IObuffer, dwBytesWriteRequest, &dwBytesSucceed);
if (dwBytesSucceed != dwBytesWriteRequest || status != SI_SUCCESS)
{
m_bWriteError = TRUE; // Note: Set error flag immediately so that multiple
// message boxes do not queue up.
CString sError;
sError.Format("Error writing to USB.\nWrote %d of %d bytes.\n\nApplication is aborting.\nReset hardware and try again.", dwBytesSucceed, dwBytesWriteRequest);
if (AfxMessageBox(sError,MB_OK|MB_ICONEXCLAMATION))
{
OnCancel();
return;
}
}
//--------------------------------------------
//Read I2C Data from USB
//--------------------------------------------
// Init. before read
dwBytesSucceed = 0;
memset(&m_IObuffer, 0, sizeof(USB_iobuf));
// Read transfer packet
status = SI_Read(m_hUSBDevice, &m_IObuffer, dwBytesReadRequest, &dwBytesSucceed);
if (((dwBytesSucceed != dwBytesReadRequest) && (m_bReadError == FALSE)) || status != SI_SUCCESS)
{
m_bReadError = TRUE; // Note: Set error flag immediately so that multiple
// message boxes do not queue up.
CString sError;
sError.Format("Error reading from USB.\nRead %d of %d bytes.\n\nApplication is aborting.\nReset hardware and try again.", dwBytesSucceed, dwBytesReadRequest);
if (AfxMessageBox(sError,MB_OK|MB_ICONEXCLAMATION))
{
OnCancel();
return;
}
}
//****************************************************************
//绘图操作,以下绘图均在后台完成,图像不会在屏幕上显示
//****************************************************************
//----------------------------------------------------------------
//设置后台环境
//----------------------------------------------------------------
CDC *pDC; //创建目标DC指针
pDC=GetDlgItem(IDC_Bmp)->GetDC(); //将目标DC指向实际位图控件IDC_Bmp
CBitmap bmp; //创建后台位图
bmp.CreateCompatibleBitmap(pDC,900,380); //根据实际位图控件为后台位图分配内存空间
CDC bDC; //创建后台DC
bDC.CreateCompatibleDC(pDC); //根据实际DC为后台DC分配内存空间
bDC.SelectObject(&bmp); //将后台DC与后台位图联系起来,即后台DC选好后台位图
//-------------------------------------------------------------------
//建立剪裁区
CRgn rgn;
rgn.CreateRectRgn(0,0,900,380); //创建一个矩形裁剪区,对象为rgn
bDC.SelectClipRgn(&rgn); //选择该裁剪区,绘图在此裁剪区中完成,超出部分不显示
//--------------------------------------------------------------------------------------------
//画背景
CBrush brush(RGB(128,128,128));
CBrush *old = bDC.SelectObject(&brush);
CRect rect;
rect.SetRect(0,0,900,380);
bDC.Rectangle(rect);
bDC.SelectObject(old);
//--------------------------------------------------------------------------------------------
//Draw Circle use brush
CBrush brush1(RGB(255,0,0)); //red
old = bDC.SelectObject(&brush1);
CRect cRect;
cRect.SetRect(m_IObuffer.USB_Buffer[0],m_IObuffer.USB_Buffer[0],m_IObuffer.USB_Buffer[0],m_IObuffer.USB_Buffer[0]); //圆心是USBbuffer第一个字节数据
cRect.InflateRect(50,50); //半径是50
bDC.Ellipse(cRect);
bDC.SelectObject(old);
CBrush brush2(RGB(255,255,0)); //yellow
old = bDC.SelectObject(&brush2);
cRect.SetRect(m_IObuffer.USB_Buffer[1],m_IObuffer.USB_Buffer[1],m_IObuffer.USB_Buffer[1],m_IObuffer.USB_Buffer[1]); //圆心是USBbuffer第二个字节数据
cRect.InflateRect(30,30); //半径是30
bDC.Ellipse(cRect);
bDC.SelectObject(old);
//***********************************************************************************************
//将后台画好的图像在显示器的目标控件中实际显示出来,以下操作完成真正在显示器上显示的操作
//***********************************************************************************************
pDC->BitBlt(0,0,900, 380, &bDC, 0, 0, SRCCOPY);
//-----------------------------------------------------
//change led status
i = ~i;
break;
}
case 2: //Timer2的实际操作——画线
{
SI_STATUS status = SI_SUCCESS;
// Confirm an error hasn't already occurred
if ( (m_bReadError == TRUE) || (m_bWriteError == TRUE) )
{
// Call base timer
CDialog::OnTimer(nIDEvent);
// Further processing not possible
return;
}
//-----------------------------------------------------
//发送指令给MCU
//-----------------------------------------------------
m_IObuffer.USB_Buffer[0] = j;
m_IObuffer.USB_Buffer[1] = j; //change led status
m_IObuffer.USB_Buffer[3] = 1; //send I2C read command
//USB data transmission
DWORD dwBytesSucceed = 0;
DWORD dwBytesWriteRequest = (sizeof(USB_iobuf)-4);
DWORD dwBytesReadRequest = (sizeof(USB_iobuf)-4);
// Init. before write
dwBytesSucceed = 0;
// Perform USB Data Transfer
// Write transfer packet
status = SI_Write(m_hUSBDevice, &m_IObuffer, dwBytesWriteRequest, &dwBytesSucceed);
if (dwBytesSucceed != dwBytesWriteRequest || status != SI_SUCCESS)
{
m_bWriteError = TRUE; // Note: Set error flag immediately so that multiple
// message boxes do not queue up.
CString sError;
sError.Format("Error writing to USB.\nWrote %d of %d bytes.\n\nApplication is aborting.\nReset hardware and try again.", dwBytesSucceed, dwBytesWriteRequest);
if (AfxMessageBox(sError,MB_OK|MB_ICONEXCLAMATION))
{
OnCancel();
return;
}
}
//--------------------------------------------
//Read I2C Data from USB
//--------------------------------------------
// Init. before read
dwBytesSucceed = 0;
memset(&m_IObuffer, 0, sizeof(USB_iobuf));
// Read transfer packet
status = SI_Read(m_hUSBDevice, &m_IObuffer, dwBytesReadRequest, &dwBytesSucceed);
if (((dwBytesSucceed != dwBytesReadRequest) && (m_bReadError == FALSE)) || status != SI_SUCCESS)
{
m_bReadError = TRUE; // Note: Set error flag immediately so that multiple
// message boxes do not queue up.
CString sError;
sError.Format("Error reading from USB.\nRead %d of %d bytes.\n\nApplication is aborting.\nReset hardware and try again.", dwBytesSucceed, dwBytesReadRequest);
if (AfxMessageBox(sError,MB_OK|MB_ICONEXCLAMATION))
{
OnCancel();
return;
}
}
//****************************************************************
//绘图操作,以下绘图均在后台完成,图像不会在屏幕上显示
//****************************************************************
//----------------------------------------------------------------
//设置后台环境
//----------------------------------------------------------------
CDC *pDC; //创建目标DC指针
pDC=GetDlgItem(IDC_Bmp)->GetDC(); //将目标DC指向实际位图控件IDC_Bmp
CBitmap bmp; //创建后台位图
bmp.CreateCompatibleBitmap(pDC,900,380); //根据实际位图控件为后台位图分配内存空间
CDC bDC; //创建后台DC
bDC.CreateCompatibleDC(pDC); //根据实际DC为后台DC分配内存空间
bDC.SelectObject(&bmp); //将后台DC与后台位图联系起来,即后台DC选好后台位图
//-------------------------------------------------------------------
//建立剪裁区
CRgn rgn;
rgn.CreateRectRgn(0,0,900,380); //创建一个矩形裁剪区,对象为rgn
bDC.SelectClipRgn(&rgn); //选择该裁剪区,绘图在此裁剪区中完成,超出部分不显示
//--------------------------------------------------------------------------------------------
//画背景
CBrush brush(RGB(128,128,128));
CBrush *old = bDC.SelectObject(&brush);
CRect rect;
rect.SetRect(0,0,900,380);
bDC.Rectangle(rect);
bDC.SelectObject(old);
//--------------------------------------------------------------------------------------------
//draw line
//Setting the pen
CPen Pen;
Pen.CreatePen(PS_SOLID, 10, RGB(0, 0, 255));
CPen* Penold=bDC.SelectObject(&Pen);
bDC.MoveTo(m_IObuffer.USB_Buffer[1],m_IObuffer.USB_Buffer[1]);
bDC.LineTo(m_IObuffer.USB_Buffer[2],m_IObuffer.USB_Buffer[2]);
bDC.MoveTo(m_IObuffer.USB_Buffer[2],m_IObuffer.USB_Buffer[2]);
bDC.LineTo(850,380);
//***********************************************************************************************
//将后台画好的图像在显示器的目标控件中实际显示出来,以下操作完成真正在显示器上显示的操作
//***********************************************************************************************
pDC->BitBlt(0,0,900, 380, &bDC, 0, 0, SRCCOPY);
//change led status
j = ~ j;
break;
}
default: break;
}
// CDialog::OnTimer(nIDEvent);
}
void CTabPage2::OnStopDraw()
{
// TODO: Add your control notification handler code here
KillTimer(1);
KillTimer(2);
Invalidate(TRUE); //清屏操作
}</span>