在许多Windows软件的安装程序中,经常可以看到以某种颜色由浅至深的渐变为背景的窗口。使用Visual Basic制作这类窗口的方法有不少,而通过调用API函数绘制渐变的窗口背景色,似乎是种既简单又节省资源的方法。
首先,在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 %=deletepbject(hbrush %)
color %=color % - 4
if color %<0 then color % =0
fillarea.top=fillarea.bottom
fillarea.bottom=fillarea.bottom+stepsize %
next
me.scalemode=oldmode %
endsub
按F5运行,就会出现个以从上至下、由浅至深的渐变蓝色为背景的窗口。对上述代码稍加改动,便可制作出各种颜色和水平方向的渐变背景。