心跳检测View.cpp
资源名称:心跳检测.rar [点击查看]
上传用户:dtengfei
上传日期:2013-01-16
资源大小:393k
文件大小:16k
源码类别:
医药行业
开发平台:
Visual C++
- // 心跳检测View.cpp : implementation of the CMyView class
- //
- #include "stdafx.h"
- #include "心跳检测.h"
- #include "math.h"
- #include "心跳检测Doc.h"
- #include "心跳检测View.h"
- #include "Mainfrm.h"
- #include <windowsx.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyView
- IMPLEMENT_DYNCREATE(CMyView, CFormView)
- BEGIN_MESSAGE_MAP(CMyView, CFormView)
- //{{AFX_MSG_MAP(CMyView)
- ON_WM_PAINT()
- ON_BN_CLICKED(IDC_EXIT, OnExit)
- ON_BN_CLICKED(IDC_PRINT, OnMyPrint)
- ON_BN_CLICKED(IDC_OPEN, OnOpen)
- ON_BN_CLICKED(IDC_SAVE, OnSave)
- ON_BN_CLICKED(IDC_STOP, OnStop)
- ON_BN_CLICKED(IDC_PLAY, OnPlay)
- ON_BN_CLICKED(IDC_REFURBISH, OnRefurbish)
- ON_WM_CREATE()
- ON_BN_CLICKED(IDC_BITMAP, OnBitmap)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyView construction/destruction
- CMyView::CMyView()
- : CFormView(CMyView::IDD)
- {
- //{{AFX_DATA_INIT(CMyView)
- m_StartT = 0;
- m_EndT = 0;
- m_TopE = 0;
- m_BottomE = 0;
- m_E = _T("");
- m_F = _T("");
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- m_TopE=100;
- m_EndT=1;
- m_pMemDC=new CDC;
- //m_pBitmap=new CBitmap;
- }
- CMyView::~CMyView()
- {
- delete m_pMemDC;
- //delete m_pBitmap;
- }
- void CMyView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMyView)
- DDX_Text(pDX, IDC_START_T, m_StartT);
- DDV_MinMaxInt(pDX, m_StartT, 0, 29);
- DDX_Text(pDX, IDC_END_T, m_EndT);
- DDV_MinMaxInt(pDX, m_EndT, 1, 30);
- DDX_Text(pDX, IDC_TOP_E, m_TopE);
- DDX_Text(pDX, IDC_BOTTOM_E, m_BottomE);
- DDX_Text(pDX, IDC_E, m_E);
- DDX_Text(pDX, IDC_F, m_F);
- //}}AFX_DATA_MAP
- DDX_Control(pDX, IDC_PLAY, m_Play);
- DDX_Control(pDX, IDC_STOP, m_Stop);
- DDX_Control(pDX, IDC_TRANSACT, m_Transact);
- DDX_Control(pDX, IDC_SAVE, m_Save);
- DDX_Control(pDX, IDC_OPEN, m_Open);
- DDX_Control(pDX, IDC_PRINT, m_Print);
- DDX_Control(pDX, IDC_EXIT, m_Exit);
- DDX_Control(pDX, IDC_REFURBISH,m_Refurbish);
- }
- BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CFormView::PreCreateWindow(cs);
- }
- void CMyView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
- // GetParentFrame()->RecalcLayout();
- // ResizeParentToFit();
- m_Play.SetIcon(IDI_PLAY);
- m_Play.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Play.DrawFlatFocus(TRUE);
- // m_Play.SetTooltipText(_T("开始运行"));
- m_Stop.SetIcon(IDI_STOP);
- m_Stop.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Stop.DrawFlatFocus(TRUE);
- m_Transact.SetIcon(IDI_TRANSACT);
- m_Transact.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Transact.DrawFlatFocus(TRUE);
- m_Save.SetIcon(IDI_SAVE);
- m_Save.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Save.DrawFlatFocus(TRUE);
- m_Open.SetIcon(IDI_OPEN);
- m_Open.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Open.DrawFlatFocus(TRUE);
- m_Print.SetIcon(IDI_PRINT);
- m_Print.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Print.DrawFlatFocus(TRUE);
- m_Exit.SetIcon(IDI_EXIT1,IDI_EXIT);
- m_Exit.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Exit.DrawFlatFocus(TRUE);
- m_Refurbish.SetIcon(IDI_REFURBISH1,IDI_REFURBISH);
- m_Refurbish.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 50);
- m_Refurbish.DrawFlatFocus(TRUE);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyView printing
- BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- void CMyView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
- {
- // TODO: add customized printing code here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyView diagnostics
- #ifdef _DEBUG
- void CMyView::AssertValid() const
- {
- CFormView::AssertValid();
- }
- void CMyView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
- CMyDoc* CMyView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
- return (CMyDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMyView message handlers
- void CMyView::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: Add your message handler code here
- CBitmap* pOldBitmap=m_pMemDC->SelectObject(&m_pBitmap);
- //copy the memory DC to show DC
- dc.BitBlt(170,50,m_MaxX-150,m_MaxY-50,m_pMemDC,0,0,SRCCOPY);
- m_pMemDC->SelectObject(pOldBitmap);
- // DrawCordinate(&dc);
- // DrawLine(&dc);
- // Do not call CFormView::OnPaint() for painting messages
- }
- void CMyView::OnExit()
- {
- // TODO: Add your control notification handler code here
- PostMessage(WM_QUIT);
- }
- void CMyView::OnMyPrint()
- {
- // TODO: Add your control notification handler code here
- ShellExecute(GetSafeHwnd(),"open","C:\Program Files\office\Office\Winword.exe",NULL,NULL,SW_SHOWNORMAL);
- }
- void CMyView::OnOpen()
- {
- // TODO: Add your control notification handler code here
- CFileDialog dlg(TRUE,"dot",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
- "心跳检测数据(*.dot)|*.dot|所有文件(*.*)|*.*||",this);
- if(dlg.DoModal()==IDCANCEL)
- return;
- PathName=dlg.GetPathName();
- CStdioFile file;
- if(file.Open(PathName,CFile::modeRead)==0)
- {
- MessageBox("打文件"+PathName+"失败!","提示",MB_OK+MB_ICONWARNING);
- return;
- }
- //save the data
- Openfile(&file);
- //close the file
- file.Close();
- }
- void CMyView::OnSave()
- {
- // TODO: Add your control notification handler code here
- CFileDialog dlg(FALSE,"dot","未命名",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
- "心跳检测数据(*.dot)|*.dot|所有文件(*.*)|*.*||",this);
- if(dlg.DoModal()==IDCANCEL)
- return;
- PathName=dlg.GetPathName();
- CStdioFile file;
- if(file.Open(PathName,CFile::modeCreate|CFile::modeWrite)==0)
- {
- MessageBox("打文件"+PathName+"失败!","提示",MB_OK+MB_ICONWARNING);
- return;
- }
- //save the data
- SaveFile(&file);
- //close the file
- }
- void CMyView::OnStop()
- {
- // TODO: Add your control notification handler code here
- CMainFrame *m_pFrm=(CMainFrame *)AfxGetMainWnd();
- m_pFrm->TP.runing=FALSE;
- }
- void CMyView::OnPlay()
- {
- // TODO: Add your control notification handler code here
- CMainFrame *m_pFrm=(CMainFrame *)AfxGetMainWnd();
- OnStop();
- Sleep(100);
- m_pFrm->TP.PointNumber=0;
- /* for(int i=0;i<=m_pFrm->TP.WholeNumber;i++)
- {
- m_pFrm->TP.data[i]=0;
- m_pFrm->TP.data1[i]=0;
- }
- */
- /* for(int i=0;i<100;i++)
- {
- m_pFrm->TP.data[i]=fsimpf(i);
- m_pFrm->TP.data1[i]=fsimp(0,i,0.00001);
- }
- */
- m_pFrm->TP.runing=TRUE;
- ::PostThreadMessage(m_pFrm->m_JiqiThreadID,WM_CELIANG,long(&m_pFrm->TP),NULL);
- }
- void CMyView::OnRefurbish()
- {
- // TODO: Add your control notification handler code here
- }
- void CMyView::Draw(int Point)
- {
- // Invalidate(FALSE);
- CMainFrame *m_pFrm=(CMainFrame *)AfxGetMainWnd();
- m_E.Format("%3.3f",m_pFrm->TP.data[Point]);
- m_F.Format("%3.3f",float(Point));
- UpdateData(FALSE);
- DrawCordinate();
- DrawLine();
- }
- int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFormView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- InitData();
- CDC* pDC=GetDC();
- m_MaxX=GetSystemMetrics(SM_CXSCREEN);
- m_MaxY=GetSystemMetrics(SM_CYSCREEN);
- m_pMemDC->CreateCompatibleDC(pDC);
- m_pBitmap.CreateCompatibleBitmap(pDC,m_MaxX,m_MaxY);
- ReleaseDC(pDC);
- DrawCordinate();
- return 0;
- }
- void CMyView::DrawCordinate()
- {
- CBitmap* pOldBitmap=m_pMemDC->SelectObject(&m_pBitmap);
- CString str;
- //建立字体
- CFont fontBT;
- m_FontView.lfHeight=20;
- m_FontView.lfEscapement=0;
- m_FontView.lfOrientation=0;
- fontBT.CreateFontIndirect(&m_FontView);
- CFont fontXiao;
- m_FontView.lfHeight=15;
- fontXiao.CreateFontIndirect(&m_FontView);
- CFont* pOldFont=m_pMemDC->SelectObject(&fontBT);
- m_pMemDC->SetTextColor(0);
- m_pMemDC->SetBkMode(TRANSPARENT);
- //填充背景
- CBrush *brush;
- brush=new CBrush(RGB(255,255,255));
- CBrush *pOldBrush=m_pMemDC->SelectObject(brush);
- m_pMemDC->FillRect (CRect(LeftRange,TopRange,m_MaxX,m_MaxY),brush);
- //装饰条
- CPen pen(PS_SOLID,2, RGB(0,0,0));
- CPen *pOldPen=m_pMemDC->SelectObject(&pen);
- m_pMemDC->MoveTo(LeftRange, 0);
- m_pMemDC->LineTo(LeftRange, 768);
- m_pMemDC->MoveTo(LeftRange, TopRange);
- m_pMemDC->LineTo(1024, TopRange);
- CPen pen1(PS_DOT, 1, RGB(150,150,150));
- CPen pen2(PS_SOLID, 1, RGB(0,0,0));
- //画刻度
- CRect rect(left,top,right,bottom);
- m_pMemDC->Rectangle(rect);
- int i;
- //画下坐标
- double Xinter,Yinter;
- if(m_EndT-m_StartT==1)
- {
- Xinter=(right-left)/100.0;
- m_pMemDC->SelectObject(&pen1);
- for(i=1;i<100;i++)
- {
- m_pMemDC->MoveTo(int(left+i*Xinter),top);
- m_pMemDC->LineTo(int(left+i*Xinter),bottom);
- }
- m_pMemDC->SelectObject(&pen2);
- for(i=1;i<10;i++)
- {
- m_pMemDC->MoveTo(int(left+i*Xinter*10),top);
- m_pMemDC->LineTo(int(left+i*Xinter*10),bottom);
- }
- m_pMemDC->SelectObject(&fontXiao);
- for(i=0;i<=10;i++)
- {
- str.Format("%i",i*100);
- m_pMemDC->TextOut(int(left+i*Xinter*10-8),bottom+5,str);
- }
- }
- //画左坐标
- Yinter=(bottom-top)/100.0;
- m_pMemDC->SelectObject(&pen1);
- for(i=1;i<100;i++)
- {
- if(i/10.0==int(i/10.0))
- continue;
- m_pMemDC->MoveTo(left,int(top+i*Yinter));
- m_pMemDC->LineTo(right,int(top+i*Yinter));
- }
- m_pMemDC->SelectObject(&pen2);
- for(i=1;i<10;i++)
- {
- m_pMemDC->MoveTo(left,int(top+i*Yinter*10));
- m_pMemDC->LineTo(right,int(top+i*Yinter*10));
- }
- for(i=1;i<=10;i++)
- {
- str.Format("%i",i*10);
- m_pMemDC->TextOut(left-25,int(bottom-i*Yinter*10-8),str);
- }
- m_pMemDC->SelectObject(pOldBrush);
- m_pMemDC->SelectObject(pOldFont);
- m_pMemDC->SelectObject(pOldPen);
- m_pMemDC->SelectObject(pOldBitmap);
- }
- void CMyView::InitData()
- {
- m_FontView.lfHeight=15;
- m_FontView.lfWidth=0;
- m_FontView.lfEscapement=0;
- m_FontView.lfOrientation=0;
- m_FontView.lfWeight=FW_NORMAL;
- m_FontView.lfItalic=FALSE;
- m_FontView.lfUnderline=FALSE;
- m_FontView.lfStrikeOut=FALSE;
- m_FontView.lfCharSet=GB2312_CHARSET;
- m_FontView.lfOutPrecision=OUT_STROKE_PRECIS;
- m_FontView.lfClipPrecision=CLIP_STROKE_PRECIS;
- m_FontView.lfQuality=DRAFT_QUALITY;
- m_FontView.lfPitchAndFamily=VARIABLE_PITCH|FF_MODERN;
- strcpy(m_FontView.lfFaceName,"System");
- TopRange=0;
- LeftRange=0;
- left=LeftRange+40;
- right=820;
- top=TopRange+20;
- bottom=630;
- }
- void CMyView::DrawLine()
- {
- CBitmap* pOldBitmap=m_pMemDC->SelectObject(&m_pBitmap);
- CMainFrame *m_pFrm=(CMainFrame *)AfxGetMainWnd();
- CPen pen1(PS_SOLID, 1, RGB(255,0,0));
- CPen *pOldPen = m_pMemDC->SelectObject(&pen1);
- double Xinter,Yinter;
- Xinter=(right-left)/float(m_pFrm->TP.WholeNumber);
- Yinter=(bottom-top)/100.0;
- /*
- for(int i=1;i<=m_pFrm->TP.PointNumber;i++)
- {
- int e1=int(bottom-m_pFrm->TP.data[i-1]*Yinter);
- int e2=int(bottom-m_pFrm->TP.data[i]*Yinter);
- if(e1<top) e1=top;
- if(e1>bottom) e1=bottom;
- if(e2<top) e2=top;
- if(e2>bottom) e2=bottom;
- m_pMemDC->MoveTo(int(left+(i-1)*Xinter),e1);
- m_pMemDC->LineTo(int(left+i*Xinter),e2);
- }
- */
- for(int i=1;i<m_pFrm->TP.PointNumber;i++)
- {
- int e1=int(bottom-m_pFrm->TP.data[i-1]*Yinter);
- int e2=int(bottom-m_pFrm->TP.data[i]*Yinter);
- int e3=int(bottom-m_pFrm->TP.data1[i-1]*Yinter);
- int e4=int(bottom-m_pFrm->TP.data1[i]*Yinter);
- m_pMemDC->MoveTo(int(left+(i-1)*Xinter),e1);
- m_pMemDC->LineTo(int(left+i*Xinter),e2);
- m_pMemDC->MoveTo(int(left+(i-1)*Xinter),e3);
- m_pMemDC->LineTo(int(left+i*Xinter),e4);
- }
- m_pMemDC->SelectObject(pOldPen);
- m_pMemDC->SelectObject(pOldBitmap);
- Invalidate(FALSE);
- }
- void CMyView::SaveFile(CStdioFile *file)
- {
- CMainFrame *m_pFrm=(CMainFrame *)AfxGetMainWnd();
- CString mStr;
- for(int i=0;i<m_pFrm->TP.PointNumber;i++)
- {
- if(m_pFrm->TP.data[i]<0)
- break;
- }
- mStr.Format("采集点共:%dn",i);
- file->WriteString(mStr);
- for(int j=0;j<i;j++)
- {
- mStr.Format("采集点%d:%3.4fn",j,m_pFrm->TP.data[j]);
- file->WriteString(mStr);
- }
- }
- void CMyView::Openfile(CStdioFile *file)
- {
- CMainFrame *m_pFrm=(CMainFrame *)AfxGetMainWnd();
- CString mStr;
- int j;
- CEdit* pEdit;
- CString str;
- file->ReadString(mStr);
- j=mStr.Find(":",0)+1;
- mStr=mStr.Right(mStr.GetLength()-j);
- pEdit=(CEdit *)GetDlgItem(IDC_F);
- pEdit->SetSel(0,-1);
- pEdit->ReplaceSel(mStr);
- m_pFrm->TP.PointNumber=atoi(mStr)-1;
- str=mStr;
- int i;
- for(i=0;i<atoi(str);i++)
- {
- file->ReadString(mStr);
- j=mStr.Find(":",0)+1;
- mStr=mStr.Right(mStr.GetLength()-j);
- m_pFrm->TP.data[i]=atof(mStr);
- }
- DrawCordinate();
- DrawLine();
- /* file->Seek(-15,CFile::end);
- file->ReadString(mStr);
- j=mStr.Find(":",0)+1;
- mStr=mStr.Right(mStr.GetLength()-j);
- pEdit=(CEdit *)GetDlgItem(IDC_E);
- pEdit->SetSel(0,-1);
- pEdit->ReplaceSel(mStr);
- */
- }
- void CMyView::OnBitmap()
- {
- // TODO: Add your control notification handler code here
- CClientDC dc(this);
- BITMAP btm;
- m_pBitmap.GetBitmap(&btm);
- DWORD size=btm.bmWidthBytes*btm.bmHeight;//bmWidthBytes每条扫描线字节数
- LPSTR lpData=(LPSTR)GlobalAllocPtr(GPTR,size);
- /////////////////////////////////////////////
- BITMAPINFOHEADER bih;
- bih.biBitCount=btm.bmBitsPixel;//每个象素的位数0代表JPEG格式或被指定
- bih.biClrImportant=0;//显示位图时所需要的颜色数,若为0则需要所有的颜色数
- bih.biClrUsed=0;//指定颜色表中位图所用道的颜色索引数若为0则取最大值
- bih.biCompression=0;//压缩格式0指的是未压缩
- bih.biHeight=btm.bmHeight;//位图高
- bih.biPlanes=1;//必须为一
- bih.biSize=sizeof(BITMAPINFOHEADER);//这种结构的大小
- bih.biSizeImage=size;//位图大小的字节数
- bih.biWidth=btm.bmWidth;//位图宽
- bih.biXPelsPerMeter=0;//X方向每米所规定的象素方式
- bih.biYPelsPerMeter=0;//
- ///////////////////////////////////
- GetDIBits(dc,m_pBitmap,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
- /////////////////////////////////////////////
- BITMAPFILEHEADER bfh;
- bfh.bfReserved1=bfh.bfReserved2=0;
- bfh.bfType=((WORD)('M'<< 8)|'B');
- bfh.bfSize=54+size;
- bfh.bfOffBits=54;
- /////////////////////////////////////////////
- CFile bf;
- CFileDialog dlg(FALSE,"txt","未命名",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
- "心跳检测数据(*.bmp)|*.bmp|所有文件(*.*)|*.*||",this);
- if(dlg.DoModal()==IDCANCEL)
- return;
- CString name;
- name=dlg.GetPathName();
- if(bf.Open(name,CFile::modeCreate|CFile::modeWrite))
- {
- bf.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
- bf.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
- bf.WriteHuge(lpData,size);
- bf.Close();
- }
- GlobalFreePtr(lpData);
- }
- double CMyView::fsimpf(double x)
- {
- double y;
- // y=log(1.0+x)/(1.0+x*x);
- // y=sin(x/50.0);
- y=sin(x/10);
- return(y);
- }
- double CMyView::fsimp(double a, double b, double eps)
- {
- int n,k;
- double h,t1,t2,s1,s2,ep,p,x;
- n=1; h=b-a;
- t1=h*(fsimpf(a)+fsimpf(b))/2.0; /*用梯形公式求出一个大概的估值*/
- s1=t1;
- ep=eps+1.0;
- while (ep>=eps)
- {
- /*用梯形法则计算*/
- p=0.0;
- for (k=0;k<=n-1;k++)
- {
- x=a+(k+0.5)*h;
- p=p+fsimpf(x);
- }
- t2=(t1+h*p)/2.0;
- /*用辛普森公式求精*/
- s2=(4.0*t2-t1)/3.0;
- ep=fabs(s2-s1);
- t1=t2; s1=s2; n=n+n; h=h/2.0;
- }
- return(s2);
- }