VB實(shí)現(xiàn)文字“閃入”顯示的特殊效果

字號:

對于編程愛好者來說,開發(fā)軟件過程中文字顯示處理是一項(xiàng)很重要的內(nèi)容,它的顯示效果的好壞,對程序的界面效果有很大的影響,如果文字顯示時能夠打破陳規(guī),有所創(chuàng)新,使用一些別致的方式,可以給用戶耳目一新的感覺,從而增加程序的親和力。針對Visual Basic編程,筆者給出了文字"閃入"顯示這一特殊顯示效果的實(shí)現(xiàn)方法,希望能夠?qū)ψx者朋友們開闊思路有所幫助。
    一、實(shí)現(xiàn)原理及相關(guān)函數(shù)介紹
    所謂文字的"閃入",指的是將所要顯示的文字分成兩部分,每部分的字符分別從程序界面的兩端進(jìn)入,并最終顯示出來。它實(shí)現(xiàn)的原理是:對于一個待顯示的字符串,各個字符間人為的確定一個最初的間隔距離,在顯示過程中,對稱顯示并逐漸縮小這個距離直至達(dá)到系統(tǒng)默認(rèn)的字符間距,從而實(shí)現(xiàn)字符串從界面二側(cè)"閃入"的效果。具體在編程實(shí)現(xiàn)中,一是需要使用SetTextCharacterExtra函數(shù)在待顯示的字符串的每個字符中加入間隔距離。二是在程序中加入定時器,每次定時器觸發(fā)后,用DrawTextEx顯示一個字符。三是在使用DrawTextEx函數(shù)時設(shè)置顯示的格式為DT_CENTER,并且設(shè)置該函數(shù)的DRAWTEXTPARAMS結(jié)構(gòu)參數(shù)時,將其iLeftMargin、iRightMargin成員的值設(shè)為"0"。
    程序?qū)崿F(xiàn)過程中,需要聲明、使用下列三個API函數(shù),它們分別是:
    1、SetTextCharacterExtra
    Declare Function SetTextCharacterExtra Lib "gdi32" Alias "SetTextCharacterExtraA" (ByVal hdc As Long, ByVal nCharExtra As Long) As Long
    說明:該函數(shù)用于在描繪文本時,指定字符串內(nèi)各字符間插入的額外間距。參數(shù)hdc代表設(shè)備場景的句柄,nCharExtra指的是要在字符間插入的額外空間(采用設(shè)備場景的邏輯坐標(biāo)系統(tǒng))。該函數(shù)調(diào)用成功后,返回一個Long類型的值,它指的是這個設(shè)備場景的前一個額外間距設(shè)置。
    2、DrawTextEx
    Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hDC As Long, ByVal lpsz As String, ByVal n As Long, lpRect As RECT, ByVal un As Long, lpDrawTextParams As DRAWTEXTPARAMS) As Long
    參數(shù)hDC是要在其中繪圖的一個設(shè)備場景的句柄,lpsz 是欲描繪輸出的文本字串,n為欲描繪的字符數(shù)量,如果要描繪整個字串(直到中止符),則可將這個參數(shù)設(shè)為-1。lpRect RECT,指定用于繪圖的一個格式化矩形(采用邏輯坐標(biāo)),un是一個標(biāo)志位。決定了以何種形式執(zhí)行繪圖,例如:DT_EDITCONTROL 對一個多行編輯控件進(jìn)行模擬;DT_ENDELLIPSES 將在字串不能在矩形里全部容下的情況下就在末尾顯示省略號等等。lpDrawTextParams是一個指向DRAWTEXTPARAMS結(jié)構(gòu)的指針,它包含了額外的格式信息。
    二、實(shí)現(xiàn)代碼
    了解了實(shí)現(xiàn)原理及方法,下面就讓我們來動手編程吧。首先,啟動Visual Basic生成一單文檔應(yīng)用程序,在Form1上放置Timer控件用來啟動定時程序;放置三個Label控件,其中一個用來顯示文本信息,二個用來作為按鈕分別用來啟動文本顯示及退出程序。最后添加代碼如下:
    Option Explicit
    ’ TYPE STRUCTURES
    Private Type tpeTextProperties
    cbSize As Long
    iTabLength As Long
    iLeftMargin As Long
    iRightMargin As Long
    uiLengthDrawn As Long
    End Type
    Private Type tpeRectangle
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    ’ CONSTANTS
    Private Const DT_CENTER = &H1
    Private Const DT_VCENTER = &H4
    ’ API DECLARATIONS
    Private Declare Function DrawTextEx Lib "user32" Alias "DrawTextExA" (ByVal hdc As Long, ByVal lpsz As String, ByVal n As Long, lpRect As tpeRectangle, ByVal un As Long, lpDrawTextParams As tpeTextProperties) As Long
    Private Declare Function SetTextCharacterExtra Lib "gdi32" (ByVal hdc As Long, ByVal nCharExtra As Long) As Long
    Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As tpeRectangle) As Long
    Public strCharSpace As Integer
    Private Sub Form_Load()
    ’ Call the button code which performs the function which
    ’ we want to do here.
    Call cmdStart_Click
    End Sub
    Private Sub cmdClose_Click()
    Unload frmMain ’ Unload this form from memory
    End ’ End the program
    End Sub
    Private Sub cmdStart_Click()
    ’ Draw the text with a large space between the characters
    strCharSpace = 240
    Call doAnimationFX
    ’ Start the timer
    tmrProgTimer.Enabled = True
    End Sub
    Private Sub tmrProgTimer_Timer()
    ’ Take away one of the present value of the spacing
    strCharSpace = strCharSpace - 1
    Call doAnimationFX ’ Draw the new string
    ’ Check the value of ’strCharSpace’
    If strCharSpace = 0 Then tmrProgTimer.Enabled = False
    End Sub
    Private Sub doAnimationFX()
    ’ Procedure Scope Declarations
    Dim typeDrawRect As tpeRectangle
    Dim typeDrawParams As tpeTextProperties
    Dim strCaption As String
    ’ Set the string which will be animated
    strCaption = "Visual Basic Code"
    ’ Set the area in which the animation will take place.
    ’ Needs to be a control which has the ’.hwnd’ property
    ’ and can be refreshed and cleared easily. So a picture
    ’ box is the best candidate
    GetClientRect picAniRect.hwnd, typeDrawRect
    ’ Now set the properties which will be used in the animation
    With typeDrawParams
    ’ The size of the animation
    .cbSize = Len(typeDrawParams)
    ’ The left and right margins
    .iLeftMargin = 0
    .iRightMargin = 0
    End With
    ’ Clear the picture box
    picAniRect.Cls
    ’ Set the character spacing which will be used
    SetTextCharacterExtra picAniRect.hdc, Val(strCharSpace)
    ’ Draw the string of text, in the set area with the
    ’ specified options
    DrawTextEx picAniRect.hdc, strCaption, Len(strCaption), _
    typeDrawRect, SaveOptions, typeDrawParams
    ’ Refresh the picture box which contains the animation
    picAniRect.Refresh
    End Sub
    Private Function SaveOptions() As Long
    ’ Procedure Scope Declaration
    Dim MyFlags As Long
    ’ Set the options which will be used in the FX
    MyFlags = MyFlags Or DT_CENTER
    MyFlags = MyFlags Or DT_VCENTER
    ’ Store the flags which we have set above
    SaveOptions = MyFlags
    End Function
    三、小結(jié)
    筆者在文中只是就文本的兩側(cè)對稱"閃入"顯示效果的原理、方法及實(shí)現(xiàn)代碼作了簡單的介紹。其實(shí),用好上述幾個函數(shù)還可以實(shí)現(xiàn)其他特殊顯示,如文字的拖動顯示效果等,但由于篇幅有限,在這里就不再贅述,有興趣的讀者朋友們可以通過電子郵件(liutaomail@ah163.net)與我聯(lián)系,索要相關(guān)代碼。