最近接手了一個項目,其中涉及到MFC和實時曲線顯示的問題,由于我之前從未接觸過此類技術,現學現搞,把其間用到的覺得對初學者有用的東西,總結一下。
尤其是關于TeeChart控件部分,網上資料零碎,且很多不全面,代碼難以使用。我苦尋數周在外國一些網站上尋到了一些有用的信息,把相關的可運行的代碼示例貼在文中,希望能幫到后來者。
(如有疑問可在帖子后面留言)?
?
MFC部分:
一、
分割窗體
新建一個單文檔的MFC工程(注意在向導中設置窗口最大化和分割窗口支持)。
新建兩個對話框,用于分割窗口
【注意】對話框的樣式(Style)屬性改為下層(Child),邊框(Border)屬性改為None,最開始沒有改這個,程序運行的時候報錯了。
【注意】將兩個對話框生成從CFormView派生的類。
?
在CMainFrame的OnCreateClient中添加
【例1】把框架分割成兩列,右邊的一列和對話框綁定。
[cpp] view plaincopyprint?
m_SplitterWnd.CreateStatic(this,1,2));???m_SplitterWnd.SetColumnInfo(0,?200,?0)?;???CRect?rect;??GetClientRect(&rect);??????????if(!m_SplitterWnd.CreateView(0,0,RUNTIME_CLASS(CMyView),CSize(rect.Width()/4*3,rect.Height()),pContext)||?????????!m_SplitterWnd.CreateView(0,1,RUNTIME_CLASS(CMyDlg),???????????????????????????CSize(rect.Width()/4,rect.Height()),pContext))??{???????return?FALSE;??}?????return?TRUE;??
m_SplitterWnd.CreateStatic(this,1,2)); //把此框架窗口分割成1行2列。
m_SplitterWnd.SetColumnInfo(0, 200, 0) ; //設置第0列的最大寬度為200,最小寬度為0 (此句話非常重要)
CRect rect;
GetClientRect(&rect);//第1行第1列的窗口與CMyView綁定。其寬度為框架寬度的3/4.高度與框架的高度一致
if(!m_SplitterWnd.CreateView(0,0,RUNTIME_CLASS(CMyView),CSize(rect.Width()/4*3,rect.Height()),pContext)||//第1行第2列的窗口與我們的對話框CMyDlg綁定。其寬度為框架寬度的1/4.!m_SplitterWnd.CreateView(0,1,RUNTIME_CLASS(CMyDlg),CSize(rect.Width()/4,rect.Height()),pContext))
{ return FALSE;
}return TRUE;
?
【例2】在分割后的子窗口上繼續分割
在CMainFrame中添加兩個成員變量,類型為CSplitterWnd,如下所示
CSplitterWnd m_splitterWnd1;
CSplitterWnd m_splitterWnd2;
?
添加虛函數virtualBOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
程序代碼修改部分如下:
[cpp] view plaincopyprint?
BOOLCMainFrame::OnCreateClient(LPCREATESTRUCT?lpcs,?CCreateContext*?pContext)??{??????????if(m_splitterWnd1.CreateStatic(this,1,2)==NULL)??????????return?FALSE;?????????????m_splitterWnd1.SetColumnInfo(0,?200,?0)?;??????????????m_splitterWnd1.CreateView(0,0,RUNTIME_CLASS(CsplitterwndView),CSize(600,500),pContext);?????????????if(m_splitterWnd2.CreateStatic(&m_splitterWnd1,2,1,WS_CHILD|WS_VISIBLE,?????????m_splitterWnd1.IdFromRowCol(0,?1))==NULL)?????????return?FALSE;?????????????m_splitterWnd2.CreateView(0,0,RUNTIME_CLASS(CForm1),CSize(0,300),pContext);?????????????m_splitterWnd2.CreateView(1,0,RUNTIME_CLASS(CForm2),CSize(0,0),pContext);???????return?TRUE;??}????
BOOLCMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{//創建一個靜態分欄窗口,分為一行二列if(m_splitterWnd1.CreateStatic(this,1,2)==NULL)return FALSE;//設置分割窗口的大小***m_splitterWnd1.SetColumnInfo(0, 200, 0) ; //設置第0列的最大寬度為200,最小寬度為0//將CCSplitterWndView連接到0行0列窗格上m_splitterWnd1.CreateView(0,0,RUNTIME_CLASS(CsplitterwndView),CSize(600,500),pContext);//將第0行1列再分開2行1列if(m_splitterWnd2.CreateStatic(&m_splitterWnd1,2,1,WS_CHILD|WS_VISIBLE,m_splitterWnd1.IdFromRowCol(0, 1))==NULL)return FALSE;//將FormView1類連接到第二個分欄對象的0行0列m_splitterWnd2.CreateView(0,0,RUNTIME_CLASS(CForm1),CSize(0,300),pContext);//因為是上下分割,故系統不關注寬度,只看高度,故寬度可以為0//將FormView2類連接到第二個分欄對象的1行0列m_splitterWnd2.CreateView(1,0,RUNTIME_CLASS(CForm2),CSize(0,0),pContext); //此高度為0,意為分割后剩下的高度 就是它的了。return TRUE;
}
//CsplitterwndView、CForm1、CForm2都是我們自定義的類,可以把他們換成對話框或表單等。
?
//初始左右分割框架,要調用函數SetColumnInfo來設定分割線位置
對分割出來的一列再進行分割,則是由CreateView中CSize的高度來確定分割線位置
?
*總結:
* 給框架窗口添加靜態拆分視圖的過程如下:
*? 1.?給框架窗口類添加一個CsplitterWnd數據成員。????????????????????
*? 2.?覆蓋框架窗口的OnCreateClient函數,并調用CsplitterWnd::CreateStatic來創建靜態拆分視圖。?????? ????
*? 3.?使用CsplitterWnd::CreateView在每個靜態拆分窗口的窗格中創建視圖
*??????使用靜態拆分窗口的一個優點是由于您自己給窗格添加視圖,所以可以控制放入視圖的種類
?
二、
添加自定義消息響應
1、在Resource.h中添加
#define WM_MY_MESSAGE (WM_USER+100)
?
2、在CMyView的定義中添加:???? //CMyView是要響應自定義消息的我們的視圖類
//{{AFX_MSG(CMyView)
afx_msg LRESULT OnMyMsg(WPARAM, LPARAM) ;
DECLARE_MESSAGE_MAP()
//}}AFX_MSG
?
3、在CMyView的實現cpp文件中添加
BEGIN_MESSAGE_MAP(CMyView, CFormView)
???//{{AFX_MSG_MAP(CMyView)
???ON_MESSAGE(WM_MY_MESSAGE, OnMyMsg)?//添加消息映射
???//}}AFX_MSG_MAP
END_MESSAGE_MAP()
?
4、實現消息映射函數LRESULT CMyView::OnMyMsg(WPARAM wParam, LPARAM lParam)
?
5、發送消息,觸發消息響應函數
pMyView->PostMessage(WM_MY_MESSAGE,0, 0); ? ?//至于PostMessage和SendMessage的區別 請百度之。
?//pMyView是CMyView類對象的指針。
?
TeeChart部分(以VC++6.0? TeeChart8.0為例)
至于如何獲取TeeChart控件,如何注冊控件,請百度之,網上有很多。
零
在相應的源文件中添加TeeChart的頭文件 (有需要的自己再添加)
#include "tchart.h"
#include "series.h"
#include "valuelist.h"
#include "axes.h"
#include "axis.h"
#include "pen.h"
#include "axislabels.h"
#include "teefont.h"
#include "axistitle.h"
#include "aspect.h"
#include "fastlineseries.h"
#include "titles.h"
#include "fastlineseries.h"
#include "panel.h"
#include "legend.h"
#include "annotationtool.h"
#include "page.h"
#include "strings.h"
#include "gradient.h"
#include "IsoSurfaceSeries.h"
?
一、
在視圖類中動態添加TeeChart控件。(解決手工拖動添加控件,編譯報”Debug Assertion Failed” 錯的問題)
我們添加對話框資源讓其繼承自CFromView。首先手工靜態把控件拖到對話框上,然后建立類向導,生成一個對象m_chart。
?
在主框架CMainFrame::OnCreateClient()或OnCreate()中【看在哪個函數中分割窗口 產生視圖】
???RecalcLayout();?????????? //這一句很重要,沒有它,會報錯。
???pView->OnInitialUpdate();?//pView是我們分割窗口得到的CMyDlgView視圖的指針。
?
在視圖類CMyDlgView中添加OnInitialUpdate()函數
???CRect rect;
???GetClientRect(&rect);??
???m_chart.MoveWindow(&rect, TRUE);
?
在視圖類CMyDlgView中添加WM_CREATE消息響應函數OnCreate()在其中添加
?????? m_chart.Create("",WS_VISIBLE, CRect(0, 0, 0, 0), this, 1234) ;//動態生成控件
?????? m_chart.AddSeries(0);//操作控件
?????? m_chart.Series(0).FillSampleValues(50);
//m_chart是我們的控件TeeChart
?
即可。
//但此為動態添加的控件,所有設置操作都得通過代碼操作。
?
二、
繪制2D曲線
這個在網上有很多資料了。我在這里再簡單總結一下其過程。
?
A、初始化部分:
在TeeChart控件所在的視圖類的OnCreate函數中,進行TeeChart控件的初始化工作。
[cpp] view plaincopyprint?
m_chart.Create("",WS_VISIBLE,?CRect(0,?0,?0,?0),?this,?1234)?;???m_chart.GetLegend().SetVisible(false);??m_chart.GetAspect().SetView3D(FALSE);????????m_chart.GetHeader().GetText().SetItem(0,COleVariant("傳感器實時數據曲線"));????m_chart.GetAxis().GetLeft().GetTitle().SetCaption("數值");??????m_chart.GetPanel().GetGradient().SetVisible(true);??m_chart.GetPanel().GetGradient().SetStartColor(RGB(192,192,192));??m_chart.GetPanel().GetGradient().SetEndColor(RGB(255,255,255));??????m_chart.AddSeries(0);??????m_chart.Series(0).SetColor(RGB(255,0,0));??m_chart.Series(0).GetAsLine().GetLinePen().SetWidth(2);???????m_chart.GetAxis().GetBottom().SetMinMax(0,100);????m_chart.GetAxis().GetBottom().GetLabels().SetValueFormat("0.0");??
m_chart.Create("",WS_VISIBLE, CRect(0, 0, 0, 0), this, 1234) ; //動態創建TeeChart控件m_chart.GetLegend().SetVisible(false);//隱藏圖例m_chart.GetAspect().SetView3D(FALSE); //取消3D顯示//設置圖標標題m_chart.GetHeader().GetText().SetItem(0,COleVariant("傳感器實時數據曲線"));//設置縱軸標題m_chart.GetAxis().GetLeft().GetTitle().SetCaption("數值");//設置漸變背景m_chart.GetPanel().GetGradient().SetVisible(true);m_chart.GetPanel().GetGradient().SetStartColor(RGB(192,192,192));m_chart.GetPanel().GetGradient().SetEndColor(RGB(255,255,255));//添加曲線m_chart.AddSeries(0);//設置曲線屬性m_chart.Series(0).SetColor(RGB(255,0,0));//顏色m_chart.Series(0).GetAsLine().GetLinePen().SetWidth(2); //線型寬度//設置x軸的取值范圍m_chart.GetAxis().GetBottom().SetMinMax(0,100);//設置x軸上值的格式m_chart.GetAxis().GetBottom().GetLabels().SetValueFormat("0.0");
?
B、繪制部分:
在TeeChart控件所在的視圖類的自定義消息響應函數OnMyMsg中,或是在定時器中,添加:
?
[cpp] view plaincopyprint?
COleDateTimeCurTime?=?COleDateTime::GetCurrentTime();??COleDateTimeSpantmSpan?=?COleDateTimeSpan(0,0,1,0);???CStringcsTime?;??csTime=?CurTime.Format("%H:%M:%S");???????m_chart.Series(0).Add(yVal,?csTime,RGB(255,0,0));???CurTime+=?tmSpan;????m_chart.Series(0).RefreshSeries();??if(m_chart.Series(0).GetCount()?>?100)??{????m_chart.GetAxis().GetBottom().Scroll(1.0,true);???}??
COleDateTimeCurTime = COleDateTime::GetCurrentTime();COleDateTimeSpantmSpan = COleDateTimeSpan(0,0,1,0); //1sCStringcsTime ;csTime= CurTime.Format("%H:%M:%S"); //獲取當前時間//在CMyView中畫曲線m_chart.Series(0).Add(yVal, csTime,RGB(255,0,0)); //第一個參數是y軸值,第二個參數是對應的x軸的標簽值(此為當前時間字符串),第三個參數是所繪點的顏色。CurTime+= tmSpan;m_chart.Series(0).RefreshSeries();if(m_chart.Series(0).GetCount() > 100){m_chart.GetAxis().GetBottom().Scroll(1.0,true); //x坐標軸一次移動1格}
?
由于TeeChart繪制曲線點的函數Add,每調用一次才繪制一次,故需要有外部消息激發消息響應函數,才能把曲線動態繪制出來。
可以用設置定時器和自定義消息響應函數的方式來實現。(定時器比較簡單,消息響應函數上面MFC部分已經講過)
三、
繪制3D曲線
解決TeeChart8中繪制3D圖形報”Invalid?class?typecast” 錯的問題。
A、在承載TeeChart的對話框類Dlg的類定義中,添加:VARIANT SeriesIndex;
?
B、在類的相關方法中繪制,添加代碼:
m_chart.RemoveAllSeries();
?
//下面的設置很重要(沒有的話,會出錯)
SeriesIndex.vt=VT_INT;
SeriesIndex.intVal=m_chart.AddSeries(scWaterfall);//scWaterfall=33瀑布圖的編號
m_chart.Series(0).GetAsWaterfall().SetIrregularGrid(true);
?
m_chart.Series(0).GetAsWaterfall().AddXYZ(x,y, z, NULL, RGB(255,0,0));
?
(TeeChart的3D圖有很多種,上面是以瀑布圖為例的,其他圖種的編號如下:)
const unsigned long scLine = 0;
const unsigned long scBar = 1;
const unsigned long scHorizBar = 2;
const unsigned long scArea = 3;
const unsigned long scPoint = 4;
const unsigned long scPie = 5;
const unsigned long scFastLine = 6;
const unsigned long scShape = 7;
const unsigned long scGantt = 8;
const unsigned long scBubble = 9;
const unsigned long scArrow = 10;
const unsigned long scCandle = 11;
const unsigned long scPolar = 12;
const unsigned long scSurface = 13;
const unsigned long scVolume = 14;
const unsigned long scErrorBar = 15;
const unsigned long scBezier = 16;
const unsigned long scContour = 17;
const unsigned long scError = 18;
const unsigned long scPoint3D = 19;
const unsigned long scRadar = 20;
const unsigned long scClock = 21;
const unsigned long scWindRose= 22;?
const unsigned long scBar3D = 23;?
const unsigned long scImageBar = 24;?
const unsigned long scDonut = 25;
const unsigned long scTriSurface = 26;
const unsigned long scBox = 27;
const unsigned long scHorizBox = 28;
const unsigned long scHistogram = 29;
const unsigned long scColorGrid = 30;
const unsigned long scBarJoin = 31;
const unsigned long scHighLow = 32;
const unsigned long scWaterfall = 33;
const unsigned long scSmith = 34;
const unsigned long scPyramid = 35;
const unsigned long scMap = 36;
const unsigned long scHorizLine = 37;
const unsigned long scFunnel = 38;
const unsigned long scCalendar = 39;
const unsigned long scHorizArea = 40;
const unsigned long scPointFigure = 41;
const unsigned long scGauge = 42;
const unsigned long scVector3D = 43;
const unsigned long scTower = 44;
const unsigned long scPolarBar = 45;
const unsigned long scBubble3D = 46;
const unsigned long scHorizHistogram = 47;
const unsigned long scVolumePipe = 48;
const unsigned long scIsoSurface = 49;
const unsigned long scDarvas = 50;
const unsigned long scHighLowLine = 51;
const unsigned long scPolarGrid = 52;
const unsigned long scDeltaPoint = 53;
const unsigned long scImagePoint = 54;
const unsigned long scOrganizational = 55;
const unsigned long scWorld = 56;
const unsigned long scTagCloud = 57;
const unsigned long scKagi = 58;
const unsigned long scRenko = 59;
const unsigned long scNumericGauge = 60;
const unsigned long scLinearGauge = 61;
const unsigned long scCircularGauge = 62;
const unsigned long scBigCandle = 63;
const unsigned long scLinePoint = 64;
?
//如需要相關圖種,只需把上面代碼
SeriesIndex.intVal= m_chart.AddSeries(scWaterfall);//把scWaterfall改為你所需圖種的編號
m_chart.Series(0).GetAsWaterfall().SetIrregularGrid(true);// GetAsWaterfall改為你所需圖種的相關函數名
?
----------------
一個完整的例子:
A、在CMyView(承載TeeChart的對話框視圖)的定義中,添加VARIANTSeriesIndex;
?
B、在int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)函數中:
[cpp] view plaincopyprint?
int?CMyView::OnCreate(LPCREATESTRUCTlpCreateStruct)??{?????????if(CFormView::OnCreate(lpCreateStruct)?==?-1)????????????????return-1;??????????????????????????m_chart.Create("",WS_VISIBLE,?CRect(0,?0,?0,?0),?this,?1234)?;?????????m_chart.GetLegend().SetVisible(false);?????????m_chart.GetAspect().SetView3D(true);??????????????m_chart.GetAxis().GetDepth().SetVisible(TRUE);???????????m_chart.GetAxis().GetDepth().GetLabels().SetVisible(TRUE);???????????m_chart.GetAxis().GetDepth().GetLabels().SetStyle(0);???????????????????????m_chart.GetPanel().GetGradient().SetVisible(true);?????????m_chart.GetPanel().GetGradient().SetStartColor(RGB(192,192,192));?????????m_chart.GetPanel().GetGradient().SetEndColor(RGB(255,255,255));?????????????????????m_chart.GetHeader().GetText().SetItem(0,COleVariant("瀑布圖"));?????????????????????m_chart.RemoveAllSeries();????????????SeriesIndex.vt=VT_INT;?????????SeriesIndex.intVal=m_chart.AddSeries(49);????????????m_chart.Series(0).GetAsIsoSurface().SetIrregularGrid(true);?????????????????????m_chart.Series(0).SetColor(RGB(255,0,0));????????????m_chart.GetAxis().GetBottom().SetMinMax(0,100);?????????m_chart.GetAxis().GetBottom().GetLabels().SetValueFormat("0.0");????????????m_chart.GetAspect().SetChart3DPercent(30);?????????????????return0;??}?????
int CMyView::OnCreate(LPCREATESTRUCTlpCreateStruct)
{if(CFormView::OnCreate(lpCreateStruct) == -1)return-1;//TODO: Add your specialized creation code herem_chart.Create("",WS_VISIBLE, CRect(0, 0, 0, 0), this, 1234) ;m_chart.GetLegend().SetVisible(false);//隱藏圖例m_chart.GetAspect().SetView3D(true); //3D顯示m_chart.GetAxis().GetDepth().SetVisible(TRUE); //顯示Z軸m_chart.GetAxis().GetDepth().GetLabels().SetVisible(TRUE); //顯示Z軸上的坐標m_chart.GetAxis().GetDepth().GetLabels().SetStyle(0); //設置顯示坐標的風格//設置漸變背景m_chart.GetPanel().GetGradient().SetVisible(true);m_chart.GetPanel().GetGradient().SetStartColor(RGB(192,192,192));m_chart.GetPanel().GetGradient().SetEndColor(RGB(255,255,255));//設置圖標標題m_chart.GetHeader().GetText().SetItem(0,COleVariant("瀑布圖"));//開始繪制3Dm_chart.RemoveAllSeries();SeriesIndex.vt=VT_INT;SeriesIndex.intVal=m_chart.AddSeries(49);// 49號圖種,IsoSurface類型3Dm_chart.Series(0).GetAsIsoSurface().SetIrregularGrid(true);//設置曲線顏色m_chart.Series(0).SetColor(RGB(255,0,0));m_chart.GetAxis().GetBottom().SetMinMax(0,100);m_chart.GetAxis().GetBottom().GetLabels().SetValueFormat("0.0");m_chart.GetAspect().SetChart3DPercent(30);//調整3D縱深比return0;
}
C、在自定義的消息處理函數中:
[cpp] view plaincopyprint?
LRESULT?CMyView::OnMyMsg(WPARAM?wParam,LPARAM?lParam)??{?????????staticdouble?xVal?=?.0?;?????????doubleyVal?=?.0?;?????????CTSDoc*?pDoc?=??(CTSDoc*)(this->GetDocument())?;?????????yVal=?pDoc->clientDataBuff[0]?;????????????for(int?zVal=0;?zVal<50;?zVal++)?????????m_chart.Series(0).GetAsIsoSurface().AddXYZ(xVal,yVal,?(double)zVal,??????????????????????????????????????????????????NULL,RGB(255,(int)(yVal*30)+160,0));?????????m_chart.Series(0).RefreshSeries();?????????if(m_chart.Series(0).GetCount()?>?100)?????????{???????????m_chart.GetAxis().GetBottom().Scroll(1.0,true);??????????}????????????xVal++;?????????return0?;??}?????
LRESULT CMyView::OnMyMsg(WPARAM wParam,LPARAM lParam)
{staticdouble xVal = .0 ;doubleyVal = .0 ;CTSDoc* pDoc = (CTSDoc*)(this->GetDocument()) ;yVal= pDoc->clientDataBuff[0] ;for(int zVal=0; zVal<50; zVal++)m_chart.Series(0).GetAsIsoSurface().AddXYZ(xVal,yVal, (double)zVal,NULL,RGB(255,(int)(yVal*30)+160,0));m_chart.Series(0).RefreshSeries();if(m_chart.Series(0).GetCount() > 100){m_chart.GetAxis().GetBottom().Scroll(1.0,true); //坐標軸一次移動1格}xVal++;return0 ;
}
四、
用AddArray一次把數組中的值繪制出來
1、先在TeeChart所在的類中 添加:
COleSafeArray XValues;
COleSafeArray YValues;
?
2、再在TeeChart的初始化設置函數添加:
[cpp] view plaincopyprint?
DWORD?numElements[]?=?{200000};???????XValues.Create(VT_R8,?1,?numElements);YValues.Create(VT_R8,1,?numElements);???????long?index;??for(index=0;?index<200000;?index++)?{?????double?val?=?(double)index;?????XValues.PutElement(&index,?&val);??};????????????for(index=0;?index<200000;?index++)?{?????double?val?=?rand()%100;?????YValues.PutElement(&index,?&val);??};??
DWORD numElements[] = {200000};// Create the safe-arrays...
XValues.Create(VT_R8, 1, numElements);YValues.Create(VT_R8,1, numElements);// Initialize them with values...
long index;
for(index=0; index<200000; index++) {double val = (double)index;XValues.PutElement(&index, &val);
};for(index=0; index<200000; index++) {double val = rand()%100;YValues.PutElement(&index, &val);
};?
3、在相應的位置添加:
m_chart.Series(0).AddArray(200000,YValues,XValues);
總結
以上是生活随笔為你收集整理的MFC TeeChart 用法整理二的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。