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

📄 controldlg.cpp

📁 此程序是关于步进电机的控制
💻 CPP
字号:
// ControlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Control.h"
#include "ControlDlg.h"
#include <windows.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()

/////////////////////////////////////////////////////////////////////////////
// CControlDlg dialog

CControlDlg::CControlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CControlDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CControlDlg)
	m_strBase = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//	m_flag=FALSE;
	m_front_flag=FALSE;
	m_back_flag=FALSE;
	i=0;
	j=0;
	reaped=0;
}

void CControlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CControlDlg)
	DDX_Text(pDX, IDC_EDIT1, m_strBase);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CControlDlg, CDialog)
	//{{AFX_MSG_MAP(CControlDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_INIT, OnButtonInit)
	ON_BN_CLICKED(IDC_BUTTON_FRONT, OnButtonFront)
	ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnButtonPause)
	ON_BN_CLICKED(IDC_BUTTON_BACK, OnButtonBack)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
	ON_BN_CLICKED(IDC_BUTTON_MANAGE, OnButtonManage)
	ON_WM_TIMER()
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST1, OnItemchangedList1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CControlDlg message handlers

BOOL CControlDlg::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
		HICON hIcon[8];
				int n;
				m_imageList.Create(16,16,0,8,8);
				hIcon[0]=AfxGetApp()->LoadIcon(IDI_ICON_WHITE);
				hIcon[1]=AfxGetApp()->LoadIcon(IDI_ICON_BLACK);
				hIcon[2]=AfxGetApp()->LoadIcon(IDI_ICON_RED);
				hIcon[3]=AfxGetApp()->LoadIcon(IDI_ICON_BLUE);
				hIcon[4]=AfxGetApp()->LoadIcon(IDI_ICON_YELLOW);
				hIcon[5]=AfxGetApp()->LoadIcon(IDI_ICON_CYAN);
				hIcon[6]=AfxGetApp()->LoadIcon(IDI_ICON_PURPLE);
				hIcon[7]=AfxGetApp()->LoadIcon(IDI_ICON_GREEN);
				for(n=0;n<8;n++) {
					m_imageList.Add(hIcon[n]);
				}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CControlDlg::OnButtonInit() 
{
	// TODO: Add your control notification handler code here
	HINSTANCE hinst = NULL;
	hinst = LoadLibrary("LabDrv.dll");
	if (hinst == NULL)
		AfxMessageBox("加载DLL失败!");
	else
	{
		FARPROC lpfn = GetProcAddress(hinst,"OpenPortTalk");
		if (lpfn == NULL)
			AfxMessageBox(" Open函数加载失败!");		
		else
			OpenPortTalk=(FOPEN) lpfn;
		lpfn = GetProcAddress(hinst,"ClosePortTalk");
		if (lpfn == NULL)
			AfxMessageBox(" Close函数加载失败!");		
		else
			ClosePortTalk=(FCLOSE) lpfn;
		lpfn = GetProcAddress(hinst,"outportd");
		if (lpfn == NULL)
			AfxMessageBox(" OutB函数加载失败!");		
		else
			outportd=(FOUTD) lpfn;
		lpfn = GetProcAddress(hinst,"inportd");
		if (lpfn == NULL)
			AfxMessageBox(" InB函数加载失败!");		
		else
		{
			inportd=(FIND) lpfn;
			if (OpenPortTalk( &Port_io_Base)!=1)
				AfxMessageBox("Not find io_Base!");
			else
			{
				//m_strBase.Format("%x", Port_io_Base);
				UpdateData(FALSE);
			}
		}
	}
	pProg=(CProgressCtrl *)GetDlgItem(IDC_PROGRESS_STEP);
	pProg->SetRange(0,7);
	pProg->SetPos(0);
}

void CControlDlg::OnButtonFront() 
{
	// TODO: Add your control notification handler code here
//	m_flag=TRUE;
	SetTimer(1,1000,NULL);
	m_front_flag=TRUE;
}


void CControlDlg::OnButtonPause() 
{
	// TODO: Add your control notification handler code here
	m_front_flag=FALSE;
	m_back_flag=FALSE;
	
}

void CControlDlg::OnButtonBack() 
{
	// TODO: Add your control notification handler code here
	SetTimer(2,2000,NULL);
	m_back_flag=TRUE;
}

void CControlDlg::OnButtonExit() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	KillTimer(2);
	KillTimer(3);
	OnOK();
}

void CControlDlg::OnButtonManage() 
{
	// TODO: Add your control notification handler code here
	CString str,path;
	char buffer[100];
	str="Text Files(*.txt)|*.txt|All Files(*.*)|*.*|";
	CFileDialog dlg(TRUE,".txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,str);
	GetCurrentDirectory(100,buffer);
	path=buffer;
	dlg.m_ofn.lpstrInitialDir=path;
	if(dlg.DoModal()==IDOK){
		m_strFileName=dlg.GetPathName();
		ShellExecute(NULL,"open","notepad.exe",m_strFileName,NULL,SW_SHOW);
	};
	
}

void CControlDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	char * str=new char;
	reaped=atoi(m_strBase);
	int n;

	int xu[8]={0x05,0x15,0x14,0x54,0x50,0x51,0x41,0x45};//相序表
	switch(nIDEvent){
	case 1:
		{
			if(m_front_flag==TRUE){
				outportd(Port_io_Base + 3,0x81);//初始化
		        outportd(Port_io_Base + 3,0x09);//置PC4=1关闭74LS373
				outportd(Port_io_Base,xu[i]);//
				outportd(Port_io_Base + 3,0x08);//置PC4=0,打开74LS373
				pProg->SetPos(i);
				i++;
				if(i==8) i=0;
				/*CListCtrl* pList=(CListCtrl* )GetDlgItem(IDC_LIST1);
				pList->SetImageList(&m_imageList,LVSIL_SMALL);
				for(n=1;n<=8;n++)
				{
					pList->InsertItem(1,NULL,1);
					pList->DeleteAllItems();
				}
				if(n==8) n=0;
				pList->DeleteItem(2);
				if(n=1) {
					pList->InsertItem(1,NULL,1);
					//pList->DeleteItem(1);
				}
				Sleep(50);
				for(n=0;n<8;n++)
				{
					pList->InsertItem(n,NULL,n);	
				}
				pList->SetBkColor(RGB(242,242,242));*/

				Sleep(reaped);
			}
		}break;
	case 2:
	{
		if(TRUE==m_back_flag){
		outportd(Port_io_Base + 3,0x81);//初始化
		outportd(Port_io_Base + 3,0x09);//置PC4=1关闭74LS373
	    outportd(Port_io_Base,xu[i]);//

⌨️ 快捷键说明

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