欧美特黄不卡,涩涩视频在线,成人国产精品一区二区网站,亚洲一区二区三区欧美

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

用API函數完成Windows顏色漸變

用API函數完成Windows顏色漸變

更新時間:2022-05-06 文章作者:未知 信息來源:網絡 閱讀次數:

文/方建文

  用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

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

主站蜘蛛池模板: 榆树市| 宣化县| 南靖县| 黄骅市| 信丰县| 沂源县| 辽中县| 镇平县| 哈巴河县| 科技| 宝应县| 建宁县| 会同县| 项城市| 尚志市| 海林市| 郁南县| 鄂州市| 微博| 怀化市| 边坝县| 固阳县| 衡南县| 丁青县| 云林县| 万荣县| 阳新县| 监利县| 甘孜| 肥乡县| 盐山县| 洱源县| 田东县| 恩平市| 长汀县| 西畴县| 四平市| 平江县| 科技| 正镶白旗| 岑溪市|