Tip 205: Drawing a Gradient Background Pattern on Forms in Visual Basic 4.0

February 28, 1996

Abstract
The Microsoft® Visual Basic® version 4.0 Setup program allows you to
create a standard installation program for your Visual Basic application.
The Setup program, as with other Microsoft Setup programs, draws a
gradient background pattern, usually in a blue color. This article
explains how to draw gradient backgrounds in a Visual Basic application.

Using the RealizePalette Function to Draw Backgrounds
Most Microsoft® Windows® applications are installed by running a Setup
program. These setup programs often display a window in which appears a
faded blue background. This same effect can be reproduced in Microsoft
Visual Basic® version 4.0 applications by using the Windows application
programming interface (API) RealizePalette function.

To display a window with a gradient background, you simply paint thin
rectangles in different colors on the form. This gives the form a
fade-to-black effect.

The RealizePalette function lets you select a logical palette for a device
context, such as a window. This allows you to use a larger number of
colors in your application without interfering with the colors used by
other forms (windows) in your program. After calling the RealizePalette
function, you need only select the colors with which you want to paint
the rectangles and the gradient background effect is created.

Example Program
This program shows how to draw a gradient background on a form/window in
Visual Basic.

 1. Create a new project in Visual Basic. Form1 is created by default.
 2. Add the following code to the General Declarations section of Form1
    (note that the Declare statement must be typed as a single line of
    code):

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

Dim RedColor(256) As Integer
Dim GreenColor(256) As Integer
Dim BlueColor(256) As Integer

 3. Add the following code to the Form_Load event for Form1:

Private Sub Form_Load()
    For I = 1 To 256
        RedColor(I) = 1
        GreenColor(I) = 1
        BlueColor(I) = I
    Next I
End Sub

 4. Add a Command Button control to Form1. Command1 is created by default.
 5. Add the following code to the Click event for Command1:

Private Sub Command1_Click()
    dummy = RealizePalette(Form1.hdc)
    Form1.Scale (0, 0)-(256, 1)
    For I = 0 To 255
        Form1.Line (I, 0)-(I + 1, 1), RGB(RedColor(I + 1), _
            GreenColor(I + 1), BlueColor(I + 1)), BF
        Form1.ForeColor = RGB(RedColor(I + 1), GreenColor(I + 1), _
            BlueColor(I + 1))
    Next I
End Sub

 6. Add a second Command Button control to Form1. Command2 is created by
    default.
 7. Add the following code to the Click event for Command2:

Private Sub Command2_Click()
    dummy = RealizePalette(Form1.hdc)
    Form1.Scale (0, 0)-(1, 256)
    For I = 0 To 255
        Form1.Line (0, I)-(1, I + 1), RGB(RedColor(I + 1), _
        GreenColor(I + 1), BlueColor(I + 1)), BF
        Form1.ForeColor = RGB(RedColor(I + 1), GreenColor(I + 1), _
        BlueColor(I + 1))
    Next I
End Sub

Run the example program by pressing F5. When you click the first Command
Button control, the program draws the black-to-blue gradient background
horizontally across the form. Clicking the second Command Button control
instructs the program to draw the gradient background vertically down the
form.

Additional References
"Color Palettes." (Library, Product Documentation, SDKs, Win32 SDK,
   Win32 Programmer's Reference, Overviews, Windows Management,
   Graphics Device Interface, Colors, About Colors)
Knowledge Base Q128637. "How to Draw a Gradient Background."
"RealizePalette QuickInfo Overview Group." (Library,
   Product Documentation, SDKs, Win32 SDK, Win32 Programmer's Reference,
   Reference, Functions, ReadEventLog to RemoveMenu)


Return