VB調用API制作漸變的窗口背景色
導讀:在許多Windows軟件的安裝程序中,時?梢钥吹揭阅撤N顏色由淺至深的漸變?yōu)楸尘暗拇翱。使用VB制作這類窗口的方法有不少,而通過調用API函數(shù)繪制漸變的窗口背景色,似乎是一種既簡單又節(jié)省資源的方法。
首先,在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運行,就會出現(xiàn)一個以從上至下、由淺至深的漸變藍色為背景的窗口。對上述代碼稍加改動,便可制作出各種顏色和水平方向的漸變背景。
相關推薦:北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內蒙古 |