用API函數(shù)完成Windows顏色漸變
發(fā)表時(shí)間:2023-08-02 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]文/方建文 用API函數(shù)實(shí)現(xiàn)顏色漸變 方建文 顏色漸變?cè)赪indows應(yīng)用程序中應(yīng)用廣泛,最典型的是窗口標(biāo)體的背景色及Windows安裝窗口的背景色等。本文就這種顏色漸變的實(shí)現(xiàn),提供API函數(shù)...
文/方建文
用API函數(shù)實(shí)現(xiàn)顏色漸變
方建文
顏色漸變?cè)赪indows應(yīng)用程序中應(yīng)用廣泛,最典型的是窗口標(biāo)體的背景色及Windows安裝窗口的背景色等。本文就這種顏色漸變的實(shí)現(xiàn),提供API函數(shù)的實(shí)現(xiàn)方法。
在Windows 98或Windows NT 5.0及更高版本中提供了一個(gè)新的API函數(shù)來實(shí)現(xiàn)漸變顏色的填充,這個(gè)函數(shù)就是GradientFill。這個(gè)函數(shù)不僅能實(shí)現(xiàn)方形的填充,還能實(shí)現(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是表示要填充對(duì)象的窗口句柄;pVertex常常是一個(gè)數(shù)組,用來存放各頂點(diǎn)的位置及顏色信息,頂點(diǎn)在TRIVERTEX中定義;dwNumVertex表示頂點(diǎn)的個(gè)數(shù);pMesh也常常是一個(gè)數(shù)組結(jié)構(gòu),表示組成圖形的各頂點(diǎn)順序,表示一個(gè)矩形用兩個(gè)頂點(diǎn),三角形要用三個(gè)頂點(diǎn);dwNumMesh表示矩形或三角形的個(gè)數(shù);dwMode表示填充的模式:水平填充,垂直填充,三角形填充。以下是示例程序:
在這個(gè)示例里您可以任意選擇兩種顏色,然后用兩種顏色對(duì)一個(gè)Picture1進(jìn)行漸變的填充。
包含的部件
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‘頂點(diǎn)類型
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‘打開顏色選擇對(duì)話框
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