📄 temperaturedlg.cpp
字号:
// TemperatureDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Temperature.h"
#include "TemperatureDlg.h"
#include "InputRule.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CTemperatureDlg dialog
CTemperatureDlg::CTemperatureDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTemperatureDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTemperatureDlg)
m_K = 0.1f;
m_T = 367.5f;
m_T0 = 210.0f;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTemperatureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTemperatureDlg)
DDX_Text(pDX, IDC_GETK, m_K);
DDX_Text(pDX, IDC_GETT, m_T);
DDX_Text(pDX, IDC_GETT0, m_T0);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTemperatureDlg, CDialog)
//{{AFX_MSG_MAP(CTemperatureDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_START_CONTROL, OnStartControl)
ON_BN_CLICKED(IDC_RULE, OnRule)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_STOP, OnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTemperatureDlg message handlers
BOOL CTemperatureDlg::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
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTemperatureDlg::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 CTemperatureDlg::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
{
CClientDC dlgDC(this);
CBitmap Background;
Background.LoadBitmap(IDB_BACKGROUND);
CDC BackGround;
BackGround.CreateCompatibleDC(NULL);
BackGround.SelectObject(&Background);
CDC *Back=&BackGround;
dlgDC.BitBlt(1,38,426,380,Back,0,0,SRCCOPY);
CPen penBlack(PS_SOLID,3,RGB(0,0,0));
CPen penBlue(PS_DASHDOT,1,RGB(0,0,255));
dlgDC.SelectObject(&penBlue);
dlgDC.SetBkMode(TRANSPARENT);
dlgDC.MoveTo(40,210);
dlgDC.LineTo(425,210);
dlgDC.SelectObject(&penBlack);
dlgDC.MoveTo(30,380);
dlgDC.LineTo(410,380);
dlgDC.LineTo(390,375);
dlgDC.MoveTo(410,380);
dlgDC.LineTo(390,385);
dlgDC.MoveTo(50,400);
dlgDC.LineTo(50,40);
dlgDC.LineTo(45,60);
dlgDC.MoveTo(50,40);
dlgDC.LineTo(55,60);
for(int i=1;i<=6;i++)
{
dlgDC.MoveTo(50+i*50,380);
dlgDC.LineTo(50+i*50,375);
dlgDC.MoveTo(50,380-i*50);
dlgDC.LineTo(55,380-i*50);
}
dlgDC.TextOut(370,383,"Time(S)");
dlgDC.TextOut(60,40,"Temperature(℃)");
dlgDC.TextOut(30,200,"T0");
dlgDC.SetTextColor(RGB(0,0,255));
dlgDC.TextOut(200,50,"系统初始温度 T:");
dlgDC.TextOut(200,70,"系统目标温度T0:");
dlgDC.TextOut(200,90,"系统当前温度Tn:");
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTemperatureDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTemperatureDlg::OnStartControl()
{
// TODO: Add your control notification handler code here
UpdateData();
AfxGetMainWnd()->SendMessage(WM_PAINT);
temperature.dx=0;
temperature.dtime=0;
if((m_K>0)&&(m_K<1)&&(m_T0!=0)&&(m_T/m_T0<=2)&&(m_T/m_T0>=0))
{
temperature.T_Start=m_T; //初始温度,由用户输入
temperature.T_Curr=m_T; //当前温度,初状态等于系统初始温度
temperature.T0=m_T0; //系统目标温度,由用户输入
temperature.K=m_K; //温度收敛系数,由用户输入,0-1之间,数值越大,收敛越快
GetDlgItem(IDC_STOP)->EnableWindow(TRUE);
GetDlgItem(IDC_START_CONTROL)->EnableWindow(FALSE);
GetDlgItem(IDC_RULE)->EnableWindow(FALSE);
GetDlgItem(IDC_GETT)->EnableWindow(FALSE);
GetDlgItem(IDC_GETT0)->EnableWindow(FALSE);
GetDlgItem(IDC_GETK)->EnableWindow(FALSE);
temperature.T=380-170*temperature.T_Curr/temperature.T0; //求初始温度在程序对话框界面上的相对坐标
temperature.E=(210-temperature.T)*4/170; //根据用户的输入,得到T和T0,计算出E
//设置系统初始温度、系统当前温度、系统目标温度的格式
start_temp.Format("%f℃",temperature.T_Start);
curr_temp.Format("%f℃",temperature.T_Curr);
target_temp.Format("%f℃",temperature.T0);
//输出系统初始温度、系统当前温度、系统目标温度
CClientDC dlgDC(this);
dlgDC.SetBkMode(TRANSPARENT);
dlgDC.SetTextColor(RGB(255,0,0));
dlgDC.TextOut(315,50,start_temp);
dlgDC.TextOut(315,70,target_temp);
dlgDC.TextOut(315,90,curr_temp);
dlgDC.SetTextColor(RGB(0,0,255));
dlgDC.TextOut(40,382,"0");
//绘制坐标轴上的刻毒
for(int i=1;i<=6;i++)
{
x_time.Format("%.1f",i*0.5+3*temperature.dtime);
dlgDC.TextOut(40+i*50,382,x_time);
}
for(i=1;i<=6;i++)
{
y_temp.Format("%.1f",temperature.T0*50/170*i);
dlgDC.TextOut(5,373-i*50,y_temp);
}
m_nTimer=SetTimer(1,50,NULL);
}
else
{
MessageBox("您的输入温度超出系统控制范围,请查阅系统输入参数说明!","温度控制系统模拟程序 张柳新 20401603",MB_ICONSTOP);
AfxGetMainWnd()->SendMessage(WM_PAINT);
}
}
void CTemperatureDlg::OnRule()
{
// TODO: Add your control notification handler code here
CInputRule dlgInputRule;
dlgInputRule.DoModal();
}
void CTemperatureDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CClientDC dlgDC(this);
CPen penRed(PS_SOLID,2,RGB(255,0,0));
dlgDC.SelectObject(&penRed);
dlgDC.SetTextColor(RGB(255,0,0));
dlgDC.MoveTo(50+5*temperature.dx,(int)temperature.T);
temperature.W_Member_Function();
temperature.ComputW();
temperature.ComputNext();
temperature.dx++;
dlgDC.LineTo(50+5*temperature.dx,(int)temperature.T);
dlgDC.SetBkMode(OPAQUE);
curr_temp.Format("%f℃ ",temperature.T_Curr);
dlgDC.TextOut(315,90,curr_temp);
if(50+5*temperature.dx>=425)
{
temperature.dtime++;
temperature.dx=0;
AfxGetMainWnd()->SendMessage(WM_PAINT);
dlgDC.SetBkMode(OPAQUE);
dlgDC.SetTextColor(RGB(255,0,0));
dlgDC.TextOut(315,50,start_temp);
dlgDC.TextOut(315,70,target_temp);
dlgDC.TextOut(315,90,curr_temp);
dlgDC.SetBkMode(TRANSPARENT);
dlgDC.SetTextColor(RGB(0,0,255));
for(int i=1;i<=6;i++)
{
x_time.Format("%.1f",i*0.5+3*temperature.dtime);
dlgDC.TextOut(40+i*50,382,x_time);
}
for(i=1;i<=6;i++)
{
y_temp.Format("%.1f",temperature.T0*50/170*i);
dlgDC.TextOut(5,373-i*50,y_temp);
}
}
if(temperature.T_Curr==temperature.T0)
{
KillTimer(m_nTimer);
MessageBox("系统温度达到目标值,温控模拟过程结束!","温度控制模拟程序 张柳新 20401603",MB_ICONINFORMATION);
GetDlgItem(IDC_GETT)->EnableWindow(TRUE);
GetDlgItem(IDC_GETT0)->EnableWindow(TRUE);
GetDlgItem(IDC_GETK)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
GetDlgItem(IDC_START_CONTROL)->EnableWindow(TRUE);
GetDlgItem(IDC_RULE)->EnableWindow(TRUE);
temperature.dx=0;
temperature.dtime=0;
AfxGetMainWnd()->SendMessage(WM_PAINT);
}
CDialog::OnTimer(nIDEvent);
}
void CTemperatureDlg::OnCancel()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}
void CTemperatureDlg::OnStop()
{
// TODO: Add your control notification handler code here
KillTimer(m_nTimer);
GetDlgItem(IDC_GETT)->EnableWindow(TRUE);
GetDlgItem(IDC_GETT0)->EnableWindow(TRUE);
GetDlgItem(IDC_GETK)->EnableWindow(TRUE);
GetDlgItem(IDC_STOP)->EnableWindow(FALSE);
GetDlgItem(IDC_START_CONTROL)->EnableWindow(TRUE);
GetDlgItem(IDC_RULE)->EnableWindow(TRUE);
temperature.dx=0;
temperature.dtime=0;
AfxGetMainWnd()->SendMessage(WM_PAINT);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -