我自己写的坐标图,可以定制坐标并切换,也可以用不同颜色画直线和曲线

类别:编程语言 点击:0 评论:0 推荐:

"CGraph.h"

#if !defined(AFX_GRAPH_H__F235CDCF_7878_44AB_9A72_DDB5384D32BF__INCLUDED_)
#define AFX_GRAPH_H__F235CDCF_7878_44AB_9A72_DDB5384D32BF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Graph.h : header file
//Create by yangyihong,contact:[email protected]

/////////////////////////////////////////////////////////////////////////////
// CGraph window


typedef struct tagGraphStr {
 char title[20];//标题
 char xstr[20];//横坐标名称
 char ystr[20];//纵坐标名称
 double dORGX,dMAXX;//横坐标原点,最大值
 double dORGY,dMAXY;//纵坐标原点,最大值
}GSTR;
static GSTR gstr[]={
 {"硅光电池","波长(nm)","光谱响应度(A/W)",380,600,0,4},
 {"光电二极管","波长(nm)","光谱响应度(A/W)",380,600,0,4},
 {"光电三极管","波长(nm)","光谱响应度(A/W)",380,600,0,4},
 {"光敏电阻","波长(nm)","光谱响应度(A/W)",380,600,0,4},

 {"PMT阴极特性","电压(V)","电流(nA)",0,12,0,5},
 {"PMT阳极特性","电压(V)","电流(nA)",0,12,0,5},
 {"PMT放大倍数","电压","放大倍数",0,12,0,5},

 {"APD特性","电压(V)","电流(nA)",0,12,0,500}

};
class CGraph : public CWnd
{
// Construction
public:
 CGraph();

// Attributes
public:
 double m_dORGX , m_dORGY ;
 double m_dMAXX , m_dMAXY ;
 double  m_dDIVX , m_dDIVY ;//横坐标和纵坐标分度

// Operations
public:

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CGraph)
 public:
 virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
 //}}AFX_VIRTUAL

// Implementation
public:
 void SwitchGraph(int index);//切换坐标
 void DrawLine (double X1,double Y1,double X2,double Y2,COLORREF cr);//画线
 void DrawPoint(double X,double Y);//画点
 void SetXScale(double dOrgX,double dMaxX);//设定X坐标分度
 void SetYScale(double dOrgY, double dMaxY);//设定Y坐标分度
 void SetTitle(int nIndex);//设定标题
 virtual ~CGraph();

 // Generated message map functions
protected:
 //{{AFX_MSG(CGraph)
 afx_msg void OnPaint();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
private:
 GSTR *m_pGstr;
 void OnDraw ();
};

/////////////////////////////////////////////////////////////////////////////

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

#endif // !defined(AFX_GRAPH_H__F235CDCF_7878_44AB_9A72_DDB5384D32BF__INCLUDED_)

"CGraph.c"

// Graph.cpp : implementation file
//2005/4/20
//Create by yangyihong,contact:[email protected]

#include "stdafx.h"
#include "CSY.h"
#include "Graph.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ORGX  65 
#define ORGY  430
#define MAXX  550
#define MAXY  400//坐标在窗口上的位置
#define nX    100//X分度数
#define nY    100//Y分度数

/////////////////////////////////////////////////////////////////////////////
// CGraph

CGraph::CGraph()
{
  SetTitle(0);
  SetXScale(380,1000);
  SetYScale(0,10);
}

CGraph::~CGraph()
{
}


BEGIN_MESSAGE_MAP(CGraph, CWnd)
 //{{AFX_MSG_MAP(CGraph)
 ON_WM_PAINT()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CGraph message handlers

void CGraph::OnDraw()
{
 int i;
 int nORGX,nORGY,nMAXX,nMAXY;
 CRect rc;
 GetClientRect(&rc);

 CDC *pDC=GetDC();
 pDC->SetBkMode(TRANSPARENT);
 pDC->SetMapMode(MM_ANISOTROPIC);
 pDC->SetWindowExt(640, 480);
 pDC->SetViewportExt(rc.right, rc.bottom);
 
 nORGX = ORGX, nORGY = ORGY;
 nMAXX = MAXX, nMAXY = MAXY;


 CFont fnt;
 CFont *poldfnt;
 LOGFONT logFont;
 memset(&logFont,0,sizeof(LOGFONT));
 logFont.lfHeight=12;logFont.lfWidth = 10;
 
 if( fnt.CreateFontIndirect(&logFont))
  poldfnt = pDC->SelectObject(&fnt);

 // 显示背景虚线
 CPen newPen(PS_DOT, 1, RGB(0, 0, 0));
 CPen* pOldPen = pDC->SelectObject(&newPen);
 for( i = 0; i < nY/10; i++)
 {
   CString str;
   str.Format("%3.1f",m_dORGY+i*m_dDIVY*10);
   pDC->MoveTo(nORGX, nORGY - i*nMAXY/10);
   pDC->LineTo(nORGX + nMAXX, nORGY - i*nMAXY/10);
   pDC->TextOut(nORGX-50, nORGY - i*nMAXY/10-4,str);

   for (int j=0;j<10;j++){
    CPen newPen(PS_SOLID, 1, RGB(0, 0, 0));
    CPen* pOldPen = pDC->SelectObject(&newPen);
    pDC->MoveTo(nORGX,nORGY - i*nMAXY/10-j*nMAXY/nY);
    if(j==5) {
     pDC->LineTo(nORGX+4+4,nORGY - i*nMAXY/10-j*nMAXY/nY);
    }else{
    pDC->LineTo(nORGX+4,nORGY - i*nMAXY/10-j*nMAXY/nY);
    }
    pDC->SelectObject(pOldPen);
   }
 }
 CString str;str.Format("%3.1f",i*m_dDIVY*10);
 pDC->TextOut(nORGX-50, nORGY - i*nMAXY/10-4,str);

 for(i = 0; i < nX/10; i++)
 {
   CString str;
   str.Format("%3.1f",m_dORGX+i*m_dDIVX*10);
   pDC->MoveTo(nORGX + i*nMAXX/10, nORGY);
   pDC->LineTo(nORGX + i*nMAXX/10, nORGY - nMAXY);
   pDC->TextOut(nORGX + i*nMAXX/10-8, nORGY+4,str);

   for (int j=0;j<10;j++){
    CPen newPen(PS_SOLID, 1, RGB(0, 0, 0));
    CPen* pOldPen = pDC->SelectObject(&newPen);
    pDC->MoveTo(nORGX + i*nMAXX/10+j*nMAXX/100,nORGY);
    if(j==5){
     pDC->LineTo(nORGX + i*nMAXX/10+j*nMAXX/nX,nORGY - 3 -3);
    }else{
    pDC->LineTo(nORGX + i*nMAXX/10+j*nMAXX/nX,nORGY - 3);
    }
    pDC->SelectObject(pOldPen);
   }
 }
 str.Format("%3.1f",m_dORGX+i*m_dDIVX*10);
 pDC->TextOut(nORGX + i*nMAXX/10-8, nORGY+4,str);

 pDC->SelectObject(pOldPen);
 
 pDC->MoveTo(nORGX, nORGY);
 pDC->LineTo(nORGX, nORGY - nMAXY);

 pDC->MoveTo(nORGX + nMAXX, nORGY);
 pDC->LineTo(nORGX + nMAXX, nORGY - nMAXY);

 pDC->MoveTo(nORGX, nORGY);
 pDC->LineTo(nORGX + nMAXX, nORGY);

 pDC->MoveTo(nORGX, nORGY - nMAXY);
 pDC->LineTo(nORGX + nMAXX, nORGY - nMAXY);

 pDC->TextOut(nORGX+nMAXX/2-10,nORGY+20,m_pGstr->title);
 pDC->TextOut(nORGX-10,nORGY-nMAXY-20,m_pGstr->ystr);
 pDC->TextOut(nORGX+nMAXX-60,nORGY+20,m_pGstr->xstr);

 if(poldfnt!=NULL)
  pDC->SelectObject(poldfnt);
}

void CGraph::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 
 // TODO: Add your message handler code here
 OnDraw ();
 // Do not call CWnd::OnPaint() for painting messages
}

BOOL CGraph::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
 return CWnd::Create(AfxRegisterWndClass(CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW, NULL, (HBRUSH)(COLOR_BTNFACE+1),NULL),
      NULL,
      dwStyle,
      rect,
      pParentWnd,
      nID,
      pContext);
}

void CGraph::SetTitle(int nIndex)
{
 m_pGstr= &gstr[nIndex];
}

void CGraph::SetXScale(double dOrgX, double dMaxX)
{
 m_dMAXX = dMaxX;
 m_dORGX = dOrgX;
 m_dDIVX = m_dMAXX / nX;
}
void CGraph::SetYScale(double dOrgY, double dMaxY)
{
 m_dMAXY = dMaxY;
 m_dORGY = dOrgY;
 m_dDIVY = m_dMAXY / nY;
}

void CGraph::DrawPoint(double X, double Y)
{
 CRect rc;
 GetClientRect(&rc);

 CDC *pDC=GetDC();
 pDC->SetMapMode(MM_ANISOTROPIC);
 pDC->SetWindowExt(640, 480);
 pDC->SetViewportExt(rc.right, rc.bottom);
 int tmpX,tmpY;

 tmpX = (int) (( X-m_dORGX) * MAXX/m_dMAXX + ORGX );
 tmpY = (int) ( ORGY-(Y-m_dORGY) * MAXY/m_dMAXY  );

 pDC->MoveTo(tmpX-5,tmpY-5);
 pDC->SetPixel(tmpX,tmpY,RGB(255,0,0));
}

void CGraph::DrawLine(double X1, double Y1, double X2, double Y2,COLORREF cr)
{
 CRect rc;
 GetClientRect(&rc);

 CDC *pDC=GetDC();
 pDC->SetMapMode(MM_ANISOTROPIC);
 pDC->SetWindowExt(640, 480);
 pDC->SetViewportExt(rc.right, rc.bottom);
 int tmpX1,tmpY1,tmpX2,tmpY2;

 tmpX1 = (int) (( X1-m_dORGX) * MAXX/m_dMAXX + ORGX );
 tmpY1 = (int) ( ORGY-(Y1-m_dORGY) * MAXY/m_dMAXY  );
 tmpX2 = (int) (( X2-m_dORGX) * MAXX/m_dMAXX + ORGX );
 tmpY2 = (int) ( ORGY-(Y2-m_dORGY) * MAXY/m_dMAXY  );
 CPen pn(PS_SOLID, 3, cr);
 CPen *poldpn;
 poldpn = pDC->SelectObject(&pn);
 
 pDC->MoveTo(tmpX1,tmpY1);
 pDC->LineTo(tmpX2,tmpY2);
 pDC->SelectObject(poldpn);
}

void CGraph::SwitchGraph(int index)
{
 if (index>=sizeof(gstr)){
  DBG("下标超界");return;
 }
 SetTitle(index);
 SetXScale(m_pGstr->dORGX,m_pGstr->dMAXX);
 SetYScale(m_pGstr->dORGY,m_pGstr->dMAXY);
 
 CRect rc;
 GetClientRect(rc);
 InvalidateRect(rc);
 
}

本文地址:http://com.8s8s.com/it/it22147.htm