在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

老資格的Delphi程序員都知道,用VCL庫編寫圖形界面是非常方便的,而LCL庫就是受VCL庫啟發而來形成的免費版本的圖形界面開發庫。感謝Github上的ying32提供的Govcl庫,使得我們現在可以使用Gox語言(Goxlang)結合LCL庫進行GUI圖形界面的編程,下面我們就來看一個具體的實例。

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

在Gox語言簡介一文中,我們已經介紹過使用基於OpenGL技術的GUI圖形界面庫Giu來開發圖形界面的方式,而使用LCL庫開發則更貼近傳統程序員的圖形界面構建思路,我們來看用LCL庫實現與之前的例子功能一致的小型計算器的程序代碼。


<code>lcl = 

import

("lcl") errT = lcl.

InitLCL

()

if

errT !=

nil

{ plerr(errT) exit() } application = lcl.

GetApplication

() application.

Initialize

() application.

SetTitle

(

"Calculator with LCL"

) application.

SetMainFormOnTaskBar

(

true

) mainForm = application.

CreateForm

() mainForm.

SetWidth

(

400

) mainForm.

SetHeight

(

200

) mainForm.

SetCaption

(

"Calculator with LCL"

) mainForm.

SetPosition

(lcl.

PoScreenCenter

) mainForm.

Font

().

SetSize

(

11

) mainForm.

SetOnDestroy

(&

func

(sender)

{

println

(

"Form Destroyed."

) }) label1 = lcl.

NewLabel

(mainForm) label1.

SetParent

(mainForm) label1.

SetLeft

(

10

) label1.

SetTop

(

10

) label1.

Font

().

SetName

(

"Arial"

) label1.

Font

().

SetSize

(

18

) label1.

SetCaption

(

"Enter an expression"

) edit1 = lcl.

NewEdit

(mainForm) edit1.

SetParent

(mainForm) edit1.

SetBounds

(

10

,

48

,

200

,

32

) edit1.

Font

().

SetSize

(

11

) onClick1 =

func

(objA)

{ rs = eval(edit1.

Text

()) edit1.

SetText

(

toString

(rs)) } onClick2 =

func

(sender)

{ application.

Terminate

() } button1 = lcl.

NewButton

(mainForm) button1.

SetParent

(mainForm) button1.

SetLeft

(

20

) button1.

SetTop

(

90

) button1.

SetCaption

(

"Go"

) button1.

SetOnClick

(&onClick1) button2 = lcl.

NewButton

(mainForm) button2.

SetParent

(mainForm) button2.

SetLeft

(

110

) button2.

SetTop

(

90

) button2.

SetCaption

(

"Close"

) button2.

SetOnClick

(&onClick2) application.

Run

() /<code>

可以看到,代碼非常簡單,連空行加起來一共不超過70行代碼,就實現了一個圖形界面的計算器。下面我們看看執行之後的效果。

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

可以看到,很簡單地,一個圖形界面的小計算器已經成功運行了。我們可以嘗試輸入一個算式讓它計算。

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

點擊“Go”按鈕後,就會得到計算結果。

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

可以看到,計算結果已經出來了,最後的小數點是有名的計算機上的浮點數誤差,屬於計算機的正常行為。

而這個70行代碼實現的計算器就這麼簡單嗎?當然不是了,請看看下面的例子:

先輸入:

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

點擊“Go”按鈕計算後,

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

這是我們把一個數字存進變量a中,然後再次存儲一個數字到變量b(名字可以任意起)中,

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

可以是算式噢,還是要點擊“Go”按鈕進行計算和保存,

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

下面一步可以用a與b進行新的計算了。

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

複雜嗎?點擊“Go”按鈕計算後可以得到,

在Gox語言中用LCL庫進行GUI圖形界面編程-GX9

可以驗算一下是否正確。這居然是一個可編程的計算器!

另外,點擊“Close”按鈕是關閉該計算器,點擊窗口右上方的關閉按鈕也是同樣的效果。

由此可見,短短几十行代碼,實現了一個麻雀雖小、五臟俱全的圖形界面小程序,非常不容易。

下面我們來解說一下具體代碼,我們還是用代碼中的註釋來說明吧。


<code>lcl = 

import

("lcl") errT = lcl.

InitLCL

()

if

errT !=

nil

{ plerr(errT) exit() } application = lcl.

GetApplication

() application.

Initialize

() application.

SetTitle

(

"Calculator with LCL"

) application.

SetMainFormOnTaskBar

(

true

) mainForm = application.

CreateForm

() mainForm.

SetWidth

(

400

) mainForm.

SetHeight

(

200

) mainForm.

SetCaption

(

"Calculator with LCL"

) mainForm.

SetPosition

(lcl.

PoScreenCenter

) mainForm.

Font

().

SetSize

(

11

) mainForm.

SetOnDestroy

(&

func

(sender)

{

println

(

"Form Destroyed."

) }) label1 = lcl.

NewLabel

(mainForm) label1.

SetParent

(mainForm) label1.

SetLeft

(

10

) label1.

SetTop

(

10

) label1.

Font

().

SetName

(

"Arial"

) label1.

Font

().

SetSize

(

18

) label1.

SetCaption

(

"Enter an expression"

) edit1 = lcl.

NewEdit

(mainForm) edit1.

SetParent

(mainForm) edit1.

SetBounds

(

10

,

48

,

200

,

32

) edit1.

Font

().

SetSize

(

11

) onClick1 =

func

(objA)

{ rs = eval(edit1.

Text

()) edit1.

SetText

(

toString

(rs)) } onClick2 =

func

(sender)

{ application.

Terminate

() } button1 = lcl.

NewButton

(mainForm) button1.

SetParent

(mainForm) button1.

SetLeft

(

20

) button1.

SetTop

(

90

) button1.

SetCaption

(

"Go"

) button1.

SetOnClick

(&onClick1) button2 = lcl.

NewButton

(mainForm) button2.

SetParent

(mainForm) button2.

SetLeft

(

110

) button2.

SetTop

(

90

) button2.

SetCaption

(

"Close"

) button2.

SetOnClick

(&onClick2) application.

Run

() /<code>

代碼中通過註釋的方式已經解釋得很詳細,另外需要注意:

調用lcl.InitLCL函數進行GUI的初始化時,如果當前系統中沒有LCL庫(liblcl.dll),則會進行自動下載,保存在gox主程序的相同路徑下。如果希望不要有這一步,建議直接去Gox語言的官網自行下載liblcl.dll庫文件,然後可以放在Windows目錄下,也可以放在Gox語言主程序可執行文件的相同目錄下。

比較以下的話,在Gox語言中使用LCL庫編程,更貼近傳統程序員的編程思維,也能更精細地控制界面元素的展現和行為方式,相比OpenGL庫來說,稍微複雜一些但功能更豐富,唯一需要依賴的是要有運行庫,但也就僅需要下載一個dll庫文件,還算可以接受。並且,這種方式也是跨平臺的,在Mac和Linux只需要下載對應版本的庫文件即可。

總的來說,Gox語言作為一個解釋執行的腳本語言,能夠提供兩種風格的圖形界面庫,並且還是內置的,無需安裝附加包,相比大多數其他腳本語言來說是非常方便的。


分享到:


相關文章: