• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

用API函数实现Windows颜色渐变

发布: 2007-7-14 20:28 | 作者: 佚名    | 来源: 网络转载     | 查看: 25次 | 进入软件测试论坛讨论

领测软件测试网 文/方建文

  用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

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网