Introduction
This article describes how you can add a static control to show a math function (or deferential or integral of it). This control is a CStatic
base. First of all, add 2DStatic.h and 2DStatic.cpp files to your project. Select Resource tab from Workspace window, and select your dialog that you want to add a display static. Select Static Control from Control Toolbox and draw it on dialog (Figure 1). Change its ID from IDC_STATIC
to IDC_MyStc
.
Figure 1 - Add Static and Button control to your dialog.
Now it's time to add a member variable to your dialog class. Call Class Wizard to do it for you. Figure 2 shows you how to do it. In this case, we add a member variable m_MyStc
with type CStatic
.
Figure 2 - Add member variable to your dialog class.
OK, open your dialog class header file, add this line on top of your class definition: #include "2DStatic.h"
.
#if !defined(AFX_2DFUNCTIONDLG_H__D5D048D5_079A_
40BD_86A0_32A26253D2E5__INCLUDED_)
#define AFX_2DFUNCTIONDLG_H__D5D048D5_079A_40BD_
86A0_32A26253D2E5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif
#include "2DStatic.h"
class C2DFunctionDlg : public CDialog
{
public:
C2DFunctionDlg(CWnd* pParent = NULL);
enum { IDD = IDD_MY2DFUNCTION_DIALOG };
C2DStatic m_MyStc;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
};
#endif
Now you can draw any function you want, only with two functions SetInitialValue
, DrawFunction
. E.g.:
m_MyStc.SetInitialValue(-15,15,-2,2);
DrawFunction(0,"sin(x)");
m_MyStc.SetInitialValue(-30,30,-10,300);
DrawFunction(1,"tan(x)");
m_MyStc.SetInitialValue(-15,15,-2,2);
DrawFunction(2,"3*x^2-2*x-4)");
Member Functions
SetInitialValue(float xMin,float xMax,float yMin,float yMax);
CPoint GetNewPixel(float x,float y);
double MathFunc(double x);
double FuncDef(double x);
double FuncInt(double x);
void ShowMathFunc(CDC* pDC);
void ShowFuncDef(CDC* pDC);
void ShowFuncInt(CDC* pDC);
bool DrawFunction(int iKind,CString sFormula);
bool SetIndexPra(CString StrFormula);
bool SimString(CString StrEdit);
double StrToInt();
double Specialfunc(CString SpFunc);
int FindNextOperation(CString strEdit,int iIndex);
CString FindOpBeforeSpc(CString StrEdit);
Note
I thank Mr. Abbas Riazi and Mr. Mehdi Bonvori for their guidance. This class has 2D draw functions and I tried to make a powerful string processing in it. But I'm sure there are a lot of problems when you use it. I'll be grateful if you give feedback on the problems, to me.