// sample1Dlg.cpp : implementation file // #include "stdafx.h" #include "sample1.h" #include "sample1Dlg.h" #include "aiousb.h" #include "detectdialog.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // 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() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg dialog CSample1Dlg::CSample1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CSample1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CSample1Dlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSample1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSample1Dlg) DDX_Control(pDX, IDC_RELAY3, m_relay3); DDX_Control(pDX, IDC_RELAY2, m_relay2); DDX_Control(pDX, IDC_RELAY1, m_relay1); DDX_Control(pDX, IDC_RELAY0, m_relay0); DDX_Control(pDX, IDC_INPUT3, m_input3); DDX_Control(pDX, IDC_INPUT2, m_input2); DDX_Control(pDX, IDC_INPUT1, m_input1); DDX_Control(pDX, IDC_INPUT0, m_input0); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSample1Dlg, CDialog) //{{AFX_MSG_MAP(CSample1Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_TIMER() ON_BN_CLICKED(IDC_RELAY3, OnRelay3) ON_BN_CLICKED(IDC_RELAY2, OnRelay2) ON_BN_CLICKED(IDC_RELAY1, OnRelay1) ON_BN_CLICKED(IDC_RELAY0, OnRelay0) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSample1Dlg message handlers BOOL CSample1Dlg::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 unsigned long pid, Status; DeviceIndex = diFirst; Status = QueryDeviceInfo(DeviceIndex, &pid, NULL, NULL, NULL, NULL); if ( (Status != 0) || ! ((pid == 0x8030) || (pid == 0x8031)) ) { DetectDialog box; if (box.DoModal() == IDOK) DeviceIndex = box.index; else exit(0); } outData = 0xff; DIO_WriteAll(DeviceIndex, &outData); relays[0] = &m_relay0; relays[1] = &m_relay1; relays[2] = &m_relay2; relays[3] = &m_relay3; inputs[0] = &m_input0; inputs[1] = &m_input1; inputs[2] = &m_input2; inputs[3] = &m_input3; high.LoadBitmap(IDB_I_HIGH); low.LoadBitmap(IDB_I_LOW); closed.LoadBitmap(IDB_R_CLOSED); open.LoadBitmap(IDB_R_OPEN); update = false; SetTimer(0, 100, NULL); return TRUE; // return TRUE unless you set the focus to a control } void CSample1Dlg::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 CSample1Dlg::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 CSample1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSample1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default unsigned inData; DIO_ReadAll(DeviceIndex, &inData); inData &= 0xff; for (int count = 0; count < 4; count++) { if (inData & (1 << count)) { inputs[count]->SetBitmap(low); } else { inputs[count]->SetBitmap(high); } } if (update == true) DIO_WriteAll(DeviceIndex, &outData); CDialog::OnTimer(nIDEvent); } void CSample1Dlg::OnRelay3() { // TODO: Add your control notification handler code here outData ^= 0x8; if (outData & 0x8) { relays[3]->SetBitmap(open); } else { relays[3]->SetBitmap(closed); } update = true; } void CSample1Dlg::OnRelay2() { // TODO: Add your control notification handler code here outData ^= 0x4; if (outData & 0x4) { relays[2]->SetBitmap(open); } else { relays[2]->SetBitmap(closed); } update = true; } void CSample1Dlg::OnRelay1() { // TODO: Add your control notification handler code here outData ^= 0x2; if (outData & 0x2) { relays[1]->SetBitmap(open); } else { relays[1]->SetBitmap(closed); } update = true; } void CSample1Dlg::OnRelay0() { // TODO: Add your control notification handler code here outData ^= 0x1; if (outData & 0x1) { relays[0]->SetBitmap(open); } else { relays[0]->SetBitmap(closed); } update = true; }