计算机二级

3773考试网计算机等级考试计算机二级正文

VB调用API制作渐变的窗口背景色

来源:fjzsksw.com 2010-8-18 8:39:14

 

首先,在module文件中加入下列声明语句:
  Declare Function CreateSolidBrush Lib"GDI"(ByVal _
  crColor As Long) As Integer
  Declare Function FillRect Lib"USER"(ByVal hDC As _
  Integer,lpRect As RECT,ByVal hBrush As Integer)As Integer
  Declare Function DeleteObject Lib"GDI"(ByVal hObject _
  As Integer)As Integer
  Type RECT
  left As Integer
  top As Integer
  right As Integer
  bottom As Integer
  EndType
  然后,在窗口的Paint事件中加入下列代码:
  Private Sub Form_Paint()
  Dim Color As Integer
  Dim hBrush As Integer
  Dim OldMode As Integer
  Dim RetVal As Integer
  Dim StepSize As Integer
  Dim X As Integer
  Dim FillArea As RECT
  OldMode%=Me.ScaleMode
  Me.ScaleMode=3
  StepSize%=1+Me.ScaleHeight/80
  Color%=255
  FillArea.left=0
  FillArea.right=Me.ScaleWidth
  FillArea.top=0
  FillArea.bottom=StepSize%
  For X%=1 To 80
  hBrush%=CreateSolidBrush (RGB(0,0,Color%))
  RetVal%=FillRect(Me.hDC,FillArea,hBrush%)
  RetVal%=DeleteObject(hBrush%)
  Color%=Color%-4
  If Color% $#@60; 0 Then Color%=0
  FillArea.top=FillArea.bottom
  FillArea.bottom=FillArea.bottom+StepSize%
  Next
  Me.ScaleMode=OldMode%
  EndSub
  按F5运行,就会出现一个以从上至下、由浅至深的渐变蓝色为背景的窗口。对上述代码稍加改动,便可制作出各种颜色和水平方向的渐变背景。

 

 

触屏版 电脑版
3773考试网 琼ICP备12003406号-1