Make Gradient Forms (Using API)

This example using msimg32.dll That's shipped with Windows 98, but not shipped with Windows 95/NT.

Preparations

Add 2 Command Buttons (named Command1 and Command2)
Pressing on Command1 will paint the form wilth gradient fill. Pressing on Command2 will clear the form.

Module Code

Option Explicit

Public Type TRIVERTEX
    x As Long
    y As Long
    Red As Integer
    Green As Integer
    Blue As Integer
    Alpha As Integer
End Type
   
Public Type GRADIENT_RECT
    UpperLeft As Long
    LowerRight As Long
End Type

Public Const GRADIENT_FILL_RECT_H As Long = &H0
Public Const GRADIENT_FILL_RECT_V  As Long = &H1

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

Form Code

Private Sub Form_Load()
   Me.ScaleMode = vbPixels
End Sub

Private Function LongToUShort(ULong As Long) As Integer
   LongToUShort = CInt(ULong - &H10000)
End Function

Private Function UShortToLong(Ushort As Integer) As Long
   UShortToLong = (CLng(Ushort) And &HFFFF&)
End Function

Private Sub Command2_Click()
   Cls
End Sub

Private Sub Command1_Click()
Dim vert(1) As TRIVERTEX
Dim gRect As GRADIENT_RECT
With vert(0)
    .x = 0
    .y = 0
    .Red = 0&
    .Green = &HFF&
    .Blue = 0&
    .Alpha = 0&
End With
With vert(1)
    .x = Me.ScaleWidth
    .y = Me.ScaleHeight
    .Red = 0&
    .Green = LongToUShort(&HFF00&)
    .Blue = LongToUShort(&HFF00&)
    .Alpha = 0&
End With
gRect.UpperLeft = 1
gRect.LowerRight = 0
'replace GRADIENT_FILL_RECT_H with GRADIENT_FILL_RECT_V  to paint
'the form with vertically gradient, instead of horizontally gradient

GradientFillRect Me.hdc, vert(0), 2, gRect, 1, GRADIENT_FILL_RECT_H
End Sub

Go Back