Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #76
Online Version at http://www.codeoftheweek.com/membersonly/bi/0076.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Cool Software

Download a free copy of NetMon - Your Internet Performance Monitor at http://www.codeoftheweek.com/netmon/index.html

Special Deal

Get a LIFETIME subscription to Code of the Week for only $49.95 (regularly $59.95). Just jump to our SECRET order page at http://www.codeoftheweek.com/orderforms/lifetime4995.html and see everything you get with this outstanding offer!

In this Issue

In this issue we show how to add a gradient color background to your forms.

This source code is designed for VB 4.0 32-bit and up. Questions? Email us at questions@codeoftheweek.com.

basFormEffects

The basFormEffects module contains a routine to paint a gradient background on any form. It is designed to work with any form.

If you have any questions about using or creating this control, let us know at questions@codeoftheweek.com

Subroutine

Public Sub GradientFill(Obj As Form, lTopColor As Long, lBottomColor As Long)

This subroutine does all the work. It will start with the lTopColor and paint a gradient to lBottomColor on the form specified by Obj. The most common place you see this type of background is in setup programs. They usually have a faded blue to black background. This routine will allow you to specify any of the built-in constants for the colors that VB defines (vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, vbWhite).

Returns

Raises an error if any of the internal calls fail. There is no particular error that will be raised.

Sample Usage

The below sample describes how to use the GradientFill routine. It assumes you have created a form and added the following code to it. Make sure the AutoRedraw property is False for this sample.

Private Sub Form_Load()
    Me.Show
End Sub

Private Sub Form_Paint()
    GradientFill Me, vbRed, vbWhite
End Sub

Source Code

Just create a new Module and then paste this source code into it. You should name the class basFormEffects.

'----------------------------------------------------------------------
'
'   Module Name:    basFormEffects
'   Written By:     C&D Programming Corp.
'   Create Date:    1/99
'   Copyright:      Copyright 1999 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Option Explicit

Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long

' This routine works with these color selections.
'    vbBlack
'    vbRed
'    vbGreen
'    vbYellow
'    vbBlue
'    vbMagenta
'    vbCyan
'    vbWhite
Public Sub GradientFill(Obj As Form, lTopColor As Long, lBottomColor As Long)
    Dim lRow As Long
    Dim sngRedDiff As Single
    Dim sngBlueDiff As Single
    Dim sngGreenDiff As Single
    Dim sngRed As Single
    Dim sngBlue As Single
    Dim sngGreen As Single
    Dim lRet As Long

    ' set scale to force palette to a "nice" size.
    Obj.Scale (0, 255)-(1, 0)

    lRet = RealizePalette(Obj.hdc)
    If lRet <> 0 Then
        Err.Raise 5, "GradientFill", "Could not realize palette."
    End If

    sngRed = Red(lTopColor)
    sngBlue = Blue(lTopColor)
    sngGreen = Green(lTopColor)

    sngRedDiff = ColorDivisor(Red(lTopColor) - Red(lBottomColor))
    sngGreenDiff = ColorDivisor(Green(lTopColor) - Green(lBottomColor))
    sngBlueDiff = ColorDivisor(Blue(lTopColor) - Blue(lBottomColor))

    For lRow = 0 To 255
        Obj.Line (0, lRow)-(1, lRow - 1), RGB(CInt(sngRed), CInt(sngGreen), CInt(sngBlue)), BF
        sngRed = ColorShift(sngRed, sngRedDiff)
        sngBlue = ColorShift(sngBlue, sngBlueDiff)
        sngGreen = ColorShift(sngGreen, sngGreenDiff)
    Next
End Sub

Private Function ColorDivisor(lColor As Long) As Single
    ColorDivisor = IIf(lColor = 0, 0, lColor / 256)
End Function
'
'   We use singles to allow for partial colors (not the extremes)
'
Private Function ColorShift(sngColor As Single, sngDiff As Single) As Single
    sngColor = sngColor + sngDiff
    If sngColor > 255.5 Then
        ColorShift = 0
    Else
        If sngColor < 0 Then
            ColorShift = 255
        Else
            ColorShift = sngColor
        End If
    End If
End Function
'
'   An easy trick to isolate a single color into a value from 0 to 255.
'
Private Function Red(lColor As Long) As Integer
    Red = CInt("&h" & Left(Hex(lColor And vbRed), 2))
End Function

Private Function Blue(lColor As Long) As Integer
    Blue = CInt("&h" & Left(Hex(lColor And vbBlue), 2))
End Function

Private Function Green(lColor As Long) As Integer
    Green = CInt("&h" & Left(Hex(lColor And vbGreen), 2))
End Function

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com