www.pudn.com > QQandSample.rar > RotationView.cpp
// RotationView.cpp : implementation of the CRotationView class
//
#include "stdafx.h"
#include "Rotation.h"
#include "Sample.h"
#include "RotationDoc.h"
#include "RotationView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRotationView
IMPLEMENT_DYNCREATE(CRotationView, CFormView)
BEGIN_MESSAGE_MAP(CRotationView, CFormView)
//{{AFX_MSG_MAP(CRotationView)
ON_WM_PAINT()
ON_COMMAND(ID_TIME_BEGAN, OnTimeBegan)
ON_COMMAND(ID_TIME_STOP, OnTimeStop)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_FFT_FFT, OnFftFft)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_MESSAGE(WM_SAMPLEFINISHED,OnSampleFinished)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//声明采集线程函数,和控制线程函数启停的全局变量samFlag
UINT SampleData(LPVOID pPrarm);//采集线程函数
extern BOOL samFlag; //控制采集函数的启停
CRotationView::CRotationView()
: CFormView(CRotationView::IDD)
{
//{{AFX_DATA_INIT(CRotationView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
//设定显示区域为700×560,图形区域为640×500,标志空间上下左右各为30
rect=CRect(0,0,700,560);
nType=0;
Flag=false;
}
CRotationView::~CRotationView()
{
}
void CRotationView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRotationView)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BOOL CRotationView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CRotationView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
//准备好画图的诸元
CDC *pDC=GetDC();
m_FrameDC.CreateCompatibleDC(pDC);
m_memDC.CreateCompatibleDC(pDC);
CBitmap Framemap,*pOldmap;
Framemap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
pOldmap=m_FrameDC.SelectObject(&Framemap);
CBitmap Clientmap;
Clientmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
pOldmap=m_memDC.SelectObject(&Clientmap);
m_memDC.FillSolidRect(0,0,rect.Width(),rect.Height(),RGB(239, 235,222));
// SetTimer(1,10,NULL);
}
/////////////////////////////////////////////////////////////////////////////
// CRotationView printing
BOOL CRotationView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CRotationView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CRotationView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CRotationView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CRotationView diagnostics
#ifdef _DEBUG
void CRotationView::AssertValid() const
{
CFormView::AssertValid();
}
void CRotationView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CRotationDoc* CRotationView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRotationDoc)));
return (CRotationDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRotationView message handlers
void CRotationView::OnPaint()
{
CRotationDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//刷新
m_memDC.SetBkMode(TRANSPARENT);
CPen pen(PS_SOLID,1,RGB(255,0,0));
CPen *pOldPen=m_memDC.SelectObject(&pen);
CBrush* pBrush = new CBrush(RGB(239, 235,222));
m_memDC.FillRect(rect, pBrush);
delete pBrush;
if(Flag)
{
switch(nType)
{
case 0:
{
m_draw.DrawWave(&m_memDC,&pDoc->m_signal);
}
break;
case 1:
{
m_draw.DrawFFT(&m_memDC,&pDoc->m_signal,true);
}
break;
case 2:
{
m_draw.DrawFFT(&m_memDC,&pDoc->m_signal,false);
}
break;
}
Flag=false;
}
CPaintDC dc(this); // device context for painting
dc.BitBlt(50,50,rect.Width(),rect.Height(),&m_memDC,0,0,SRCCOPY);
}
//命令池,发送绘图命令和参数设置命令
void CRotationView::Command(int nFolder, int index)
{
CRotationDoc* pDoc =GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_signal.hWnd=GetSafeHwnd();//得到程序安全的句柄
CWinThread* pThread=AfxBeginThread(SampleData,&pDoc->m_signal,
THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
switch(nFolder)
{
case 0:
{
if(index==0) //示波,但必须启动采集线程
{
samFlag=TRUE;
m_Sample.BeganSample(&pDoc->m_signal);
pThread->ResumeThread();
nType=0;
}
else if(index==1)//暂停示波
{
samFlag=false;
}
else if(index==2)// 采样对话框,启动采样线程,采集结束后关闭线程
{
if(m_Sample.DoModal()==IDOK)
{
samFlag=true;
m_Sample.BeganSample(&pDoc->m_signal);//初始化采样数组
pThread->ResumeThread();//启动采样命令
}
samFlag=false;
}
}
break;
case 1:
{
if(index==0)//画幅值谱
{
if(sample_Flag||pDoc->m_OpenFlag)
{
nType=1;
Flag=true;
}
else
{
AfxMessageBox("还没有采集数据");
}
}
if(index==1)//画相位谱
{
if(sample_Flag||pDoc->m_OpenFlag)
{
nType=2;
Flag=true;
}
else
{
AfxMessageBox("还没有采集数据");
}
}
Invalidate(false);
}
break;
case 2:
{
}
break;
case 3:
{
}
break;
}
}
void CRotationView::OnTimeBegan()
{
// TODO: Add your command handler code here
Command(0,0);
}
void CRotationView::OnTimeStop()
{
// TODO: Add your command handler code here
Command(0,1);
}
void CRotationView::OnFileOpen()
{
// TODO: Add your command handler code here
CRotationDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CFileDialog m_FileDlg(TRUE,NULL,"*.dat",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
"数据文件(*.dat)|*.dat||",NULL);
if(m_FileDlg.DoModal()!=IDOK)
return;
pDoc->m_pathname=m_FileDlg.GetPathName();
pDoc->m_filename=m_FileDlg.GetFileName();
CFile File;
if(!File.Open(pDoc->m_pathname,CFile::modeRead|CFile::typeBinary))
{
AfxMessageBox("不能打开文件");
}
else
{
CArchive ar(&File,CArchive::load);
pDoc->Serialize(ar);
ar.Flush();//清空文件数据
ar.Close();//关闭序列化
File.Close();//关闭文件
}
nType=0;
Flag=true;
Invalidate(false);
}
void CRotationView::OnFftFft()
{
// TODO: Add your command handler code here
Command(1,0);
}
void CRotationView::OnSampleFinished(WPARAM wParam, LPARAM lParam)
{
nType=0;
Flag=true;
Invalidate(false);
}