65.9K
CodeProject is changing. Read more.
Home

2DStatic

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.54/5 (11 votes)

Apr 7, 2005

1 min read

viewsIcon

45382

downloadIcon

1630

An article for drawing a String formula at a static.

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".

// 2DFunctionDlg.h : header file
//

#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 // _MSC_VER > 1000

#include "2DStatic.h"
///////////////////////////////////////////////////////////
// C2DFunctionDlg dialog

class C2DFunctionDlg : public CDialog
{
// Construction
public:
  C2DFunctionDlg(CWnd* pParent = NULL);  // standard constructor

// Dialog Data
  //{{AFX_DATA(C2DFunctionDlg)
  enum { IDD = IDD_MY2DFUNCTION_DIALOG };
  C2DStatic  m_MyStc;    //We change it from CStatic m_MyStc; 
                            //to C2DStatic m_MyStc;
  //}}AFX_DATA

  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(C2DFunctionDlg)
  protected:
  virtual void DoDataExchange(CDataExchange* pDX);  
           // DDX/DDV support
  //}}AFX_VIRTUAL

// Implementation
protected:
  HICON m_hIcon;

  // Generated message map functions
  //{{AFX_MSG(C2DFunctionDlg)
  afx_msg void OnPaint();
  afx_msg HCURSOR OnQueryDragIcon();
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations 
// immediately before the previous line.

#endif // !defined(AFX_2DFUNCTIONDLG_H__D5D048D5_079A_40BD_
            //86A0_32A26253D2E5__INCLUDED_)

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)");            // Draw y=sin(x)
    m_MyStc.SetInitialValue(-30,30,-10,300);
    DrawFunction(1,"tan(x)");            // Draw y=1+tan(x)^2
    m_MyStc.SetInitialValue(-15,15,-2,2);
    DrawFunction(2,"3*x^2-2*x-4)");        // Draw y=x^3-x^2-4*x+c

Member Functions

/* Set and Convert the scales */
// to Set the scale of the Static Control 
SetInitialValue(float xMin,float xMax,float yMin,float yMax);
// to change the real pixel to Static Pixel
CPoint GetNewPixel(float x,float y);

/* Math Functions */
double  MathFunc(double x);  // return y=f(x)
double FuncDef(double x);    // return y=deferential of f(x)
double FuncInt(double x);    // return y=Integral of f(x)

/* Displaying Functions */
void ShowMathFunc(CDC* pDC); // Show f(x) in static in desired scale
void ShowFuncDef(CDC* pDC);  // Show def f(x) in static in desired scale
void ShowFuncInt(CDC* pDC);  // Show int f(x) in static in desired scale

// to Draw the Function or Deferential or Integral 
bool DrawFunction(int iKind,CString sFormula);

/* String Processing Functions */
// to simaulaShow int f(x) in static in desired scale
bool SetIndexPra(CString StrFormula);
// to seprate String to two strings include Operators ( sAr[100] ) and
// integers ( Ar[100] ) and iAr ( the number of operators )
bool SimString(CString StrEdit);
// to obtain the value of function with sAr,iAr,Ar 
double StrToInt();
// to obtain Special Funnction like sin,log,… from String 
double Specialfunc(CString SpFunc);
// to find next operator after a found operator
int FindNextOperation(CString strEdit,int iIndex);
// to find operator before special functions
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.