什么控件都不需要,建一个mfc关于dialog的工程就行,文中数据的使用随机数产生
// MFCPaintDlg.h : 头文件
//
#pragma once
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
// CMFCPaintDlg 对话框
class CMFCPaintDlg : public CDialogEx
{
// 构造
public:
CMFCPaintDlg(CWnd* pParent = NULL); // 标准构造函数
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MFCPAINT_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
int left = 80, right = 1080, top = 20, bottom = 1020;
void paintGrid(CPaintDC& dc);
int currentTime = 0;
CTime nowTime;
vector<CTime> timeVec;
int point[5] = { 1000,3000,2000,8000,1000 };
vector<int> data;
// 实现
protected:
HICON m_hIcon;
// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
};
// MFCPaintDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "MFCPaint.h"
#include "MFCPaintDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CMFCPaintDlg 对话框
CMFCPaintDlg::CMFCPaintDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_MFCPAINT_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMFCPaintDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMFCPaintDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
END_MESSAGE_MAP()
// CMFCPaintDlg 消息处理程序
BOOL CMFCPaintDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
ShowWindow(SW_NORMAL);
// TODO: 在此添加额外的初始化代码
CRect rect;
GetWindowRect(&rect);
rect.bottom = rect.top + 1200;
rect.right = rect.left + 1200;
MoveWindow(rect);
// 获取屏幕的宽度和高度
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(this, (screenWidth - 1200) / 2, (screenHeight - 1200) / 2,1200,1200,100);
// 设置种子,以确保每次运行程序时生成不同的随机数序列
srand((int)time(NULL));
SetTimer(1,1000,NULL);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CMFCPaintDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CMFCPaintDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); // 用于绘制的设备上下文
paintGrid(dc);
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCPaintDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMFCPaintDlg::paintGrid(CPaintDC& dc)
{
dc.MoveTo(left, top);
dc.LineTo(left, bottom);
dc.LineTo(right, bottom);
dc.LineTo(right, top);
dc.LineTo(left, top);
//这三句的作用是将画布清除及上面的点,而在下面重新进行网格绘制
CRect rect;
GetClientRect(&rect);
dc.FillSolidRect(rect, RGB(255, 255, 255)); // 使用白色填充整个画布
int j = 0;
// 显示网格
CPen pen(PS_SOLID, 1, RGB(150, 150, 150)); //创建笔,虚线,并调整坐标颜色灰色
CPen* pOldPen = (CPen*)dc.SelectObject(&pen);
for (int x = left; x <= right; x += 100)
{
dc.MoveTo(x, top);
dc.LineTo(x, bottom);
}
for (int i = 0; i < timeVec.size(); i++)
{
CString strTime;
strTime = timeVec[timeVec.size() - i - 1].Format(_T("%H:%M:%S"));
CSize textSize = dc.GetTextExtent(strTime); // 获取文本尺寸
int textX = right - i * 100 - textSize.cx / 2;// 计算时间文本的位置
int textY = bottom + 10;
dc.TextOut(textX, textY, strTime);
}
for (int y = bottom; y >= top; y -= 100) {
dc.MoveTo(left, y);
dc.LineTo(right, y);
CRect textRect(left - 200, y - 10, left - 3, y + 10);
CString yInfo;
yInfo.Format(_T("%.2f"), -32767 + 6553.4 * j);
dc.DrawText(yInfo, &textRect, DT_SINGLELINE | DT_RIGHT);
j += 1;
}
// 显示坐标轴X
CPen pen2(PS_SOLID, 3, RGB(0, 0, 0));
CPen* pOldPen2 = (CPen*)dc.SelectObject(&pen2);
dc.MoveTo(left - 10,bottom / 2 + 10);
dc.LineTo(right + 10, bottom / 2 + 10);
dc.LineTo(right + 5, bottom / 2 + 5);
dc.MoveTo(right + 10, bottom / 2 + 10);
dc.LineTo(right + 5, bottom / 2 + 15);
//Y
dc.MoveTo(right / 2 + 40, bottom + 10);
dc.LineTo(right / 2 + 40, top - 10);
dc.LineTo(right / 2 + 35, top - 5);
dc.MoveTo(right / 2 + 40, top - 10);
dc.LineTo(right / 2 + 45, top - 5);
CPen pen3(PS_DOT, 3, RGB(255, 0, 0)); // 创建红色的画笔
CPen* pOldPen3 = dc.SelectObject(&pen3);
for (int i = 0; i < data.size(); i++)
{
int pointX = right - (timeVec.size() - i - 1) * 100;// 计算数据点的位置
int pointY = bottom - (int)(((data[i] + 32767) / 65535.0) * 1000); // 根据数据值计算Y坐标
dc.Ellipse(pointX - 3, pointY - 3, pointX + 3, pointY + 3);
dc.TextOut(pointX + 10, pointY - 10, static_cast<LPCWSTR>(CString(std::to_string(data[i]).c_str())));
if (i > 0)
{
int startX = right - (timeVec.size() - i) * 100;
int startY;
if (data[i - 1] > 0)
startY = bottom - (int)(((data[i - 1] + 32767) / 65535.0) * 1000);
else
startY = top - (int)((data[i - 1] - 32767) / 65535.0 * 1000);
int endX = right - (timeVec.size() - i - 1) * 100;
int endY;
if (data[i] > 0)
endY = bottom - (int)(((data[i] + 32767) / 65535.0) * 1000);
else
endY = top - (int)((data[i] - 32767) / 65535.0 * 1000);
dc.MoveTo(startX, startY);
dc.LineTo(endX, endY);
}
}
dc.SelectObject(pOldPen3);
}
void CMFCPaintDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (nIDEvent == 1)
{
// 更新currentTime的值
currentTime += 1; // 假设每秒钟增加1
nowTime = CTime::GetCurrentTime();
timeVec.push_back(nowTime);
// 生成随机数
int randomNumber = std::rand();
if(currentTime % 2 == 0)
data.push_back(-randomNumber);
else
data.push_back(randomNumber);
if (timeVec.size() == 12)
{
timeVec.erase(timeVec.begin());
data.erase(data.begin());
}
InvalidateRect(NULL, FALSE);// 触发重绘
}
CDialogEx::OnTimer(nIDEvent);
}