Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Color in the Printer object

Status
Not open for further replies.

simonkue

MIS
Jul 9, 2002
105
GB
I am using the printer object to print line by line a report. I am building up the report line in a variable string and then output the string directly to the printer. This allows me to specify variable gaps/columns for a multi-part report that I couldn't get to work another way!
The problem is that I need to print some figures/text in colour, while most of the report is black on white.
I cannot find a way to get characters 1-45 to print in black 50-55 to print in red and chars 75-78 in yellow.
Is there some way - via attribute bytes for example - that I can alter the way text is rendered within a string?
Can anyone help please?
 

You could also use the RichTextBox control. Set up everything the way you want it to look (recommend fixed width font) and use the selstart, sellength and selcolor properties to change the color of the font and then use the rtb to print.

Good Luck

 
Thanks for the ideas....
I've tried the RichTextBox and can't get it to work on the printer.
It displays exacty as I want on the form - Red background, black text, but still prints with a white background.
I can get colour onto the print line by using the Forecolor property of the printer object - proving that the printer is capable (!) but ideally, I'd like to print black text over a yellow or red background.
Any ideas?
I've also had a look at the TextOut api and it seems suited to graphical display/output. Can it cope with line by line output to a printer and does this mean that I'll need to rewrite the entire print routine (many other lines are OK as black on white) so that all print for this report is routed through this api?
 
re TextOut
You should be able to mix the two. For what its worth this is one subroutine I use. The bit which scales the x and y for a printer is a bit duff.

Sub PrintRotatedString(hPrintDc As Long, OutString As String, X As Long, Y As Long, lFontSize As Single, cFontName As String, lWeight As Long, lItalic As Long, lUnderline As Long, lAngle As Long, crColor As Long, lPrinter As Long, fZoom As Double)

Dim LF As LOGFONT 'Structure for setting up rotated font
Dim temp As String 'Temp string var
Dim Result As Long 'Return value for calling API functions
Dim hOldfont As Long 'Hold old font information
Dim myX As Long
Dim myY As Long
Dim hFont As Long 'Handle to new Font
Const LOGPIXELSY = 90 ' Logical pixels/inch in Y

myX = X
myY = Y

If lPrinter Then
myX = myX * Screen.TwipsPerPixelX / (Printer.TwipsPerPixelX * fZoom) '0.01 * Doc.lZoom)
myY = myY * Screen.TwipsPerPixelY / (Printer.TwipsPerPixelY * fZoom) '0.01 * Doc.lZoom)

End If

Result = SetTextColor(hPrintDc, crColor)
Result = SetTextAlign(hPrintDc, TA_BASELINE) 'TA_TOP)

'Set rotation in tenths of a degree, i.e., 1800 = 180 degrees
LF.lfEscapement = lAngle * 10
If lPrinter Then
LF.lfHeight = -MulDiv(lFontSize, GetDeviceCaps(hPrintDc, LOGPIXELSY), 72) 'lFontSize
Else
LF.lfHeight = -MulDiv(lFontSize * fZoom, GetDeviceCaps(hPrintDc, LOGPIXELSY), 72) 'lFontSize
End If
LF.lfFaceName = cFontName & vbNullChar
LF.lfWeight = 400 + 300 * lWeight
LF.lfItalic = lItalic
LF.lfUnderline = lUnderline
hFont = CreateFontIndirect(LF) 'Create the rotated font
di.cbSize = 20 'Size of DOCINFO structure

'Select our rotated font structure and save previous font info
hOldfont = SelectObject(hPrintDc, hFont)

Result = TextOut(hPrintDc, myX, myY, OutString, Len(OutString))

Result = DeleteObject(hFont) 'Delete the font object

End Sub



 
petermeachem,

I've picked out the SetTextColor api and tried to run that on it's own without your code for font manipulation, however, I get the message "Printer Error "482" Overflow when I use the Printer.hDC. Certainly the number is large when viewed through the debugger, but it should be OK in a Long.
Any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top