一、實現原理及相關函數介紹
所謂文字的"閃入",指的是將所要顯示的文字分成兩部分,每部分的字符分別從程序界面的兩端進入,并最終顯示出來。它實現的原理是:對于一個待顯示的字符串,各個字符間人為的確定一個最初的間隔距離,在顯示過程中,對稱顯示并逐漸縮小這個距離直至達到系統默認的字符間距,從而實現字符串從界面二側"閃入"的效果。具體在編程實現中,一是需要使用SetTextCharacterExtra函數在待顯示的字符串的每個字符中加入間隔距離。二是在程序中加入定時器,每次定時器觸發后,用DrawTextEx顯示一個字符。三是在使用DrawTextEx函數時設置顯示的格式為DT_CENTER,并且設置該函數的DRAWTEXTPARAMS結構參數時,將其iLeftMargin、iRightMargin成員的值設為"0"。
程序實現過程中,需要聲明、使用下列三個API函數,它們分別是:
1、SetTextCharacterExtra
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.kjueaiud.com/
領測軟件測試網最新更新
關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
技術支持和業務聯系:info@testage.com.cn 電話:010-51297073
版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備2023014753號-2
技術支持和業務聯系:info@testage.com.cn 電話:010-51297073
老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月