2条回答 默认 最新
- maxos 2019-03-05 22:48关注
完整源代码下载链接: https://pan.baidu.com/s/14mTPTMBEmQN_A84t4XUgWg
提取码: y6nv关键代码:
BOOL CDrawImageDlg::OnEraseBkgnd(CDC* pDC) { CDialogEx::OnEraseBkgnd(pDC); if (pDC == NULL) { return true; } // 从编辑框取值: std::wstring wsRangeX = GetEditText(IDC_EDIT4); std::wstring wsRangeY = GetEditText(IDC_EDIT5); std::wstring wsDrawLeft = GetEditText(IDC_EDIT6); std::wstring wsDrawTop = GetEditText(IDC_EDIT7); std::wstring wsDrawRight = GetEditText(IDC_EDIT8); std::wstring wsDrawBottom = GetEditText(IDC_EDIT9); // 定义 float fRangeX = (float)wcstof(wsRangeX.c_str(), NULL); float fRangeY = (float)wcstof(wsRangeY.c_str(), NULL); // x轴与 y轴的起始量: int nStartX = (int)wcstol(wsDrawLeft.c_str(), NULL, 10); int nStartY = (int)wcstol(wsDrawTop.c_str(), NULL, 10); // X轴与Y轴的绘制区域,防止画出界: int nRangeWidth = (int)wcstol(wsDrawRight.c_str(), NULL, 10); int nRangeHeight = (int)wcstol(wsDrawBottom.c_str(), NULL, 10); // 计算坐标中心点: int nBaseX = nStartX + (nRangeWidth ) / 2; int nBaseY = nStartY + (nRangeHeight) / 2; // 计算单个刻度需要跨多少像素(X轴): float fPixelX = ((float)nRangeWidth / (fRangeX*2.0f)); // 计算单个刻度需要跨多少像素(Y轴): float fPixelY = ((float)nRangeHeight / (fRangeY*10.0f*2.0f)); // 画坐标轴: { // 绘制外框: pDC->Rectangle(nStartX, nStartY, nStartX + nRangeWidth, nStartY+nRangeHeight); // 绘制X轴基线(坐标轴) pDC->MoveTo(nStartX, nBaseY); pDC->LineTo(nStartX+nRangeWidth, nBaseY); // 绘制Y轴基线(坐标轴) pDC->MoveTo(nBaseX, nStartY); pDC->LineTo(nBaseX, nStartY+nRangeHeight); // 绘制刻度(+X轴): float i = 0.0f, x = 0.0f, y = 0.0f; for (i = 0.0f; i < (float)fRangeX; i += 1.0f) { int nLen = 4; x = ((float)nBaseX + (fPixelX*(float)i)); int xPos = (int)x; pDC->MoveTo(xPos, nBaseY); if (((int)i % 5) == 0) { wchar_t wsText[8] = L""; wsprintf(wsText, L"%d", (int)i); pDC->TextOutW(xPos, nBaseY+3, wsText, wcslen(wsText)); pDC->LineTo(xPos, nBaseY - nLen * 2); } else { pDC->LineTo(xPos, nBaseY - nLen); } } // 绘制刻度(-X轴): i = 0.0f, x = 0.0f; for (i = 0.0f; i < (float)fRangeX; i += 1.0f) { int nLen = 4; x = ((float)nBaseX - (fPixelX*(float)i)); int xPos = (int)x; pDC->MoveTo(xPos, nBaseY); if (((int)i % 5) == 0) { if ((int)i != 0) { // '0'在画正坐标轴时画过了。 wchar_t wsText[8] = L""; wsprintf(wsText, L"-%d", (int)i); pDC->TextOutW(xPos, nBaseY + 3, wsText, wcslen(wsText)); } pDC->LineTo(xPos, nBaseY - nLen * 2); } else { pDC->LineTo(xPos, nBaseY - nLen); } } // 绘制刻度(+Y轴): i = 0.0f, y = 0.0f; for (i = 0.0f; i < (float)(fRangeY*10.0f); i += 1.0f) { int nLen = 4; y = ((float)nBaseY - (fPixelY*(float)i)); int yPos = (int)y; pDC->MoveTo(nBaseX, yPos); if (((int)i % 5) == 0) { if ((int)i != 0) { wchar_t wsText[8] = L""; swprintf(wsText, L"%.1f", i / 10.0f); // 文字向右移 2 像素: pDC->TextOutW(nBaseX + nLen * 2 + 2, yPos, wsText, wcslen(wsText)); } pDC->LineTo(nBaseX + nLen * 2, yPos); } else { pDC->LineTo(nBaseX + nLen, yPos); } } // 绘制刻度(-Y轴): i = 0.0f, y = 0.0f; for (i = 0.0f; i < (float)(fRangeY*10.0f); i += 1.0f) { int nLen = 4; y = ((float)nBaseY + (fPixelY*(float)i)); int yPos = (int)y; pDC->MoveTo(nBaseX, yPos); if (((int)i % 5) == 0) { if ((int)i != 0) { wchar_t wsText[8] = L""; swprintf(wsText, L"-%.1f", (i/10.0f)); // 文字向右移 2 像素: pDC->TextOutW(nBaseX + nLen * 2 + 2, yPos, wsText, wcslen(wsText)); } pDC->LineTo(nBaseX + nLen * 2, yPos); } else { pDC->LineTo(nBaseX + nLen, yPos); } } } // 绘制算法图像: { float fX = 0.0f, fY = 0.0f; // 绘制正坐标轴上的图像: int nFirstPoint = 0; for (fX = 0.0f; fX <= (fRangeX); fX += 0.02f) { // 计算数值: float fSinXValue = sinf(fX); // Y 坐标对齐 Y 轴刻度: float fRealY = (nRangeHeight / 2.0f)*fSinXValue / fRangeY; // 绘制到屏幕上: int x = nBaseX + (int)(fX*fPixelX); int y = nBaseY - (int)fRealY; if (nFirstPoint == 0) { pDC->MoveTo(x, y); nFirstPoint = 1; } pDC->LineTo(x, y); } // 绘制负轴上的图像: nFirstPoint = 0; for (fX = 0.0f; fX >= (-fRangeX); fX -= 0.02f) { // 计算数值: float fSinXValue = sinf(fX); // Y 坐标对齐 Y 轴刻度: float fRealY = (nRangeHeight / 2.0f)*fSinXValue / fRangeY; // 绘制到屏幕上: int x = nBaseX + (int)(fX*fPixelX); int y = nBaseY - (int)fRealY; if (nFirstPoint == 0) { pDC->MoveTo(x, y); nFirstPoint = 1; } pDC->LineTo(x, y); } } return true; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用 1
悬赏问题
- ¥15 如果要做一个老年人平板有哪些需求
- ¥15 k8s生产配置推荐配置及部署方案
- ¥15 matlab提取运动物体的坐标
- ¥15 人大金仓下载,有人知道怎么解决吗
- ¥15 一个小问题,本人刚入门,哪位可以help
- ¥15 python安卓开发
- ¥15 使用R语言GD包一直不出结果
- ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
- ¥15 如何根据一个截图编写对应的HTML代码
- ¥15 stm32标准库的PID角度环