-
vb教程之用API函数实现Windows颜色渐变
文/方建文
用API函数实现颜色渐变
方建文
颜色渐变在Windows应用程序中应用广泛,最典型的是窗口标体的背景色及Windows安装窗口的背景色等。本文就这种颜色渐变的实现,提供API函数的实现方法。
在Windows 98或Windows NT 5.0及更高版本中提供了一个新的API函数来实现渐变颜色的填充,这个函数就是GradientFill。这个函数不仅能实现方形的填充,还能实现三角形的填充,所以这种方法更有效率。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常常是一个数组,用来存放各顶点的位置及颜色信息,顶点在TRIVERTEX中定义;dwNumVertex表示顶点的个数;pMesh也常常是一个数组结构,表示组成图形的各顶点顺序,表示一个矩形用两个顶点,三角形要用三个顶点;dwNumMesh表示矩形或三角形的个数;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‘类型转换
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
'类型转换
ColorRed = Color16(RGBColor Mod &&H100)
ColorGreen = Color16(RGBColor \ &&H100 Mod &&H100)
ColorBlue = Color16((RGBColor \ &&H10000) Mod &&H100)
End Function
用API函数实现颜色渐变
方建文
颜色渐变在Windows应用程序中应用广泛,最典型的是窗口标体的背景色及Windows安装窗口的背景色等。本文就这种颜色渐变的实现,提供API函数的实现方法。
在Windows 98或Windows NT 5.0及更高版本中提供了一个新的API函数来实现渐变颜色的填充,这个函数就是GradientFill。这个函数不仅能实现方形的填充,还能实现三角形的填充,所以这种方法更有效率。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常常是一个数组,用来存放各顶点的位置及颜色信息,顶点在TRIVERTEX中定义;dwNumVertex表示顶点的个数;pMesh也常常是一个数组结构,表示组成图形的各顶点顺序,表示一个矩形用两个顶点,三角形要用三个顶点;dwNumMesh表示矩形或三角形的个数;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‘类型转换
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
'类型转换
ColorRed = Color16(RGBColor Mod &&H100)
ColorGreen = Color16(RGBColor \ &&H100 Mod &&H100)
ColorBlue = Color16((RGBColor \ &&H10000) Mod &&H100)
End Function
最新更新
python爬虫及其可视化
使用python爬取豆瓣电影短评评论内容
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比