SetTextColor API to Print VB Text in Color to Color Printer

ID Number: Q84269

1.00

WINDOWS

Summary:

Visual Basic for Windows does not directly support printing text in

color to a color printer. To print in color, you must first make a

call to the Windows API function SetTextColor. The example below shows

how to implement this call to SetTextColor into a Visual Basic

application to allow for printing of colored text.

This is a known limitation with Microsoft Visual Basic programming

system version 1.0 for Windows.

More Information:

The ForeColor property of the Printer object was not fully implemented

in Visual Basic version 1.0. You can set the property, but the setting

has no effect in version 1.0.

To send color output to a color printer, you must use the Windows API

function call SetTextColor instead of the ForeColor property of the

Printer object.

Do the following to print "Hello" in all of the 16 QBColors:

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. In the Form1 global module, add the following:

'Note the following should be all on one line:

Declare Function SetTextColor Lib "GDI" (ByVal hDC As Integer,

ByVal crColor As Long) As Long

3. In the Form1 Form_Click event procedure, add the following:

Sub Form_Click

For i = 0 to 15

x& = SetTextColor(Printer.HDC, QBColor(i))

Printer.Print "Hello"

Next i

Printer.EndDoc

End Sub

4. Press F5 to run the program and click on the form.

The word "hello" will print in 16 different colors.

Additional reference words: 1.00