这节课继续介绍CChart在Fortran窗口程序里面的应用,作为演示,将在对话框窗口里绘制柱图。
话不多说,直接开始。
第一步,建立一个Fortran Windows Application,名字为LessonA36,选择A simple Dialog Based Application,拷贝CChart.lib,CChart.dll到LessonA36文件夹,并把CChart.lib添加到项目里面。

第二步,在资源管理器中,打开主对话框,删除“TODO: Place dialog controls here.”标签,增加一个Picture控件,ID为IDC_CANVAS,同时调整对话框和控件的大小。

第三步,打开LessonA37.f90文件,找到SUBROUTINE LessonA37Sub( dlg, id, callbacktype )这个过程,在implicit none这一行后面,增加一行代码。
include 'resource.fd'
这是为了在过程中使用对话框及控件的ID
第四步,在上一行代码的后面,增加导入接口的代码。
interface
subroutine CreateChart()
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?CreateChart@Classless@@YAXXZ'::CreateChart
end subroutine CreateChart
end interface
interface
subroutine DestroyChart()
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?DestroyChart@Classless@@YAXXZ'::DestroyChart
end subroutine DestroyChart
end interface
interface
integer function AttachDlg(hDlg, nCtrlID, nType)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?AttachDlg@Classless@@YAHPAUHWND__@@IH@Z'::AttachDlg
!DEC$ATTRIBUTES VALUE::hDlg, nCtrlID, nType
integer hDlg, nCtrlID, nType
end function AttachDlg
end interface
interface
integer function AddStems(pData, nLen, title)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?AddStems@Classless@@YAHPANHPAD@Z'::AddStems
!DEC$ATTRIBUTES VALUE::pData, nLen, title
integer pData, nLen, title
end function AddStems
end interface
interface
subroutine SetStemLabel(label, which)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetStemLabel@Classless@@YAXPBDH@Z'::SetStemLabel
!DEC$ATTRIBUTES VALUE::label, which
integer label, which
end subroutine SetStemLabel
end interface
interface
subroutine SetTitle(title)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetTitle@Classless@@YAXPBD@Z'::SetTitle
!DEC$ATTRIBUTES VALUE::title
integer title
end subroutine SetTitle
end interface
interface
subroutine SetLegendShow(bShow, nPlotIndex)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetLegendShow@Classless@@YAX_NH@Z'::SetLegendShow
!DEC$ATTRIBUTES VALUE::bShow, nPlotIndex
integer*1 bShow
integer nPlotIndex
end subroutine SetLegendShow
end interface
导入方法和前面的课程完全一样,这里导入了CreateChart,DestroyChart,AttachDlg,AddStems,SetStemLabel,SetTitle,SetLegendShow这7个函数。
第五步,在integer id, callbacktype这一行下面,再增加一个双精度数组。
double precision pData(5)
第六步,在if (callbacktype == dlg_destroy) then这一行的上面,增加对话框初始化代码。
if (callbacktype == dlg_init) then
call CreateChart()
call AttachDlg(dlg%hwnd, IDC_CANVAS, 2)
pData(1) = 7
pData(2) = 6
pData(3) = 1
pData(4) = 2
pData(5) = 4
call AddStems(LOC(pData), 5, LOC("序列1"C))
pData(1) = 3
pData(2) = 7
pData(3) = 5
pData(4) = 4
pData(5) = 2
call AddStems(LOC(pData), 5, LOC("序列2"C))
call SetStemLabel(LOC("张"C), 0)
call SetStemLabel(LOC("王"C), 1)
call SetStemLabel(LOC("李"C), 2)
call SetStemLabel(LOC("赵"C), 3)
call SetStemLabel(LOC("陈"C), 4)
call SetLegendShow(1, 0)
call SetTitle(LOC("Fortran对话框标题"))
endif
注意到Fortran数组起始编号为1,而C++数组起始编号为0,混合编程时,这要特别小心。上面的代码里面,两者都有。
第七步,在if (callbacktype == dlg_destroy) then的下一行,增加代码。
call DestroyChart()
终于完成了,结果如图。

效果还是不错的!

本文介绍CChart在Fortran窗口程序中的应用,以在对话框窗口绘制柱图为例。详细说明了建立Fortran Windows Application、调整对话框和控件大小、添加代码、导入接口函数等步骤,还提醒了Fortran与C++数组编号差异在混合编程时需注意。
3325

被折叠的 条评论
为什么被折叠?



