a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 42|回复: 0

[Visual Basic] 2011年计算机等考二级VB辅导知识技巧总结(6)

[复制链接]
发表于 2012-7-31 22:10:12 | 显示全部楼层 |阅读模式
 VB调用API制作渐变的窗口背景色  导读:在许多Windows软件的安装程序中,时常可以看到以某种颜色由浅至深的渐变为背景的窗口。使用VB制作这类窗口的方法有不少,而通过调用API函数绘制渐变的窗口背景色,似乎是一种既简单又节省资源的方法。
, z, E+ {; A. W) l6 P% s. y  首先,在module文件中加入下列声明语句:( I3 z! x! [8 N8 J. k+ x
  Declare Function CreateSolidBrush Lib"GDI"(ByVal _6 l) h% C8 O5 X# e( v
  crColor As Long) As Integer. D3 V9 S0 g7 J8 Y0 x* V
  Declare Function FillRect Lib"USER"(ByVal hDC As _! Q6 M$ Z7 s( S
  Integer,lpRect As RECT,ByVal hBrush As Integer)As Integer
! t; v  T: O0 x7 S# }  @( I. Z9 W  Declare Function DeleteObject Lib"GDI"(ByVal hObject _' N9 w4 e( ~6 Y
  As Integer)As Integer
+ a3 i  G/ l! w* {2 X6 k7 O  Type RECT% [9 }  f, }' s
  left As Integer
# M, v! k# ^" D8 i2 g  top As Integer# z) x, G2 F3 J' N$ Q: C2 c# P
  right As Integer
6 x" V5 A! h) B. n, G9 K. ?  bottom As Integer
9 G; A+ m+ @8 k. p. k0 i1 t  EndType  z. U3 B3 d- q$ j1 m
  然后,在窗口的Paint事件中加入下列代码:
( ^, E  I  O, ^, ]5 K+ r  Private Sub Form_Paint()
0 u* ^% z* X/ K6 y- V- g/ y  Dim Color As Integer
7 \$ c( g, F% d! O# w  Dim hBrush As Integer
+ S6 Z0 T( F3 o8 C  n# F# r) j  Dim OldMode As Integer& @/ \- ?+ O) {, J' t
  Dim RetVal As Integer0 ]/ Q- O5 X7 t( a7 e  l
  Dim StepSize As Integer
2 J+ {6 V" {& H4 ?6 B. X. B  Dim X As Integer
4 o) N' A& h) _) e7 G. `  Dim FillArea As RECT# H# S! L  d/ a. p
  OldMode%=Me.ScaleMode' t6 ]3 o5 K, [& ^: ^
  Me.ScaleMode=3
0 E0 g# J; L0 M' ~6 B* v2 l5 N+ X  StepSize%=1+Me.ScaleHeight/80
; c1 X6 C8 I  g  Color%=255  C( L3 U: R* X4 p4 E6 u
  FillArea.left=0
5 m4 q2 m$ }' I, Z  FillArea.right=Me.ScaleWidth' K7 {3 u# H- |8 `* T* Z% U" V
  FillArea.top=0
8 O) h- \+ o- ?$ p! b+ M  FillArea.bottom=StepSize%6 C0 g4 E  |( L& \% u
  For X%=1 To 809 O3 ]; o3 N9 k
  hBrush%=CreateSolidBrush (RGB(0,0,Color%))
9 B; R! e  a8 j0 {! x  RetVal%=FillRect(Me.hDC,FillArea,hBrush%)" g4 p7 u$ M7 q6 C
  RetVal%=DeleteObject(hBrush%)! |) f: d5 Y7 o5 S6 B. R
  Color%=Color%-4
8 y% O5 _! V3 b7 h, K" g  If Color% $#@60; 0 Then Color%=0
- I- U7 h1 I2 F; g0 m; p  FillArea.top=FillArea.bottom
9 a) I0 f) s5 g) V0 X, e8 y  p5 d  FillArea.bottom=FillArea.bottom+StepSize%2 s2 Y8 G. v$ m9 E" G
  Next7 A/ F  L  {- q
  Me.ScaleMode=OldMode%
% X# r: F" r+ Z: i! y+ I  EndSub* P4 c; c1 L% g8 Q2 l  y7 S* R
  按F5运行,就会出现一个以从上至下、由浅至深的渐变蓝色为背景的窗口。对上述代码稍加改动,便可制作出各种颜色和水平方向的渐变背景。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-18 09:00 , Processed in 0.257081 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表