作者: 刘涛 出处: 天极网 责任编辑: 方舟
对于编程爱好者来说,开发软件过程中文字显示处理是一项很重要的内容,它的显示效果的好坏,对程序的界面效果有很大的影响,如果文字显示时能够打破陈规,有所创新,使用一些别致的方式,可以给用户耳目一新的感觉,从而增加程序的亲和力。针对Visual Basic编程,笔者给出了文字"闪入"显示这一特殊显示效果的实现方法,希望能够对读者朋友们开阔思路有所帮助。
一、实现原理及相关函数介绍
所谓文字的"闪入",指的是将所要显示的文字分成两部分,每部分的字符分别从程序界面的两端进入,并最终显示出来。它实现的原理是:对于一个待显示的字符串,各个字符间人为的确定一个最初的间隔距离,在显示过程中,对称显示并逐渐缩小这个距离直至达到系统默认的字符间距,从而实现字符串从界面二侧"闪入"的效果。具体在编程实现中,一是需要使用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)与我联系,索要相关代码。