📄 demodlg.cpp
字号:
// DemoDlg.cpp : implementation file
//
#include "stdlib.h"
#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"
#include <conio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
void Cardinitialize();
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()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog
CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDemoDlg)
m_DelayTime = 10;
m_Samples =200;
m_Valve2 = 50.0f;
m_Valve1 = 0.0f;
m_isDrawResult = FALSE;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_bottomoff=25;
m_leftoff=25;
m_rightoff=5;
m_topoff=5;
m_disy=20;//刻度间距(象素)
m_disx=40;
m_PressureChartMax=99;//0--6
m_AngleChartMax=600.0;
int i;//
for(i=0;i<60000;i++){
m_Angle[i]=0;
m_p1[i]=50;
m_p2[i]=0.5;
m_Velocity[i]=0;
}
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDemoDlg)
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Text(pDX, IDC_EDIT_DelayTime, m_DelayTime);
DDV_MinMaxInt(pDX, m_DelayTime, 0, 500);
DDX_Text(pDX, IDC_EDIT_Samples, m_Samples);
DDV_MinMaxLong(pDX, m_Samples, 0, 60000);
DDX_Text(pDX, IDC_EDIT_VALVE2, m_Valve2);
DDX_Text(pDX, IDC_EDIT_VALVE1, m_Valve1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
//{{AFX_MSG_MAP(CDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_RUN, OnButtonRun)
ON_BN_CLICKED(IDC_DRAW, OnDraw)
ON_BN_CLICKED(IDC_SaveToFile, OnSaveToFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers
BOOL CDemoDlg::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
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
LV_COLUMN h;
h.mask=LVCF_FMT|LVCF_TEXT|LVCF_WIDTH;
h.fmt=LVCFMT_CENTER;
h.cx=80;
h.pszText="次数";
m_list.InsertColumn(0,&h);
h.pszText="温度";
m_list.InsertColumn(1,&h);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDemoDlg::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 CDemoDlg::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();
// if(m_isDrawResult)
// {
CWnd* pWnd;
CDC* pDC;
pWnd=(CWnd*)GetDlgItem(IDC_RESULT);
pDC=pWnd->GetDC();
DrawResult(pDC);
// pWnd=(CWnd*)GetDlgItem(IDC_ANGLE_CHART);
// pDC=pWnd->GetDC();
// DrawAngle(pDC);
// }
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDemoDlg::OnButtonRun()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_list.SetRedraw(FALSE);//将control list 表清空
m_list.GetItemCount();
m_list.DeleteAllItems();
m_list.SetRedraw(TRUE);
int n;
LARGE_INTEGER litmp;
LONGLONG QPart1,QPart2;
double dfMinus, dfFreq, dfTim;
QueryPerformanceFrequency(&litmp);
dfFreq=(double)litmp.QuadPart;
int us;//Seconds
float T;
us=m_DelayTime*1000-1;
T=(float)m_DelayTime/1000.0;
if(!SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS))AfxMessageBox("set priority class error");
if(!SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_HIGHEST))AfxMessageBox("set thread priority error");
int temp,i=0;
CString s;
for(n=0;n<m_Samples;n++){
QueryPerformanceCounter(&litmp);//us
QPart1 = litmp.QuadPart;//us
temp=rand()%int(m_Valve2-m_Valve1);
m_p1[n]=m_Valve1+temp;
if (m_p1[n]>m_Valve2)
{
AfxMessageBox("超过上限");
break;
}
if (m_p1[n]<m_Valve1)
{
AfxMessageBox("超过下限");
break;
}
s.Format("%d",n);
m_list.InsertItem(n,s);
s.Format("%f",m_p1[n]);
m_list.SetItemText(n,1,s);
do
{
QueryPerformanceCounter(&litmp);
QPart2 = litmp.QuadPart;
dfMinus = (double)(QPart2 - QPart1);
dfTim = dfMinus/dfFreq * 1000*1000;
}while(dfTim<us);//us
}
//结束处理
if(!SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS))AfxMessageBox("set priority class error");
if(!SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL))AfxMessageBox("set thread priority error");
UpdateData(false);
OnDraw();
if (n==m_Samples) {
AfxMessageBox("运行结束");
}
}
void CDemoDlg::DrawResult(CDC *pDC)//pressure
{
int i;
float ddt;
CString str;
//DrawCoordinate(pDC);
DrawRectCoordiante(pDC);
CFont font;
VERIFY(font.CreatePointFont(70, "Arial", pDC));
CFont* def_font = pDC->SelectObject(&font);//保存原来字体
ddt=(float)m_disy*m_PressureChartMax/(float)(m_orgy-m_bottomoff-m_topoff);
//纵坐标刻度
i=0;
do{
str.Format("%3.2f",i*ddt);
pDC->TextOut(2,m_orgy-i*m_disy-m_bottomoff-m_disy/4,str);
i++;
}while(i*m_disy<(m_orgy-m_bottomoff-m_topoff));//
pDC->TextOut(m_orgx-m_rightoff-150,m_topoff+2,"温 度 曲 线 ℃");
pDC->SelectObject(def_font);
font.DeleteObject();
//画曲线
CPen mypen;
COLORREF PenColor=RGB(255,0,0);
mypen.CreatePen(PS_SOLID,1,PenColor);
CPen* LastPen=pDC->SelectObject(&mypen);
pDC->MoveTo(GetDrawPressurePoint(0,m_p1[0]));
for(i=1;i<m_Samples;i++){
pDC->LineTo(GetDrawPressurePoint(i,m_p1[i]));
}
pDC->SelectObject(LastPen);
mypen.DeleteObject();
}
void CDemoDlg::OnDraw()
{
// TODO: Add your control notification handler code here
UpdateData(true);
Invalidate();
}
void CDemoDlg::OnSaveToFile()
{
CFileDialog fdlg(false,"*.txt","*.txt",OFN_HIDEREADONLY ,"(*.txt)|*.txt|");
if(fdlg.DoModal()==IDOK)
{
FILE* pFile=NULL;
pFile=fopen(fdlg.GetPathName(),"w");
fprintf(pFile,"***********随机温度运行结果**************\n");
fprintf(pFile,"采样周期\t采样次数\t最低温度输入\t最高温度输入\n");
fprintf(pFile,"%dms\t%d\t%f\t%f\n", m_DelayTime,m_Samples,m_Valve1,m_Valve2);
fprintf(pFile,"step\t p1\n");
for(int i=0;i<m_Samples;i++)
{
// fprintf(pFile,"%d\t%f\t%f\t%f\n",i, m_p1[i],m_p2[i],m_p2[i]/m_p1[i]);
fprintf(pFile,"%d\t%f\n",i, m_p1[i]);
}
fprintf(pFile,"********************************\n");
fclose(pFile);
}
}
void CDemoDlg::DrawCoordinate(CDC *pDC)//画坐标系
{
int i;
LONG xx;
LONG yy;
CWnd* pWnd;
CRect rect;
pWnd=pDC->GetWindow( );
pWnd->GetClientRect(&rect);
// COLORREF FillColor=RGB(255,255,255);
//pDC->FillSolidRect(rect,FillColor);
pDC->Rectangle(rect);
m_orgx=rect.right;
m_orgy=rect.bottom;
yy=m_orgy-m_bottomoff;
xx=m_orgx-m_rightoff;
//画坐标
pDC->MoveTo(m_leftoff,yy);
pDC->LineTo(xx,yy);
pDC->MoveTo(m_leftoff,yy);
pDC->LineTo(m_leftoff,m_topoff);
//画箭头
pDC->MoveTo(m_leftoff,m_topoff);
pDC->LineTo(m_leftoff-2,m_topoff+6);
pDC->MoveTo(m_leftoff,m_topoff);
pDC->LineTo(m_leftoff+2,m_topoff+6);
pDC->MoveTo(m_orgx-m_rightoff,m_orgy-m_bottomoff);
pDC->LineTo(m_orgx-m_rightoff-6,m_orgy-m_bottomoff+2);
pDC->MoveTo(m_orgx-m_rightoff,m_orgy-m_bottomoff);
pDC->LineTo(m_orgx-m_rightoff-6,m_orgy-m_bottomoff-2);
//画刻度
CFont font;
VERIFY(font.CreatePointFont(70, "Arial", pDC));
CFont* def_font = pDC->SelectObject(&font);//保存原来字体
pDC->TextOut(xx/2,yy+m_bottomoff/2-1,"Time(Second)");
float ddt;//s
ddt=(float)(m_disx*m_DelayTime*m_Samples)/(float)(m_orgx-m_leftoff-m_rightoff)/1000.0;
CString str;
pDC->TextOut(m_leftoff,m_orgy-m_bottomoff+1,"0");
i=1;
do{
pDC->MoveTo(i*m_disx+m_leftoff,yy);
pDC->LineTo(i*m_disx+m_leftoff,yy-5);
str.Format("%1.3f",i*ddt);
pDC->TextOut(i*m_disx+m_leftoff-m_disx/2,yy+1,str);
i++;
}while(i*m_disx<(xx-m_leftoff));
i=1;
do{
pDC->MoveTo(m_leftoff,m_orgy-i*m_disy-m_bottomoff);
pDC->LineTo(m_leftoff+5,m_orgy-i*m_disy-m_bottomoff);
i++;
}while(i*m_disy<(yy-m_topoff));
pDC->SelectObject(def_font);
font.DeleteObject();
}
void CDemoDlg::DrawRectCoordiante(CDC *pDC)
{
int i;
LONG xx;
LONG yy;
CWnd* pWnd;
CRect rect;
pWnd=pDC->GetWindow( );
pWnd->GetClientRect(&rect);
pDC->Rectangle(rect);
m_orgx=rect.right;
m_orgy=rect.bottom;
yy=m_orgy-m_bottomoff;
xx=m_orgx-m_rightoff;
pDC->Rectangle(m_leftoff,m_topoff,xx,yy);
CPen mypen;
COLORREF PenColor=RGB(0,0,0);
mypen.CreatePen(PS_DOT,1,PenColor);
CPen* LastPen=pDC->SelectObject(&mypen);
CFont font;
VERIFY(font.CreatePointFont(70, "Arial", pDC));
CFont* def_font = pDC->SelectObject(&font);//保存原来字体
pDC->TextOut(xx/2,yy+m_bottomoff/2-1,"Time(Second)");
float ddt;//s
ddt=(float)(m_disx*m_DelayTime*m_Samples)/(float)(m_orgx-m_leftoff-m_rightoff)/1000.0;
CString str;
pDC->TextOut(m_leftoff,m_orgy-m_bottomoff,"0");
i=1;
do{
pDC->MoveTo(i*m_disx+m_leftoff,yy);
pDC->LineTo(i*m_disx+m_leftoff,m_topoff);
str.Format("%1.3f",i*ddt);
pDC->TextOut(i*m_disx+m_leftoff-m_disx/2,yy,str);
i++;
}while(i*m_disx<(xx-m_leftoff));//横坐标刻度
i=1;
do{
pDC->MoveTo(m_leftoff,m_orgy-i*m_disy-m_bottomoff);
pDC->LineTo(xx,m_orgy-i*m_disy-m_bottomoff);
i++;
}while(i*m_disy<(yy-m_topoff));//纵坐标刻度
pDC->SelectObject(LastPen);
pDC->SelectObject(def_font);
font.DeleteObject();
// COLORREF FillColor=RGB(255,255,255);
//pDC->FillSolidRect(rect,FillColor);
}
CPoint CDemoDlg::GetDrawPressurePoint(float x, float y)
{
int Dx,Dy;
int i=0;
Dx=x*(m_orgx-m_leftoff-m_rightoff)/(float)m_Samples+m_leftoff;
Dy=(m_orgy-m_bottomoff)-y*(float)(m_orgy-m_bottomoff-m_topoff)/m_PressureChartMax;
CPoint p(Dx,Dy);
return p;
}
void CDemoDlg::OnCancel()
{
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -