(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; } else {
CDialog::OnPaint(); }}
void chart::Resize(void) { float fsp[2];
POINT newPoint;//获取当前对话框⼤⼩ CRect newRect;//获取当前对话框的坐标 GetClientRect(&newRect);
newPoint.x = newRect.right - newRect.left; newPoint.y = newRect.bottom - newRect.top; fsp[0] = (float)newPoint.x / oldPiont.x; fsp[1] = (float)newPoint.y / oldPiont.y;
int woc; CRect rect;
CPoint oldTLPoint, newTLPoint;//左上⾓ CPoint oldBRPoint, newBRPoint;//右下⾓ //列出所有的⼦空间
HWND hwndChild = ::GetWindow(m_hWnd, GW_CHILD); while (hwndChild) {
woc = ::GetDlgCtrlID(hwndChild);//取得ID GetDlgItem(woc)->GetWindowRect(rect); ScreenToClient(rect);
oldTLPoint = rect.TopLeft();
newTLPoint.x = long(oldTLPoint.x*fsp[0]); newTLPoint.y = long(oldTLPoint.y*fsp[1]); oldBRPoint = rect.BottomRight();
newBRPoint.x = long(oldBRPoint.x*fsp[0]); newBRPoint.y = long(oldBRPoint.y*fsp[1]);
rect.SetRect(newTLPoint, newBRPoint);
GetDlgItem(woc)->MoveWindow(rect, TRUE);
hwndChild = ::GetWindow(hwndChild, GW_HWNDNEXT); }
oldPiont = newPoint; return;}
void chart::OnSize(UINT nType, int cx, int cy) { //窗体⼤⼩发⽣变动。处理函数resize
if (nType == SIZE_RESTORED || nType == SIZE_MAXIMIZED) {
Resize(); }}
void chart::DataShow(double *xb, double *yb, int len) { m_ChartCtrl.EnableRefresh(false); CChartLineSerie *pLineSerie; m_ChartCtrl.RemoveAllSeries();
pLineSerie = m_ChartCtrl.CreateLineSerie();
pLineSerie->SetSeriesOrdering(poNoOrdering);//设置为⽆序 pLineSerie->AddPoints(xb, yb, len); UpdateWindow();
m_ChartCtrl.EnableRefresh(true);}
void chart::OnTimer(UINT nIDEvent) { static int offset = 0;
for (int m = 0; m < DATA_SHOW_LENGHT - DATA_UPDATE_LENGHT; m++) {
//xd[m] = xd[DATA_UPDATE_LENGHT + m];
yBuff[m] = yBuff[DATA_UPDATE_LENGHT + m]; }
int index = DATA_SHOW_LENGHT - DATA_UPDATE_LENGHT; for (int i = 0; i < DATA_UPDATE_LENGHT; i++) {
//yd[index + i] = cos((index + i + w)/5) * 50 + 100+rand() / 1000;
yBuff[index + i] = cos((i + offset) / 5) * DATA_SHOW_Y_AXIS / 4 + rand() / 1000 + DATA_SHOW_Y_AXIS / 2; }
DataShow(xBuff, yBuff, DATA_SHOW_LENGHT); offset++;
if (offset > 10000) { offset = 0; }}
View Code chart.h
#pragma once
#include \"ChartCtrl/ChartCtrl.h\"#include \"ChartCtrl/ChartTitle.h\"
#include \"ChartCtrl/ChartLineSerie.h\"#include \"ChartCtrl/ChartAxisLabel.h\"// chart dialog
class chart : public CDialog{
DECLARE_DYNAMIC(chart)
public:
chart(CWnd* pParent = nullptr); // standard constructor
virtual ~chart();
// Dialog Data
#ifdef AFX_DESIGN_TIME enum { IDD = IDD_chart };#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // 实现protected:
// ⽣成的消息映射函数
virtual BOOL OnInitDialog(); afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnSize(UINT nType, int cx, int cy); afx_msg void OnTimer(UINT nIDEvent); DECLARE_MESSAGE_MAP()
public:
POINT oldPiont; void Resize(void);
CChartCtrl m_ChartCtrl; void ChartCtrlInit(void); void DataBuffInit(void);
void DataShow(double *xb, double *yb, int len);};
View Code
4. 控制台调⽤动态折线图
chart *chartdialog = new chart;
int ReturnValue = chartdialog->DoModal(); // Show the dialogprintf(\"%d\", ReturnValue);
View Code
效果图
可能出现的问题
1. 在编译的时候,ChartCtrl⾥⾯的cpp⽼是出现没有\"pch.h\"即预编译头的问题,所以⼲脆取消了预编译头进⾏编译。
2. 将ChartCtrl库放到项⽬⾥。添加之后取名为ChartCtrl,然后将⽂件都已Add --> Existing Item的⽅式加进项⽬⾥。ChartCtrl⽂件夹的存放路径与控制台的cpp⽂件同⽬录。
源码:CPUUsage.cpp
// CPUUsage.cpp : This file contains the 'main' function. Program execution begins and ends there.//
#include \"pch.h\"
#include \"framework.h\"#include \"chart.h\"
#include \"CPUUsage.h\"#ifdef _DEBUG
#define new DEBUG_NEW#endif
// The one and only application objectCWinApp theApp;using namespace std;int main(){
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(nullptr);
if (hModule != nullptr) {
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0)) {
// TODO: code your application's behavior here. wprintf(L\"Fatal Error: MFC initialization failed\\n\"); nRetCode = 1; } else {
// TODO: code your application's behavior here. chart *chartdialog = new chart;
int ReturnValue = chartdialog->DoModal(); // Show the dialog printf(\"%d\", ReturnValue); } } else {
// TODO: change error code to suit your needs wprintf(L\"Fatal Error: GetModuleHandle failed\\n\"); nRetCode = 1; }
return nRetCode;}
View Code OK.