选择显示字体大小

vb实现文字“闪入”显示的特殊效果


  对于编程爱好者来说,开发软件过程中文字显示处理是一项很重要的内容,它的显示效果的好坏,对程序的界面效果有很大的影响,如果文字显示时能够打破陈规,有所创新,使用一些别致的方式,可以给用户耳目一新的感觉,从而增加程序的亲和力。 针对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)与我联系,索要相关代码。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons