www.pudn.com > VC6.0.rar > Demo03Dlg.cpp, change:2004-03-17,size:5345b


// Demo03Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Demo03.h" 
#include "Demo03Dlg.h" 
# include	"dmc1000.h"		//包含库头文件 
const int IDC_TIMER = 0x01;	//定义定时器的一个ID值 
const int XCH = 0;			//今次只测试一个轴的效果,其它轴类似 
 
/******************************************** 
	雷赛DMC1000控制卡编程示例之三 
 
?-主要演示功能: 
	1. 动态改变速度 
 
!-关键函数: 
	速度变化函数 
	d1000_change_speed 
 
#-注意事项: 
	1. 动态变速注意S形和T形变化效果的不同 
	2. 动态变速有一定限定,请注意 
 
 
	Writen by LEISAI-SUPPORT 
	FAX: 0755 - 26402718 
	TEL: 0755 - 26434329 
	EMAIL: support@leisai.com 
	HTTP: www.leisai.com	 
********************************************/ 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo03Dlg dialog 
 
CDemo03Dlg::CDemo03Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CDemo03Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDemo03Dlg) 
	m_nSpeedST	= 0; 
	m_nStart	= 1600; 
	m_nSpeed	= 6400; 
	m_fAccel	= 0.1; 
	m_nCSpeed	= 0; 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CDemo03Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDemo03Dlg) 
	DDX_Radio(pDX, IDC_RADIO_SPEEDST, m_nSpeedST); 
	DDX_Text(pDX, IDC_EDIT_START, m_nStart); 
	DDX_Text(pDX, IDC_EDIT_SPEED, m_nSpeed); 
	DDX_Text(pDX, IDC_EDIT_ACCEL, m_fAccel); 
	DDX_Slider(pDX, IDC_SLIDER_SPEED, m_nCSpeed); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CDemo03Dlg, CDialog) 
	//{{AFX_MSG_MAP(CDemo03Dlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_TIMER() 
	ON_WM_DESTROY() 
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd) 
	ON_BN_CLICKED(IDC_BUTTON_DEC, OnButtonDec) 
	ON_WM_VSCROLL() 
	ON_BN_CLICKED(ID_STOP, OnStop) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDemo03Dlg message handlers 
 
BOOL CDemo03Dlg::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 
	 
	if( d1000_board_init() <= 0 )//控制卡初始化 
		MessageBox("未找到控制卡!","警告"); 
 
	UpdateControl(); 
 
	SetTimer( IDC_TIMER, 100, NULL ); 
	 
	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 CDemo03Dlg::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 CDemo03Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CDemo03Dlg::OnTimer(UINT nIDEvent)  
{ 
	static CString string; 
 
	string.Format( "位置:%ld",  
		long(d1000_get_command_pos( XCH ) ) ); 
 
	GetDlgItem( IDC_STATIC_POSITION )->SetWindowText( string ); 
 
	 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CDemo03Dlg::OnDestroy()  
{ 
	KillTimer( IDC_TIMER );	//在关闭控制卡之前先关闭定时器 
	d1000_board_close();	//以后不再提示 
	CDialog::OnDestroy(); 
	 
	 
} 
 
void CDemo03Dlg::OnCancel()  
{ 
		 
	CDialog::OnCancel(); 
} 
 
void CDemo03Dlg::OnOK()  
{//执行 
	if( !UpdateData( TRUE ) )  
		return; 
 
	m_nSpeedST == 0 ?//T形 
		d1000_start_tv_move( XCH, m_nStart, m_nSpeed, m_fAccel ): 
		d1000_start_sv_move( XCH, m_nStart, m_nSpeed, m_fAccel ); 
//	CDialog::OnOK(); 
} 
 
void CDemo03Dlg::UpdateControl() 
{ 
 
} 
 
void CDemo03Dlg::OnButtonAdd()  
{//增加10% 
	double fSpeed = d1000_get_speed( XCH )*1.1; 
	m_nSpeed = long(fSpeed); 
	if( m_nSpeed > 2000000 ) m_nSpeed = 2000000; 
 
	d1000_change_speed( XCH, m_nSpeed ); 
	UpdateData( FALSE ); 
 
} 
 
void CDemo03Dlg::OnButtonDec()  
{//减少10% 
	double fSpeed = d1000_get_speed( XCH )*0.9; 
	m_nSpeed = long(fSpeed); 
	if( m_nSpeed < 20 ) m_nSpeed = 20; 
 
	d1000_change_speed( XCH, m_nSpeed ); 
	UpdateData( FALSE ); 
	 
} 
 
void CDemo03Dlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)  
{ 
	int oldV = m_nCSpeed; 
	UpdateData( TRUE ); 
	if( oldV != m_nCSpeed ){//拉杆变速 
		oldV = (m_nCSpeed - oldV)*50; 
		double fSpeed = d1000_get_speed( XCH )+oldV; 
	 
		m_nSpeed = long(fSpeed); 
		if( m_nSpeed > 2000000 ) m_nSpeed = 2000000; 
		if( m_nSpeed < 20 ) m_nSpeed = 20; 
		d1000_change_speed( XCH, m_nSpeed ); 
		UpdateData( FALSE ); 
 
	} 
	CDialog::OnVScroll(nSBCode, nPos, pScrollBar); 
} 
 
void CDemo03Dlg::OnStop()  
{//停止 
	//当减速时间为0时,为急停效果 
	d1000_decel_stop( XCH ); 
	return; 
	 
	 
}