CChart本来就是设计来用在窗口程序里的,因此,在Windows窗口程序里,使用CChart比在控制台程序更直接。
在窗口程序里,使用CChart仍然可以采用上节课的方法,建立弹出窗口用于绘图,由于窗口程序自带消息循环,可以把上节课提到的SimpleMsgLoop接口去掉,代码更简洁。因为这种方法的代码和上节课几乎一样,这里就不赘述了。
本节课介绍直接在Windows程序的主窗口绘图。
实际上,方法和前面第六课几乎完全一样,区别有两点。一是不能使用C++类,需要采用C风格代码,二是需要翻译成Fortran语言。
下面开始。
第一步,建立一个Fortran Windows Application,名字为LessonA36,选择A simple Single Document Interface (SDI),拷贝CChart.lib,CChart.dll到LessonA36文件夹,并把CChart.lib添加到项目里面。
第二步,打开LessonA36.f90文件,找到MainWndProc函数,在! Variables这一行的前面,添加我们需要导入到CChart函数。
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 Attach(hWnd)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?Attach@Classless@@YAHPAUHWND__@@@Z'::Attach
!DEC$ATTRIBUTES VALUE::hWnd
integer hWnd
end function Attach
end interface
interface
integer*1 function SetType(chartType)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetType@Classless@@YA_NH@Z'::SetType
!DEC$ATTRIBUTES VALUE::chartType
integer chartType
end function SetType
end interface
interface
integer function AddPie(pie)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?AddPie@Classless@@YAHN@Z'::AddPie
!DEC$ATTRIBUTES VALUE::pie
double precision pie
end function AddPie
end interface
interface
subroutine SetPieTitle(title, pieIndex)
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?SetPieTitle@Classless@@YAXPBDH@Z'::SetPieTitle
!DEC$ATTRIBUTES VALUE::title, pieIndex
integer title, pieIndex
end subroutine SetPieTitle
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 ReDraw()
!DEC$ATTRIBUTES DLLIMPORT,ALIAS:'?ReDraw@Classless@@YAXXZ'::ReDraw
end subroutine ReDraw
end interface
这里导入了CreateChart,DestroyChart,Attach,SetType,AddPie,SetPieTitle,SetTitle,ReDraw这些函数。
第三步,在MainWndProc函数里面,找到select case ( mesg )这一行,在其下面增加如下代码。
! WM_CREATE
case (WM_CREATE)
call CreateChart()
call Attach(hWnd)
call SetType(1)
call AddPie(3)
call AddPie(2)
call AddPie(1)
call AddPie(6)
call AddPie(7)
call SetPieTitle(LOC("赵敏"C), 0)
call SetPieTitle(LOC("周芷若"C), 1)
call SetPieTitle(LOC("殷离"C), 2)
call SetPieTitle(LOC("小昭"C), 3)
call SetPieTitle(LOC("黄衫女"C), 4)
call SetTitle(LOC("Fortran 窗口标题"C))
MainWndProc = 0
return
第四步,在case (WM_DESTROY)的下一行,增加一句代码。
call DestroyChart()
打完收工!效果如图。
下课了!!!