// mfcplotView.cpp: CmfcplotView 类的实现
//
#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "mfcplot.h"
#endif
#include "mfcplotDoc.h"
#include "mfcplotView.h"
#include <vector>
using std::vector;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CmfcplotView
IMPLEMENT_DYNCREATE(CmfcplotView, CView)
BEGIN_MESSAGE_MAP(CmfcplotView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
// ON_WM_NCMOUSEMOVE()
ON_WM_MOUSEMOVE()
ON_WM_SETCURSOR()
ON_COMMAND(ID_MOVE_MENU, &CmfcplotView::OnMoveMenu)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//ON_UPDATE_COMMAND_UI(ID_EDGE_MENU, &CmfcplotView::OnUpdateEdgeMenu)
ON_UPDATE_COMMAND_UI(ID_MOVE_MENU, &CmfcplotView::OnUpdateMoveMenu)
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
// CmfcplotView 构造/析构
CmfcplotView::CmfcplotView() noexcept
{
// TODO: 在此处添加构造代码
isMoving = false;
//下面这个没什么用,防止编译警告
tmp_Xmin = m_Xmin = -10;
tmp_Xmax = m_Xmax = 10;
tmp_Ymin = m_Ymin = -1;
tmp_Ymax = m_Ymax = 1;
}
CmfcplotView::~CmfcplotView()
{
}
BOOL CmfcplotView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CView::PreCreateWindow(cs);
}
//
double CmfcplotView::LPxtoFPx(int x) {
return m_Xmin + (1.0 * x - nLeft) * (m_Xmax - m_Xmin) / (1.0 * nRight - nLeft);
}
int CmfcplotView::FPxtoLPx(double x) {
return nLeft + (int)round((x - m_Xmin) * (1.0 * nRight - nLeft) / (m_Xmax - m_Xmin));
}
double CmfcplotView::LPytoFPy(int y) {
return m_Ymin + (1.0 * nButton - y) * (m_Ymax - m_Ymin) / (1.0 * nButton - nTop);
}
int CmfcplotView::FPytoLPy(double y) {
return nButton - (int)round((y - m_Ymin) * (1.0 * nButton - nTop) / (m_Ymax - m_Ymin));
}
// CmfcplotView 绘图
void CmfcplotView::OnDraw(CDC* pDC)
{
CmfcplotDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
m_Xmin = pDoc->m_Xmin;//极值保存在Doc中
m_Xmax = pDoc->m_Xmax;
m_Ymin = pDoc->m_Ymin;
m_Ymax = pDoc->m_Ymax;
CRect rect;
GetClientRect(&rect);//获得视图区
nTop = (int)round(rect.bottom * 0.1); //函数图像不会占据整个视图区
nButton = (int)round(rect.bottom * 0.9);
nLeft = (int)round(rect.right * 0.1);
nRight = (int)round(rect.right * 0.9);
if (pDoc->m_WillShowEdge) {//画边框
pDC->MoveTo(nLeft, nTop);
pDC->LineTo(nLeft, nButton);
pDC->LineTo(nRight, nButton);
pDC->LineTo(nRight, nTop);
pDC->LineTo(nLeft, nTop);
}
//画x坐标信息
int nX,nY;
bool BIGX = abs(m_Xmin) > 100 || abs(m_Xmax) > 100;//x坐标值比较大时,标注更稀
for (nX = nLeft; nX < nRight; nX += (BIGX ? 100 : 50)) { //每隔100/50像素一个标注
CRect textRect(nX - (BIGX ? 50 : 25), nButton + 1, nX + (BIGX ? 50 : 25), nButton + 20);//显示区域
CString xInfo;
xInfo.Format(_T("%.2f"),LPxtoFPx(nX));
pDC->DrawText(xInfo, &textRect, DT_SINGLELINE | DT_CENTER);
//单行,上下左右居中显示
}
if (nX - nRight <= (BIGX ? 50 : 25)) {//最后一个x坐标,与前一个标注距离太近则不显示
CRect textRect(nRight, nButton + 1, nRight + 50, nButton + 20);
CString xInfo;
xInfo.Format(_T("%.2f"),m_Xmax);
pDC->DrawText(xInfo, &textRect, DT_SINGLELINE | DT_LEFT | DT_TOP);
}
//y坐标
for (nY = nButton - 50; nY > nTop; nY -= 50) {
CRect textRect(nLeft - 200, nY-10, nLeft - 3, nY + 10);
CString yInfo;
yInfo.Format(_T("%.2f"), LPytoFPy(nY));
pDC->DrawText(yInfo, &textRect, DT_SINGLELINE | DT_RIGHT);
}
if (nTop - nY <= 25) {
CRect textRect(nLeft - 200, nTop - 10, nLeft - 3, nTop + 10);
CString yInfo;
yInfo.Format(_T("%.2f"),m_Ymax);
pDC->DrawText(yInfo, &textRect, DT_SINGLELINE | DT_BOTTOM | DT_RIGHT);
}
// 显示网格
if (pDoc->m_WillShowGrid) {
CPen pen(PS_DOT, 1, RGB(100, 100, 100)); //创建笔,虚线,并调整坐标颜色灰色
CPen *pOldPen = (CPen *)pDC->SelectObject(&pen);
for (nX = nLeft+50; nX < nRight; nX += 50) {
pDC->MoveTo(nX, nTop);
pDC->LineTo(nX, nButton);
}
for (nY = nButton - 50; nY > nTop; nY -= 50) {
pDC->MoveTo(nLeft, nY);
pDC->LineTo(nRight, nY);
}
pDC->SelectObject(pOldPen);
}
// 显示坐标轴
if (pDoc->m_WillShowAxis) {
CPen pen(PS_SOLID, 2, RGB(0, 0, 0));
CPen* pOldPen = (CPen*)pDC->SelectObject(&pen);
int oX = FPxtoLPx(0);
int oY = FPytoLPy(0);
bool showY = oX >= nLeft && oX <= nRight;
bool showX = oY >= nTop && oY <= nButton;//判断x,y轴是否在范围内
if (showX) {
pDC->MoveTo(nLeft - 10, oY);
pDC->LineTo(nRight + 10, oY);
}
if (showY) {
pDC->MoveTo(oX, nButton + 10);
pDC->LineTo(oX, nTop - 10);
}
if (showX && showY) {
pDC->TextOutW(oX + 1, oY + 1, _T("O"));
}
if (showX) {
pDC->MoveTo(nRight + 10, oY);
pDC->LineTo(nRight + 5, oY + 5);
pDC->MoveTo(nRight + 10, oY);
pDC->LineTo(nRight + 5, oY - 5);
pDC->TextOutW(nRight + 10, oY, _T("X轴"));
}
if (showY) {
pDC->MoveTo(oX, nTop - 10);
pDC->LineTo(oX - 5, nTop - 5);
pDC->MoveTo(oX, nTop - 10);
pDC->LineTo(oX + 5, nTop - 5);
pDC->TextOutW(oX + 5, nTop - 10, _T("Y轴"));
}
pDC->SelectObject(pOldPen);
}
POSITION p = pDoc->m_List.GetHeadPosition();
int showTop = nTop;
while (p != nullptr) {
bool shouldMov = true;//一段曲线第一个点MoveTo,其他都是LineTo
FuncData* tmpFD = (FuncData*)pDoc->m_List.GetNext(p);
CPen pen(tmpFD->m_penType, tmpFD->m_penWidth, tmpFD->m_color);
CPen* pOldPen = (CPen*)pDC->SelectObject(&pen);
if (tmpFD->FuncCas == CAS_NORMAL) {//动态X坐标模式下,普通函数x范围与视图不同时自动同步
if (pDoc->m_ForceXrange && isMoving!=2)
if (tmpFD->minX != m_Xmin || tmpFD->maxX != m_Xmax) {
tmpFD->minX = m_Xmin;
tmpFD->maxX = m_Xmax;
tmpFD->CalcList();
}
}
for (auto dot : tmpFD->vetPoint) {
if (dot.first < m_Xmin || dot.first > m_Xmax || dot.second < m_Ymin || dot.second > m_Ymax || dot.second != dot.second) {
shouldMov = true;
continue;
}
if (shouldMov) {
pDC->MoveTo(FPxtoLPx(dot.first), FPytoLPy(dot.second));
shouldMov = false;
}
else
pDC->LineTo(FPxtoLPx(dot.first), FPytoLPy(dot.second));
}
pDC->MoveTo(nRight+5, showTop);//显示图例
pDC->LineTo(rect.right, showTop);
showTop += 5;
if (tmpFD->FuncCas == CAS_NORMAL)
pDC->TextOutW(nRight + 5, showTop, _T("f(x)=")+tmpFD->m_Equation);
else if (tmpFD->FuncCas == CAS_POLAR)
pDC->TextOutW(nRight + 5, showTop, _T("r(t)=") + tmpFD->m_Equation);
else if (tmpFD->FuncCas == CAS_TWO) {
pDC->TextOutW(nRight + 5, showTop, _T("x(t)=") + tmpFD->m_Equation);
showTop += 20;
pDC->TextOutW(nRight + 5, showTop, _T("y(t)=") + tmpFD->GetEquation2());
} else if (tmpFD->FuncCas == CAS_DATA)
pDC->TextOutW(nRight + 5, showTop, _T("y(t)=") + tmpFD->m_Equation);
showTop += 25;
pDC->SelectObject(pOldPen);
}
}
// CmfcplotView 打印
BOOL CmfcplotView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CmfcplotView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void CmfcplotView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
// CmfcplotView 诊断
#ifdef _DEBUG
void CmfcplotView::AssertValid() const
{
CView::AssertValid();
}
void CmfcplotView::Dump(CDump
没有合适的资源?快使用搜索试试~ 我知道了~
mfc绘制数学函数图像
共68个文件
h:17个
cpp:14个
sample:12个
5星 · 超过95%的资源 需积分: 50 80 下载量 92 浏览量
2020-07-18
22:52:49
上传
评论 11
收藏 583KB 7Z 举报
温馨提示
基于MFC编写一个单文档程序,在客户区用图形来展现各种数学函数曲线。 具体要求: 1)可绘制常见的数学函数曲线,如三角函数、指数函数、幂函数等。 2)通过菜单选择不同的函数曲线类型,可以通过对话框指定该函数的参数,如Xa中的幂a的具体设置,sin(ax+b)中的参数a、b的设置。 3)绘制数学曲线时应包括坐标轴和刻度以及相应的数字(字符),数学函数公式; 4)可以在一个坐标系中绘制多条数学曲线; 5)可以选择不同颜色来绘制不同的曲线。 6)当鼠标移动到曲线上某点时,可以显示该点的坐标。 7)可以进行图形的缩放。
资源推荐
资源详情
资源评论
收起资源包目录
mfcplot.7z (68个子文件)
mfcplot
mfcplot
mfcplotView.h 2KB
CDataFuncDlg.cpp 2KB
.gitignore 6KB
mfcplot.vcxproj 11KB
mfcplot.cpp 5KB
framework.h 2KB
CSetXYrangeDlg.cpp 1KB
CTwoFuncDlg.h 718B
mfcplot.sln 1KB
CPolarFuncDlg.cpp 2KB
mfcplotDoc.h 2KB
mfcplot.vcxproj.filters 4KB
课设报告.doc 607KB
targetver.h 299B
pch.cpp 158B
MainFrm.h 715B
CDataFuncDlg.h 650B
CDelFuncDlg.cpp 597B
CTwoFuncDlg.cpp 2KB
.gitattributes 3KB
mfcplot.h 550B
CFuncDlg.cpp 9KB
CalculatorFunc.cpp 4KB
mfcplot.rc 46KB
FuncData.cpp 7KB
MainFrm.cpp 2KB
CSetXYrangeDlg.h 611B
Resource.h 5KB
CNormalFuncDlg.cpp 2KB
mfcplotView.cpp 13KB
res
mfcplot.ico 66KB
mfcplotDoc.ico 5KB
Toolbar.bmp 2KB
mfcplot.rc2 670B
icon1.ico 44KB
CPolarFuncDlg.h 673B
CFuncDlg.h 2KB
CDelFuncDlg.h 446B
CalculatorFunc.h 437B
pch.h 544B
FuncData.h 3KB
.git
HEAD 23B
packed-refs 114B
index 3KB
objects
pack
pack-7123f18b041376c40bb5b570f367768b61095734.idx 5KB
pack-7123f18b041376c40bb5b570f367768b61095734.pack 132KB
info
description 73B
config 300B
info
exclude 240B
hooks
pre-applypatch.sample 424B
pre-commit.sample 2KB
pre-merge-commit.sample 416B
applypatch-msg.sample 478B
pre-rebase.sample 5KB
commit-msg.sample 896B
prepare-commit-msg.sample 1KB
update.sample 4KB
pre-receive.sample 544B
fsmonitor-watchman.sample 5KB
post-update.sample 189B
pre-push.sample 1KB
logs
HEAD 202B
refs
heads
master 202B
remotes
origin
HEAD 202B
refs
tags
heads
master 41B
remotes
origin
HEAD 32B
mfcplotDoc.cpp 12KB
CNormalFuncDlg.h 833B
共 68 条
- 1
资源评论
- QASWINE2020-09-21https://gitee.com/rewine/mfcplot
QASWINE
- 粉丝: 82
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功