65.9K
CodeProject 正在变化。 阅读更多。
Home

2DStatic

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.54/5 (11投票s)

2005年4月7日

1分钟阅读

viewsIcon

46038

downloadIcon

1630

一篇关于在静态控件上绘制字符串公式的文章。

引言

本文档描述了如何在静态控件中添加一个用于显示数学函数(或其微分或积分)的控件。该控件基于CStatic。首先,将2DStatic.h2DStatic.cpp文件添加到您的项目中。在工作区窗口中选择“资源”选项卡,然后选择要添加显示静态控件的对话框。从“控件工具箱”中选择“静态控件”并在对话框上绘制它(图1)。将其ID从IDC_STATIC更改为IDC_MyStc

图1 - 将静态控件和按钮控件添加到您的对话框。

现在是时候为您的对话框类添加一个成员变量了。使用“类向导”来完成它。图2显示了如何操作。在这种情况下,我们添加了一个类型为CStatic的成员变量m_MyStc

图2 - 将成员变量添加到您的对话框类。

好的,打开您的对话框类头文件,在类定义顶部添加以下行:#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_)

现在您可以使用两个函数SetInitialValueDrawFunction绘制任何您想要的函数。例如:

    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

成员函数

/* 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);

注意

感谢Abbas Riazi先生和Mehdi Bonvori先生的指导。这个类具有2D绘制函数,并且我尝试在其中实现强大的字符串处理。但我确信在使用它时会遇到很多问题。如果您能提供关于问题的反馈,我将不胜感激。

© . All rights reserved.