www.pudn.com > dv_2_26.zip > DVView.cpp


// DVView.cpp : implementation of the CDVView class 
// 
#include "stdafx.h" 
#include  
 
#include "DV.h" 
 
#include "DVDoc.h" 
#include "DVView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define  PORT0 0x300 
#define  PORT1 0x301 
#define  PORT2 0x302 
#define  PORT3 0x303 
#define  PORT4 0x304 
#define  PORT5 0x305 
#define  PORT6 0x306 
#define	 PORT7 0x307 
 
static int nPerScr = 650; 
static int nIndexScr = 0; 
static int nPerFl = 5000; 
static WORD CurData[650]; 
 
///////////////////////////////////////////////////////////////////////////// 
// CDVView 
 
IMPLEMENT_DYNCREATE(CDVView, CFormView) 
 
BEGIN_MESSAGE_MAP(CDVView, CFormView) 
	//{{AFX_MSG_MAP(CDVView) 
	ON_BN_CLICKED(IDC_START_CAPTURE, OnStartCapture) 
	ON_BN_CLICKED(IDC_VIEW_DATA, OnViewData) 
	ON_BN_CLICKED(ID_QUIT, OnQuit) 
	ON_WM_PAINT() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_RBUTTONDOWN() 
	ON_EN_CHANGE(IDC_DELAY_MS, OnChangeDelayMs) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDVView construction/destruction 
 
CDVView::CDVView() 
	: CFormView(CDVView::IDD) 
{ 
	//{{AFX_DATA_INIT(CDVView) 
	m_DelayMs = 0; 
	//}}AFX_DATA_INIT 
	// TODO: add construction code here 
//	 bLRCursor = TRUE;//right cursor is selected 
	 nLeft = nRight = 0; 
} 
 
CDVView::~CDVView() 
{ 
} 
 
void CDVView::DoDataExchange(CDataExchange* pDX) 
{ 
	CFormView::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDVView) 
	DDX_Text(pDX, IDC_DELAY_MS, m_DelayMs); 
	DDV_MinMaxUInt(pDX, m_DelayMs, 0, 20); 
	//}}AFX_DATA_MAP 
} 
 
BOOL CDVView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CFormView::PreCreateWindow(cs); 
} 
 
void CDVView::OnInitialUpdate() 
{ 
	CFormView::OnInitialUpdate(); 
	GetParentFrame()->RecalcLayout(); 
	ResizeParentToFit(0); 
 
	GetDlgItem(IDC_PICTURE)->GetWindowRect(&rectPic); 
	ScreenToClient(&rectPic); 
 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CDVView diagnostics 
 
#ifdef _DEBUG 
void CDVView::AssertValid() const 
{ 
	CFormView::AssertValid(); 
} 
 
void CDVView::Dump(CDumpContext& dc) const 
{ 
 
	CFormView::Dump(dc); 
} 
 
CDVDoc* CDVView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDVDoc))); 
	return (CDVDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CDVView message handlers 
 
void CDVView::OnStartCapture()  
{ 
	memset(CurData,0,nPerScr*2); 
	CClientDC dc(this); 
 
	CString strWrite,strTemp; 
	strWrite.Format("序号\t\tN1\t\tN2\t\t(N1+N2)/2\n\n"); 
 
	MSG MyMsg; 
	BOOL test; 
	BOOL TAG = TRUE; 
 
	WORD w_hbyte,w_lbyte;	 
	WORD d1,d2,ave; 
	WORD old_ave = 0; 
 
//	int i = 0; 
//	int ii = 0; 
	int nFileSeq = 0; 
 
	while(TAG) 
	{ 
		::PeekMessage( &MyMsg, NULL, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE ); 
 
		if(MyMsg.wParam == 27){TAG = FALSE;SaveData(strWrite,nIndexScr);} 
 
		_inp(PORT0); 
 
		Sleep(m_DelayMs); 
//		Sleep(2); 
 
		do{ 
			test=_inp(PORT1)&0x1; 
			::PeekMessage( &MyMsg, NULL, WM_KEYDOWN, WM_KEYDOWN, PM_REMOVE ); 
			if(MyMsg.wParam == 27)	test = TRUE; 
		}while(!test); 
 
		w_hbyte = _inp(PORT4) & 0x07; 
		w_lbyte = _inp(PORT3); 
		d1 = (w_hbyte<<8) + w_lbyte; 
		strTemp.Format("%d\t\t%d\t\t",nIndexScr,d1); 
		strWrite += strTemp; 
 
		w_hbyte = _inp(PORT7) & 0x07; 
		w_lbyte = _inp(PORT6); 
		d2 = (w_hbyte<<8) + w_lbyte; 
		strTemp.Format("%d\t\t",d2); 
		strWrite += strTemp; 
 
		ave = (d1 + d2)/2; 
		strTemp.Format("%d\n",ave); 
		strWrite += strTemp; 
 
		CurData[nIndexScr] = ave; 
//		ave = rand()%200; 
		//调用自定义的追加点函数 
		AppendLine(nIndexScr-1,old_ave,nIndexScr,ave,&dc); 
		old_ave = ave; 
 
		nIndexScr++; 
		if(nIndexScr >= nPerScr) 
		{ 
			//调用自定义的保存数据函数 
			nIndexScr=0; 
			strWrite.Format("序号\t\tN1\t\tN2\t\t(N1+N2)/2\n\n"); 
//			SaveData(strWrite,nIndexScr); 
			 
			 
//			memset(CurData,0,nPerScr*2); 
 
			CRect rectInvalid; 
			GetWindowRect(&rectInvalid); 
			ScreenToClient(&rectInvalid); 
			InvalidateRect(rectInvalid,1); 
			UpdateWindow(); 
		}		 
	} 
} 
 
BOOL CDVView::AppendLine(int x0,int y0,int x1,int y1,CDC *pdc) 
{ 
	CPen *pOldPen,pen(PS_SOLID,1,RGB(0,255,0)); 
	pOldPen=pdc->SelectObject(&pen); 
 
	int n_step=1; 
	float fScale = 0.10; 
 
	int temp = (int)((float)y0*fScale); 
	temp = (temp>rectPic.Height())?(rectPic.Height()):temp; 
	pdc->MoveTo((rectPic.left+x0*n_step),(rectPic.bottom-temp)); 
 
	temp = (int)((float)y1*fScale); 
	temp = (temp>rectPic.Height())?(rectPic.Height()):temp; 
	pdc->LineTo((rectPic.left+x1*n_step),(rectPic.bottom-temp)); 
 
	pdc->SelectObject(pOldPen); 
	return TRUE; 
} 
 
BOOL CDVView::SaveData(CString &str,int nSeq) 
{ 
	CStdioFile file; 
	CString strTemp; 
//	strTemp.Format("data%d.txt",nSeq); 
	BOOL bSuccess = file.Open( "data.txt" , CFile::modeWrite | CFile::typeText | CFile::modeCreate); 
	if(bSuccess) 
	{ 
		file.WriteString(str); 
		file.Close(); 
		return TRUE; 
	} 
	else 
	{ 
		AfxMessageBox("写数据文件DATA??.TXT失败!"); 
		return FALSE; 
	} 
	return FALSE; 
} 
 
 
 
void CDVView::OnViewData()  
{ 
	HWND hwnd = GetSafeHwnd(); 
	HINSTANCE result = ShellExecute(hwnd, "open", "data.txt", NULL,NULL, SW_SHOW);	 
} 
 
BOOL CDVView::BkGnd(CDC *pdc) 
{   //interX:4.6ms 
	pdc->SetBkMode(TRANSPARENT); 
	pdc->SelectStockObject(BLACK_BRUSH); 
	pdc->Rectangle(rectPic); 
	CPen *pOldPen,pen(PS_SOLID,1,RGB(129,129,129)); 
	pOldPen = pdc->SelectObject(&pen); 
 
	int interY = rectPic.Height()/10; 
 
	CString str; 
	 
	for(int j=0;j<=10;j++) 
	{ 
		pdc->MoveTo(rectPic.left,rectPic.bottom-interY*j); 
		pdc->LineTo(rectPic.right,rectPic.bottom-interY*j); 
		 
		str.Format("%d",j*interY*10); 
		pdc->TextOut(rectPic.left-40,rectPic.bottom-interY*j-10,str); 
	} 
 
	for(j=0;j<=10;j++) 
	{ 
		pdc->MoveTo(rectPic.left+j*65,rectPic.bottom); 
		pdc->LineTo(rectPic.left+j*65,rectPic.top); 
		pdc->SetBkMode(TRANSPARENT); 
		str.Format("%d",(int)((float)j*65.0*4.6)); 
		pdc->TextOut(rectPic.left+j*65-20,rectPic.bottom,str); 
	} 
 
	pdc->SetBkMode(OPAQUE); 
	CPen penCursor(PS_SOLID,1,RGB(255,255,255)); 
	pOldPen = pdc->SelectObject(&penCursor); 
 
 
	pdc->MoveTo(rectPic.left+nLeft,rectPic.bottom); 
	pdc->LineTo(rectPic.left+nLeft,rectPic.top); 
	pdc->TextOut(rectPic.left+nLeft,rectPic.top,"左"); 
 
	pdc->MoveTo(rectPic.left+nRight,rectPic.bottom); 
	pdc->LineTo(rectPic.left+nRight,rectPic.top); 
	pdc->TextOut(rectPic.left+nRight,rectPic.top,"右"); 
 
	float ll=(float)nLeft*4.6; 
	float rr=(float)nRight*4.6; 
	float dd=rr-ll; 
	float hh; 
	hh=(dd==0)?0:(float)(1.0/abs(dd)*1000.0); 
 
 
	str.Format("左标线位置:%.2fms,该点振幅:%d\n右标线位置:%.2fms,该点振幅:%d\n两标线间隔:%.2fms合%.2fHz",ll,CurData[nLeft],rr,CurData[nRight],dd,hh); 
	SetDlgItemText(IDC_CURSOR_RESULT,str); 
	 
 
	pdc->SelectObject(pOldPen); 
 
 
	return TRUE; 
} 
 
void CDVView::OnQuit()  
{ 
	PostQuitMessage(1); 
} 
 
void CDVView::OnPaint()  
{ 
	CPaintDC dc(this); // device context for painting 
	 
	BkGnd(&dc); 
	PlayBackCurData(&dc); 
	 
	// Do not call CFormView::OnPaint() for painting messages 
} 
 
void CDVView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	int x,y; 
	x = point.x-rectPic.left; 
	y = rectPic.bottom-point.y; 
	if(x>=0 && y>=0) nRight = x; 
	InvalidateRect(rectPic,0); 
	UpdateWindow(); 
	 
	CFormView::OnRButtonDown(nFlags, point); 
} 
 
 
void CDVView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	/* 
	CClientDC dc(this); 
//	dc.SetBkMode(TRANSPARENT); 
	CPen *pOldPen,pen(PS_SOLID,1,RGB(0,0,255)); 
	pOldPen=dc.SelectObject(&pen);*/ 
 
	int x,y; 
	x = point.x-rectPic.left; 
	y = rectPic.bottom-point.y; 
	if(x>=0 && y>=0) nLeft = x; 
	InvalidateRect(rectPic,0); 
	UpdateWindow(); 
 
/* 
	dc.MoveTo(rectPic.left+nLeft,rectPic.bottom); 
	dc.LineTo(rectPic.left+nLeft,rectPic.top); 
	dc.TextOut(rectPic.left+nLeft,rectPic.top,"左"); 
 
	dc.MoveTo(rectPic.left+nRight,rectPic.bottom); 
	dc.LineTo(rectPic.left+nRight,rectPic.top); 
	dc.TextOut(rectPic.left+nRight,rectPic.top,"右"); 
 
	dc.SelectObject(pOldPen); 
	*/ 
	CFormView::OnLButtonDown(nFlags, point); 
} 
 
BOOL CDVView::PlayBackCurData(CDC *pdc) 
{ 
	CPen *pOldPen,pen(PS_SOLID,1,RGB(0,255,0)); 
	pOldPen=pdc->SelectObject(&pen); 
 
	int n_step=1; 
	float fScale = 0.10; 
 
	for(int i=0;irectPic.Height())?(rectPic.Height()):temp; 
		pdc->MoveTo((rectPic.left+i*n_step),(rectPic.bottom-temp)); 
 
		temp = (int)((float)CurData[i+1]*fScale); 
		temp = (temp>rectPic.Height())?(rectPic.Height()):temp; 
		pdc->LineTo((rectPic.left+(i+1)*n_step),(rectPic.bottom-temp)); 
	} 
 
	pdc->SelectObject(pOldPen); 
	return TRUE; 
} 
 
 
void CDVView::OnChangeDelayMs()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CFormView::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
 
	UpdateData(TRUE); 
	 
	 
}