文/方建文
用API函數(shù)實現(xiàn)顏色漸變
方建文
顏色漸變在Windows應用程序中應用廣泛,最典型的是窗口標體的背景色及Windows安裝窗口的背景色等。本文就這種顏色漸變的實現(xiàn),提供API函數(shù)的實現(xiàn)方法。
在Windows 98或Windows NT 5.0及更高版本中提供了一個新的API函數(shù)來實現(xiàn)漸變顏色的填充,這個函數(shù)就是GradientFill。這個函數(shù)不僅能實現(xiàn)方形的填充,還能實現(xiàn)三角形的填充,所以這種方法更有效率。API聲明如下:
Public Declare Function GradientFillTriangle Lib "msimg32" Alias "GradientFill" (ByVal hDC As Long, pVertex As TRIVERTEX, ByVal dwNumVertex As Long, pMesh As GRADIENT_TRIANGLE, ByVal dwNumMesh As Long, ByVal dwMode As Long) As Long
Public Declare Function GradientFillRect Lib "msimg32" Alias "GradientFill" (ByVal hDC As Long, pVertex As TRIVERTEX, ByVal dwNumVertex As Long, pMesh As GRADIENT_RECT, ByVal dwNumMesh As Long, ByVal dwMode As Long) As Long
其中GradientFillTriangle用于三角形的填充,GradientFillRect用于矩形填充。hDC是表示要填充對象的窗口句柄;pVertex常常是一個數(shù)組,用來存放各頂點的位置及顏色信息,頂點在TRIVERTEX中定義;dwNumVertex表示頂點的個數(shù);pMesh也常常是一個數(shù)組結(jié)構(gòu),表示組成圖形的各頂點順序,表示一個矩形用兩個頂點,三角形要用三個頂點;dwNumMesh表示矩形或三角形的個數(shù);dwMode表示填充的模式:水平填充,垂直填充,三角形填充。以下是示例程序:
在這個示例里您可以任意選擇兩種顏色,然后用兩種顏色對一個Picture1進行漸變的填充。
包含的部件
Form1—AutoRedraw:True
Picture1---Align:1—Align Top
Frame1----Caption:漸變模式
Option1—Caption:由上到下
Value:True
Option2---Caption:由左到右
Label1(0)---Caption:顏色1
Command1(0)—Style:1—Graphical
Label1(1)---Caption:顏色2
Command1(1)—Style:1—Graphical
CommonDialog1--(Microsoft CommonDialog Control6.0)用于選擇顏色
Command2----Caption:填充
代碼模塊Module1中的代碼
Option Explicit
Public Const GRADIENT_FILL_RECT_H = &&H0
Public Const GRADIENT_FILL_RECT_V = &&H1
Public Const GRADIENT_FILL_TRIANGLE = &&H2‘以上為三種填充模式
Public Type GRADIENT_TRIANGLE
Vertex1 As Long
Vertex2 As Long
Vertex3 As Long
End Type
Public Type GRADIENT_RECT
UpperLeft As Long
LowerRight As Long
End Type
Public Type TRIVERTEX‘頂點類型
x As Long
y As Long
Red As Integer
Green As Integer
Blue As Integer
Alpha As Integer
End Type
Public Declare Function GradientFillTriangle Lib "msimg32" Alias "GradientFill" (ByVal hDC As Long, pVertex As TRIVERTEX, ByVal dwNumVertex As Long, pMesh As GRADIENT_TRIANGLE, ByVal dwNumMesh As Long, ByVal dwMode As Long) As Long
Public Declare Function GradientFillRect Lib "msimg32" Alias "GradientFill" (ByVal hDC As Long, pVertex As TRIVERTEX, ByVal dwNumVertex As Long, pMesh As GRADIENT_RECT, ByVal dwNumMesh As Long, ByVal dwMode As Long) As Long
Public Function UIntToInt(UInt As Long) As Integer‘類型轉(zhuǎn)換
If UInt<&&H7FFF Then
UIntToInt = CInt(UInt)
Else
UIntToInt = CInt(UInt - &&H10000)
End If
End Function
Public Function Color16(Clr As Byte) As Integer
Color16 = UIntToInt(Clr&&H100&&)
End Function
窗體模塊代碼
Private Sub Command1_Click(Index As Integer)
CommonDialog1.CancelError = True
On Error GoTo ErrHandler
CommonDialog1.Flags = cdlCCRGBInit
CommonDialog1.ShowColor‘打開顏色選擇對話框
Command1(Index).BackColor=CommonDialog1.Color
Exit Sub
ErrHandler:
End Sub
Private Sub Command2_Click()
Dim rect(0 To 1) As TRIVERTEX
Dim prect As GRADIENT_RECT
With rect(0)
.x = 0
.y = 0
RGBToColor16 Command1(0).BackColor,
.Red, .Green, .Blue
End With
With rect(1)
.x = Picture1.ScaleWidth
.y = Picture1.ScaleHeight
RGBToColor16 Command1(1).BackColor,
.Red, .Green, .Blue
End With
prect.UpperLeft = 0
prect.LowerRight = 1
If Option1.Value Then
GradientFillRect Picture1.hDC, rect(0), 2, prect, 1, GRADIENT_FILL_RECT_V‘豎直填充
Else
GradientFillRect Picture1.hDC, rect(0), 2, prect, 1, GRADIENT_FILL_RECT_H‘水平填充
End If
End Sub
Private Function RGBToColor16(RGBColor As Long, ColorRed As Integer, ColorGreen As Integer, ColorBlue As Integer) As Integer
'類型轉(zhuǎn)換
ColorRed = Color16(RGBColor Mod &&H100)
ColorGreen = Color16(RGBColor \ &&H100 Mod &&H100)
ColorBlue = Color16((RGBColor \ &&H10000) Mod &&H100)
End Function
|