VB實現文字“閃入”顯示的特殊效果

發表于:2007-05-25來源:作者:點擊數: 標簽:文字閃入效果實現特殊
對于編程愛好者來說, 開發 軟件過程中文字顯示處理是一項很重要的內容,它的顯示效果的好壞,對程序的界面效果有很大的影響,如果文字顯示時能夠打破陳規,有所創新,使用一些別致的方式,可以給用戶耳目一新的感覺,從而增加程序的親和力。針對Visual Basi
 對于編程愛好者來說,開發軟件過程中文字顯示處理是一項很重要的內容,它的顯示效果的好壞,對程序的界面效果有很大的影響,如果文字顯示時能夠打破陳規,有所創新,使用一些別致的方式,可以給用戶耳目一新的感覺,從而增加程序的親和力。針對Visual Basic編程,筆者給出了文字"閃入"顯示這一特殊顯示效果的實現方法,希望能夠對讀者朋友們開闊思路有所幫助。

  一、實現原理及相關函數介紹

  所謂文字的"閃入",指的是將所要顯示的文字分成兩部分,每部分的字符分別從程序界面的兩端進入,并最終顯示出來。它實現的原理是:對于一個待顯示的字符串,各個字符間人為的確定一個最初的間隔距離,在顯示過程中,對稱顯示并逐漸縮小這個距離直至達到系統默認的字符間距,從而實現字符串從界面二側"閃入"的效果。具體在編程實現中,一是需要使用SetTextCharacterExtra函數在待顯示的字符串的每個字符中加入間隔距離。二是在程序中加入定時器,每次定時器觸發后,用DrawTextEx顯示一個字符。三是在使用DrawTextEx函數時設置顯示的格式為DT_CENTER,并且設置該函數的DRAWTEXTPARAMS結構參數時,將其iLeftMargin、iRightMargin成員的值設為"0"。

  程序實現過程中,需要聲明、使用下列三個API函數,它們分別是:

  1、SetTextCharacterExtra

clearcase/" target="_blank" >cccccc" width="90%" align="center" bgcolor="#e3e3e3" border="1">
Declare Function SetTextCharacterExtra Lib "gdi32" Alias "SetTextCharacterExtraA" (ByVal hdc As Long, ByVal nCharExtra As Long) As Long

  說明:該函數用于在描繪文本時,指定字符串內各字符間插入的額外間距。參數hdc代表設備場景的句柄,nCharExtra指的是要在字符間插入的額外空間(采用設備場景的邏輯坐標系統)。該函數調用成功后,返回一個Long類型的值,它指的是這個設備場景的前一個額外間距設置。

  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

  參數hDC是要在其中繪圖的一個設備場景的句柄,lpsz 是欲描繪輸出的文本字串,n為欲描繪的字符數量,如果要描繪整個字串(直到中止符),則可將這個參數設為-1。lpRect RECT,指定用于繪圖的一個格式化矩形(采用邏輯坐標),un是一個標志位。決定了以何種形式執行繪圖,例如:DT_EDITCONTROL 對一個多行編輯控件進行模擬;DT_ENDELLIPSES 將在字串不能在矩形里全部容下的情況下就在末尾顯示省略號等等。lpDrawTextParams是一個指向DRAWTEXTPARAMS結構的指針,它包含了額外的格式信息。

  二、實現代碼

  了解了實現原理及方法,下面就讓我們來動手編程吧。首先,啟動Visual Basic生成一單文檔應用程序,在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

  三、小結

  筆者在文中只是就文本的兩側對稱"閃入"顯示效果的原理、方法及實現代碼作了簡單的介紹。其實,用好上述幾個函數還可以實現其他特殊顯示,如文字的拖動顯示效果等,但由于篇幅有限,在這里就不再贅述,有興趣的讀者朋友們可以通過電子郵件(liutaomail@ah163.net)與我聯系,索要相關代碼。

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97