说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

在VB中利用API实现窗体的平滑显示

HTML文档下载 WORD文档下载 PDF文档下载
在VB中利用API实现窗体的平滑显示


 Win98的一个新功能是窗体能够平滑显示,那我们是否能编程来实现这种功能呢?答案是肯定的。首先讲述一下原理,其实我并没有让窗体本身平滑地显示,而是在窗体显示之前在窗体的位置上画一系列的矩形,利用视觉暂留让人以为是窗体在从小变大。
  下面就用VB来实现这种功能。
  建立两个窗体form1和form2,在form1上添加如下控件:由五个optionbutton控件组成的控件数组,index为0-4,caption属性分别为“从中间扩散”、“右上到左下”、“左上到右下”、“右下到左上”和“左上到右下”,一个commandbutton,caption属性为“显示窗体”, 添加一个模块其中代码如下:
   Option Explicit
  `声明所用的API函数、常量和变量
  Public Declare Function GetDC Lib “user32” (ByVal hwnd As Long) As Long
   Public Declare Function Rectangle Lib “gdi32” (ByVal hdc As Long, _
   ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2_
   As Long) As Long
   Public Declare Function SelectObject Lib “gdi32” (ByVal hdc As Long, _
   ByVal hObject As Long) As Long
   Public Declare Function DeleteObject Lib “gdi32”(ByVal hObject As _
   Long) As Long
   Public Declare Function ReleaseDC Lib “user32” (ByVal hwnd As Long,_
   ByVal hdc As Long) As Long
   Public Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)
   Public Declare Function CreateSolidBrush Lib “gdi32” (ByVal crColor As_
   Long) As Long
   Public Declare Function GetBkColor Lib “gdi32” (ByVal hdc As Long) As Long
   Public hbrush As Long, hdc5 As Long
   Public dx As Long, dy As Long
   Public rx1 As Long, rx2 As Long, ry1 As Long, ry2 As Long
   Public i As Long, j As Long, bcolor As Long
   Public ind As Integer
   Public DispCnt As Long
  
  Public Sub showform(win As Form, ind)
   DispCnt = 60 `画几个矩形后显示窗体
   `下面这段代码用来获得窗体颜色,不用me.backcolor的原因是窗体颜色不一定是系统调色板的颜色,如果用me.backcolor的话颜色可能会不准。
   hdc5 = GetDC(0)
   bcolor = GetBkColor(win.hdc)
   hbrush = CreateSolidBrush(bcolor)`设定刷子颜色
   Call SelectObject(hdc5, hbrush)
   dx = win.Width \ (DispCnt)
   dy = win.Height \ (DispCnt)
   j = 1
   Select Case ind
   Case 1
   dx = dx \ 2; dy = dy \ 2
   For i = DispCnt To 1 Step -1
   rx1 = (win.Left + dx * (i - 1)) \ Screen.TwipsPerPixelX
   ry1 = (win.Top + dy * (i - 1)) \ Screen.TwipsPerPixelY
   rx2 = rx1 + dx * 2 * j \ Screen.TwipsPerPixelX
   ry2 = rx1 + dy * 2 * j \ Screen.TwipsPerPixelY
   j = j + 1
   Call Rectangle(hdc5, rx1, ry1, rx2, ry2)
   Sleep (1); Next i
  Case 2
   For i = DispCnt To 1 Step -1
   rx1 = (win.Left + win.Width - dx * j) \ Screen.TwipsPerPixelX
   ry1 = win.Top \ Screen.TwipsPerPixelY
   rx2 = (win.Left + win.Width) \ Screen.TwipsPerPixelX
   ry2 = (win.Top + dy * j) \ Screen.TwipsPerPixelY
   j = j + 1
   Call Rectangle(hdc5, rx1, ry1, rx2, ry2)
   Sleep (1); Next i
   Case 3
   For i = DispCnt To 1 Step -1
   rx1 = win.Left \ Screen.TwipsPerPixelX
   ry1 = win.Top \ Screen.TwipsPerPixelY
   rx2 = rx1 + dx * j \ Screen.TwipsPerPixelX
   ry2 = rx1 + dy * j \ Screen.TwipsPerPixelY
   j = j + 1
   Call Rectangle(hdc5, rx1, ry1, rx2, ry2)
   Sleep (1); Next i
   Case 4
   For i = DispCnt To 1 Step -1
   rx1 = (win.Left + dx * (i - 1)) \ Screen.TwipsPerPixelX
   ry1 = (win.Top + dy * (i - 1)) \ Screen.TwipsPerPixelY
   rx2 = (win.Left + win.Width) \ Screen.TwipsPerPixelX
   ry2 = (win.Top + win.Height) \ Screen.TwipsPerPixelY
   j = j + 1
   Call Rectangle(hdc5, rx1, ry1, rx2, ry2)
   Sleep (1); Next i
   Case 5
   For i = DispCnt To 1 Step -1
   rx1 = (win.Left) \ Screen.TwipsPerPixelX
   ry1 = (win.Top + win.Height - dy * j) \ Screen.TwipsPerPixelY
   rx2 = (win.Left + dx * j) \ Screen.TwipsPerPixelX
   ry2 = (win.Top + win.Height) \ Screen.TwipsPerPixelY
   j = j + 1
   Call Rectangle(hdc5, rx1, ry1, rx2, ry2)
   Sleep (1); Next i
   End Select
  Call ReleaseDC(0, hdc5)`释放设备描述表
  Call DeleteObject(hbrush)`删除刷子
  End Sub
  在form1中输入如下代码
  Private Sub Command1_Click()`显示窗体
  Unload Form2
  Load Form2
  Form2.Show
  End Sub
  
  Private Sub Form_Load()
   ind = 1`初始化参数
  End Sub
  
  Private Sub Option1_Click(Index As Integer)
  Select Case Index
   Case 0
   ind = 1
   Case 1
   ind = 2
   Case 2
   ind = 3
   Case 3
   ind = 4
   Case 4
   ind = 5
   Case 5
   ind = 6
   End Select
  
  End Sub
  在form2中输入如下代码
  Private Sub Form_Load()
   Call showform(Me, ind)
  End Sub
  本程序调用了一些有关系统和绘图的API函数,有利于大家进一步了解API函数的功能。本程序在vb5企业版,Win98中运行通过。
备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘