⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfctransferdlg.cpp

📁 介绍控制器运动的例子
💻 CPP
字号:
// MFCTransferDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCTransfer.h"
#include "MFCTransferDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMFCTransferDlg dialog

CMFCTransferDlg::CMFCTransferDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCTransferDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMFCTransferDlg)
	m_Total = 0;
	m_Status = _T("Disconnected");
	m_Port = 1;
	m_IP = _T("0.0.0.0");
	m_Filename = _T("");
	m_Dest = _T("PROG0");
	m_Cardnbr = 0;
	m_Bytes = 0;
	m_BPS = 38400;
	m_BusType = FALSE;
	m_Transport = 0;
	m_Product = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

  // **ADDED** 
  // Create Interfaces using Prog ID
  m_Util = new IUtility;
  m_Util->CreateDispatch(_T("BoxBridge.Utility.1"));
  m_LookAtStatus = false;
  m_IdTimer = -1;
  // **ADDED**
}

// **ADDED** 
CMFCTransferDlg::~CMFCTransferDlg()
{
  // Need to Clean up COM
  if(m_Util){
    m_Util->Disconnect();
    delete m_Util;
  }
  // CoUninitialize();       // Clean up of COM done automatically by MFC
}
// **ADDED** 

void CMFCTransferDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMFCTransferDlg)
	DDX_Control(pDX, IDC_CBO_PROG, m_ProgName);
	DDX_Text(pDX, IDC_TXT_TOTAL, m_Total);
	DDX_Text(pDX, IDC_TXT_STATUS, m_Status);
	DDX_Text(pDX, IDC_TXT_PORT, m_Port);
	DDX_Text(pDX, IDC_TXT_IP, m_IP);
	DDX_Text(pDX, IDC_TXT_FILENAME, m_Filename);
	DDX_Text(pDX, IDC_TXT_DEST, m_Dest);
	DDX_Text(pDX, IDC_TXT_CARDNBR, m_Cardnbr);
	DDX_Text(pDX, IDC_TXT_BYTES, m_Bytes);
	DDX_Text(pDX, IDC_TXT_BPS, m_BPS);
	DDX_Check(pDX, IDC_CHK_PCI, m_BusType);
	DDX_Radio(pDX, IDC_RDO_TRANSPORT, m_Transport);
	DDX_CBIndex(pDX, IDC_CBO_PROD, m_Product);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMFCTransferDlg, CDialog)
	//{{AFX_MSG_MAP(CMFCTransferDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_CONNECT, OnBtnConnect)
	ON_BN_CLICKED(IDC_BTN_DNFILE, OnBtnDnfile)
	ON_BN_CLICKED(IDC_BTN_DNOS, OnBtnDnos)
	ON_BN_CLICKED(IDC_BTN_DNSTOP, OnBtnDnstop)
	ON_BN_CLICKED(IDC_BTN_UPFILE, OnBtnUpfile)
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCTransferDlg message handlers

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

	// 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
  m_ProgName.SetCurSel(0);    // Can only initialize after window created
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CMFCTransferDlg::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 CMFCTransferDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// **ADDED** 
// Code behind buttons
//
void CMFCTransferDlg::OnBtnConnect() 
{
  try{
  //******************
  // Connect to the Device on all interfaces
  UpdateData();

  m_Ver = CString("v") + m_Util->GetBstrVersion();  // Version

  m_Util->SetNBPS(m_BPS);               // Set BPS
  m_Util->SetNPort(m_Port);             // Set Port
  m_Util->SetBstrIP(m_IP);              // Set IP Address
  m_Util->SetNBus(m_BusType?1:0);       // Set Bus, checked(true) is ISA, else PCI
  m_Util->Connect(m_Transport,m_Cardnbr);//Connect to Transport
  m_Cardnbr = m_Util->GetNCard();       // One way to use USB is to grab the first unit, but then need to directly connect after that

  if(!(m_Util->GetIsOffline())){ // Verify Connection and start timer
    m_Status = _T("Connected  ") + m_Ver;
    m_IdTimer = SetTimer(1, 100, 0);
  }

  UpdateData(FALSE);

  // *** 
  // Error Handling should go everywhere a call to Communications Server is made 
  // In this sample it is only placed here to aid readablility.
  // ***
  // Error Handling - the errors specifically thrown by Communications Server
  // show up as COM exceptions and can be caught with the COleDispatchException class.
  }catch(COleDispatchException* e) {
    ostringstream errmsg;
    unsigned short errnbr = e->m_scError;
    errmsg << "ERROR NO: " << hex << e->m_scError << dec << "(" << errnbr << ")\nERROR DESC: " << string(e->m_strDescription) << "\nERROR INTERFACE: " << string(e->m_strSource);
    MessageBox(errmsg.str().c_str(), "COM Error", MB_OK);
	}
	catch(CException *e) { 
		TCHAR szCause[1024];
		e->GetErrorMessage(szCause, 1024);   
    MessageBox(szCause, "non-COM Error", MB_OK);
	}
}
 
void CMFCTransferDlg::OnBtnDnfile() 
{
  // Download a file to the controller
  UpdateData();
  m_Util->DownloadFile(m_Dest, m_Filename);
  m_LookAtStatus = true;
}

void CMFCTransferDlg::OnBtnDnos() 
{
  // Download an OS to the controller
  UpdateData();
  m_Util->DownloadOS(m_Product,m_Filename);
  m_LookAtStatus = true;
}

void CMFCTransferDlg::OnBtnDnstop() 
{
  // Stop the file or OS download
  m_Util->StopDownload();
  m_LookAtStatus = false;
}

void CMFCTransferDlg::OnBtnUpfile() 
{
  // Upload a file from the controller
  UpdateData();
  CString holdProg;
  m_ProgName.GetLBText(m_ProgName.GetCurSel(), holdProg);
  m_Util->UploadFile(holdProg, m_Filename);
	MessageBox("File Uploaded - NOTE: File uploads are blocking and are complete upon return from the upload call.", "Upload File", MB_OK);
}

void CMFCTransferDlg::OnBtnBrowse() 
{
  // Find a file name
  CFileDialog dlg(TRUE);
  if(dlg.DoModal()==IDOK){
    UpdateData();
    m_Filename = dlg.GetPathName();
    UpdateData(FALSE);
  }
	
}

void CMFCTransferDlg::OnTimer(UINT nIDEvent) 
{
  if(m_LookAtStatus){
    UpdateData();
    m_LookAtStatus = false; // Assume false, but make true again under some status
    long status = m_Util->GetStatusDL(&m_Total,&m_Bytes);
    switch(status){
      case 0:  
        m_Status = "No Transfer in progress.";
        break;
      case 1:
        m_LookAtStatus = true;
        m_Status = "Transfer in progress.";
        break;
      case 2:
        m_Status = "End of transfer.";
        break;
      case 3:
        m_Status = "User cancelled Transfer.";
        break;
      case 4:
        m_Status = "Error reading from file.";
        break;
      case 5:
        m_Status = "Too many errors during Transfer.";
        break;
      case 6:
        m_Status = "Transfer has timed out waiting for response.";
        break;
      case 7:
        m_Status = "The ACR OS failed to verify against the hardware description file (config image).";
        break;
      case 8:
        m_LookAtStatus = true;
        m_Status = "Save to flash of OS in progress.";
        break;
      case 9:
        m_Status = "Problem encountered in saving OS.";
        break;
      case 10:
        m_Status = "Problem encountered in saving OS, failed CRC check.";
        break;
      case 11:
        m_Status = "Problem encountered in saving OS, problem with flash.";
        break;
      default:
        char holdNbr[20];
        ltoa(status,holdNbr,10);
        m_Status = "Some Problem downloading file. Error: " + CString(holdNbr);
        break;
    }
    UpdateData(FALSE);
  }
	
	CDialog::OnTimer(nIDEvent);
}

void CMFCTransferDlg::OnOK() 
{
  if(m_IdTimer>-1)
    KillTimer(m_IdTimer);

	CDialog::OnOK();
}
// **ADDED** 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -