明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線(xiàn)學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

使用LineDDA函數(shù)做容易動(dòng)畫(huà)

[摘要]如果在VB中實(shí)現(xiàn)比較簡(jiǎn)單的動(dòng)畫(huà)效果,也許很多人會(huì)選用Timer控件,其實(shí)API函數(shù)中有一個(gè)LineDDA,用這個(gè)函數(shù)實(shí)現(xiàn)簡(jiǎn)單的動(dòng)畫(huà)效果還是比較不錯(cuò)的。因?yàn)槭茿PI函數(shù),所以很一般化,因此很多語(yǔ)言都可以用它來(lái)實(shí)現(xiàn)簡(jiǎn)單的動(dòng)畫(huà)。 該函數(shù)的原型如下: BOOL LineDDA(int nXSt...
如果在VB中實(shí)現(xiàn)比較簡(jiǎn)單的動(dòng)畫(huà)效果,也許很多人會(huì)選用Timer控件,其實(shí)API函數(shù)中有一個(gè)LineDDA,用這個(gè)函數(shù)實(shí)現(xiàn)簡(jiǎn)單的動(dòng)畫(huà)效果還是比較不錯(cuò)的。因?yàn)槭茿PI函數(shù),所以很一般化,因此很多語(yǔ)言都可以用它來(lái)實(shí)現(xiàn)簡(jiǎn)單的動(dòng)畫(huà)。

該函數(shù)的原型如下:

BOOL LineDDA(int nXStart, int nYStart, int nXEnd, int nYEnd, LINEDDAPROC lpLineFunc, LPARAM lpData);

參數(shù)說(shuō)明如下:

nXStart:起點(diǎn)的X值

nYStart:起點(diǎn)的Y值

nXEnd:終點(diǎn)的X值

nYEnd:終點(diǎn)的Y值

lpLineFunc:回調(diào)函數(shù)的地址

lpData:用戶(hù)自定義參數(shù)(這個(gè)參數(shù)會(huì)傳給回調(diào)函數(shù))

這個(gè)函數(shù)和動(dòng)畫(huà)其實(shí)沒(méi)什么關(guān)系,它的功能就是計(jì)算出連接兩點(diǎn)的線(xiàn)段上的每一個(gè)屏幕像素的坐標(biāo),這兩個(gè)點(diǎn)的坐標(biāo)已經(jīng)在函數(shù)的前四個(gè)參數(shù)中給出。每計(jì)算出一個(gè)坐標(biāo),該函數(shù)就會(huì)調(diào)用第五個(gè)參數(shù)所指的回調(diào)函數(shù),我們可以在回調(diào)函數(shù)中完成一些簡(jiǎn)單的操作,以實(shí)現(xiàn)動(dòng)畫(huà)效果。

回調(diào)函數(shù)的原型是: VOID CALLBACK LineDDAProc(int X, int Y, LPARAM lpData);

前兩個(gè)參數(shù)是點(diǎn)的坐標(biāo),第三個(gè)參數(shù)就是由LineDDA傳過(guò)來(lái)的自定義參數(shù),是由我們自己指定的,傳什么都行。 :)

LineDDA 函數(shù)在VB中的聲明是:

Public Declare Function LineDDA Lib "gdi32.dll" (ByVal n1 As Long, ByVal n2 As Long, ByVal n3 As Long, ByVal n4 As Long, ByVal lpLineDDAProc As Long, ByVal lParam As Long) As Long

其回調(diào)用函數(shù)原型為:

Public Sub LineDDAProc(ByVal X As Long, ByVal Y As Long, ByVal lpData As Long)

在VB中,回調(diào)函數(shù)必須放在標(biāo)準(zhǔn)模塊中,傳遞函數(shù)地址時(shí)使用AddressOf運(yùn)算符,后面接函數(shù)名。

VB源程序如下:

窗體模塊:



Option Explicit



Private Sub Command1_Click()

' 循環(huán)調(diào)用 LineDDA 函數(shù), 在其指定的回調(diào)中實(shí)現(xiàn)簡(jiǎn)單動(dòng)畫(huà)

Dim i As Long

Dim point(9) As POINTAPI

For i = 0 To UBound(point) - 1

point(i + 1).X = point(i).X + 50

If point(i).Y = 0 Then point(i + 1).Y = 50 Else point(i + 1).Y = 0

LineDDA point(i).X, point(i).Y, point(i + 1).X, point(i + 1).Y, AddressOf LineDDAProc, Me.hdc

Next i

End Sub




標(biāo)準(zhǔn)模塊:



Option Explicit



' API 函數(shù)聲明

Public Declare Function LineDDA Lib "gdi32.dll" (ByVal n1 As Long, ByVal n2 As Long, ByVal n3 As Long, ByVal n4 As Long, ByVal lpLineDDAProc As Long, ByVal lParam As Long) As Long

Public Declare Function DrawText Lib "user32.dll" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long

Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)



' API 類(lèi)型聲明

Public Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type



Public Type POINTAPI

X As Long

Y As Long

End Type



' API 常量聲明

Private Const DT_CENTER = &H1

Private Const DT_VCENTER = &H4



' LineDDA 函數(shù)的回調(diào)函數(shù)

' 參數(shù): X, Y 為點(diǎn)坐標(biāo), lpData 為自定義參數(shù)

Public Sub LineDDAProc(ByVal X As Long, ByVal Y As Long, ByVal lpData As Long)

Dim rct As RECT

If X Mod 10 = 0 Then

rct.Left = X

rct.Right = rct.Left + 18

rct.Top = Y

rct.Bottom = rct.Top + 18

DrawText lpData, "LPP", -1, rct, DT_CENTER Or DT_VCENTER

Sleep (100)

DoEvents

End If

End Sub






上面的程序?qū)崿F(xiàn)的效果是在窗體上按照一條反折線(xiàn)動(dòng)態(tài)顯示一些文字,在窗體中添加一個(gè)CommandButton,復(fù)制上面的代碼即可。其實(shí),在著名的PGP加密軟件中,當(dāng)密碼輸入錯(cuò)誤時(shí),窗口會(huì)很調(diào)皮的抖動(dòng)一下,這個(gè)效果完全可以用 LineDDA 函數(shù)實(shí)現(xiàn),關(guān)鍵就看我們的想象力了。 :D