?
Solved

Printing rotated text and setting orientation

Posted on 2003-03-09
20
Medium Priority
?
641 Views
Last Modified: 2008-02-26
Hi all together!

First of all:

I am totally not the one just asking for code without knowing what it's all about. But as i said before and now again in other words: i think it is better for my health if i post it here :) I have just about enough of this topic, honestly !

All my points go into this question but im waiting for the administratives to give me back 65 points from the previous version of this question which was badly formed by me and therefore got no answer. The one who answers this question successfully, gets the 25 points and the 65 points AND all points i have until this day. That is my promise !!!!!


I have to make a program to control a printer and print packets, calculate contents and so on.

Unfortunatly, during work, i hit against a hard wall of a problem regarding rotated Text. To bring it into the picture box was totally no problem with the Win32 API TextOut() function. I even set the font information into it and so on. But because i want to print in good quality, i have to send the TextOut directly to the printer. That means that i have to control the
printer through a device context. The problem is, that i can not set the printer config.

Whenever i try to set for example the printer orientation, or things like the paper width or length, there occur problems. Therefore i want to ask you a concrete question, and i beg by heart for that someone would have a look at my problem; cause you know, when you work on one and the same thing all the time, and you can't get it to work, it costs a lot of nerves and
energy you put into it, and you even stop wanting to spend your time with it. I really tried long, and i tried a lot of things, but i did not succeed:

----------------------------------------------------------------

Is anyone able to provide me with a Visual Basic (6) routine, which can print a rotated text in a specific angle, font, and size, etc. on the standard printer of the system AND which is able to set the printer orientation, paperheight and paperlength ???

----------------------------------------------------------------

I'm looking forward to your help guys and girls.
Thanks in advance.

Cheers & Love.
0
Comment
Question by:spiritwithin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 2

Accepted Solution

by:
spauljoseph earned 420 total points
ID: 8101339
First of all thanks for your details question.

Please use the following.


'Create a New Project and add a Module and Form in it
'NOTE set the AutoRedraw property of Form to True and try this.
'Use the same function to select any object say PictureBox, Printer or something else you want. It will work fine with all.

'For PictureBox use the function call with argument as Picture1 (or the picture box name)
'For Printer use the function call with argument as Printer

'Paste the following code in the Module1 code window
Option Explicit

'Font Weight Parameters
Public Enum fntWeight
    FW_HEAVY = 900
    FW_BLACK = FW_HEAVY
    FW_BOLD = 700
    FW_SEMIBOLD = 600
    FW_DEMIBOLD = FW_SEMIBOLD
    FW_DONTCARE = 0
    FW_EXTRABOLD = 800
    FW_EXTRALIGHT = 200
    FW_LIGHT = 300
    FW_MEDIUM = 500
    FW_NORMAL = 400
    FW_REGULAR = FW_NORMAL
    FW_THIN = 100
    FW_ULTRABOLD = FW_EXTRABOLD
    FW_ULTRALIGHT = FW_EXTRALIGHT
End Enum
'Font Character Sets
Public Enum fntCharSet
    ANSI_CHARSET = 0
    DEFAULT_CHARSET = 1
    SYMBOL_CHARSET = 2
    SHIFTJIS_CHARSET = 128
    HANGEUL_CHARSET = 129
    CHINESEBIG5_CHARSET = 136
    OEM_CHARSET = 255
End Enum
'Font Output Precis
Public Enum fntOutPrecis
    OUT_CHARACTER_PRECIS = 2
    OUT_DEFAULT_PRECIS = 0
    OUT_DEVICE_PRECIS = 5
    OUT_OUTLINE_PRECIS = 8
    OUT_RASTER_PRECIS = 6
    OUT_STRING_PRECIS = 1
    OUT_STROKE_PRECIS = 3
    OUT_TT_ONLY_PRECIS = 7
    OUT_TT_PRECIS = 4
End Enum
'Font Clip Precis
Public Enum fntClipPrecis
    CLIP_CHARACTER_PRECIS = 1
    CLIP_DEFAULT_PRECIS = 0
    CLIP_EMBEDDED = 128
    CLIP_LH_ANGLES = 16
    CLIP_MASK = &HF
    CLIP_STROKE_PRECIS = 2
    CLIP_TO_PATH = 4097
    CLIP_TT_ALWAYS = 32
End Enum
'Font Quality
Public Enum fntQuality
    DEFAULT_QUALITY = 0
    PROOF_QUALITY = 2
    DRAFT_QUALITY = 1
End Enum
'Font Pitch
Public Enum fntPitch
    DEFAULT_PITCH = 0
    FIXED_PITCH = 1
    VARIABLE_PITCH = 2
End Enum
'Font Family
Public Enum fntFamily
    FF_DECORATIVE = 80
    FF_DONTCARE = 0
    FF_MODERN = 48
    FF_ROMAN = 16
    FF_SCRIPT = 64
    FF_SWISS = 32
End Enum
'Font Face Size
Public Enum fntFace
    LF_FACESIZE = 32
    LF_FULLFACESIZE = 64
End Enum
'Font Logging
Public Type fntLog
    lfHeight As Long
    lfWidth As Long
    lfEscapement As Long
    lfOrientation As Long
    lfWeight As Long
    lfItalic As Byte
    lfUnderline As Byte
    lfStrikeOut As Byte
    lfCharSet As Byte
    lfOutPrecision As Byte
    lfClipPrecision As Byte
    lfQuality As Byte
    lfPitchAndFamily As Byte
    lfFaceName(1 To LF_FACESIZE) As Byte
End Type
'Font Text Metrics
Public Type fntTextMetric
    tmHeight As Long
    tmAscent As Long
    tmDescent As Long
    tmInternalLeading As Long
    tmExternalLeading As Long
    tmAveCharWidth As Long
    tmMaxCharWidth As Long
    tmWeight As Long
    tmOverhang As Long
    tmDigitizedAspectX As Long
    tmDigitizedAspectY As Long
    tmFirstChar As Byte
    tmLastChar As Byte
    tmDefaultChar As Byte
    tmBreakChar As Byte
    tmItalic As Byte
    tmUnderlined As Byte
    tmStruckOut As Byte
    tmPitchAndFamily As Byte
    tmCharSet As Byte
End Type

Public Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal Height As Long, ByVal Width As Long, ByVal Escapement As Long, ByVal Orientation As Long, ByVal Weight As fntWeight, ByVal Italic As Long, ByVal Underline As Long, ByVal StrikeOut As Long, ByVal CharSet As fntCharSet, ByVal OutputPrecision As fntOutPrecis, ByVal ClipPrecision As fntClipPrecis, ByVal Quality As fntQuality, ByVal PitchAndFamily As Long, ByVal Face As String) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Public Sub DrawAngText(ByVal objObject As Object, fntAngFont As StdFont, strAngText As String, sngAngle As Single, Optional ByVal lngX As Long, Optional ByVal lngY As Long)
    Dim lngNewFont As Long
    Dim lngOldFont As Long
    Dim lngAngWeight As fntWeight

    If fntAngFont.Bold = True Then
        lngAngWeight = FW_BOLD
    Else
        lngAngWeight = FW_NORMAL
    End If
    lngNewFont = CreateFont(fntAngFont.Size * 2, 0, sngAngle * 10, 0, lngAngWeight, fntAngFont.Italic, fntAngFont.Underline, fntAngFont.Strikethrough, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH Or FF_DONTCARE, fntAngFont.Name)
    lngOldFont = SelectObject(objObject.hDC, lngNewFont)
    objObject.ForeColor = vbBlack
    objObject.CurrentX = lngX
    objObject.CurrentY = lngY
    objObject.Print strAngText
    lngNewFont = SelectObject(objObject.hDC, lngOldFont)
    DeleteObject lngNewFont
End Sub




'Paste the following code in the Form1 code window

Option Explicit

Private Sub Form_Load()
    Dim fntFont As New StdFont
   
    With fntFont
        .Name = "Arial"
        .Size = 12
    End With

    DrawAngText Me, fntFont, "Test Text", 90, 100, 1500
End Sub
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8102999
The problem using rotated text with the printer (as opposed to on screen drawing) is documented here, along with the fix:

PRB: Printing Rotated Text with Visual Basic
http://support.microsoft.com/default.aspx?scid=kb;en-us;175535

Specifically: "If you use TextOut with "Printer.hDC" instead of a local variable, it will fail. Any reference to a printer object property or method is a use of the printer object and will cause the printer object to reset its font/pen/brush."

So the trick is to store the Printer.hdc in a local variable and do all your printing using that variable.

0
 
LVL 1

Author Comment

by:spiritwithin
ID: 8103208
Thanks to you two!

Paulhews: I appreciate your help, but the problem i face, has nothing to do with the Printer.hDC, because i do not use it anyway. I use CreateDC() for creating me Device Context. However, i thank you for your tip.

spauljoseph: Thanks to you too. I think your code is exactly what i need. But i have to test it within my application. Also, i wait for the community support to move the points so it will take some days until i can accept your's as an answer. But it seems that you did the job. For now: Thanks a lot guys!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:spiritwithin
ID: 8106744
Alright. I am so for done with testing, but my results are not the best.

I had to face the problem that your function, spauljoseph, does not print out text on the printer correctly. At least when i include it into a blank project, and test it, it works for a form, or a pBox, but not for the printer. The printer output is small and unrotated, font size or rotation do not affect it. Do you know a solution for this problem?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8107190
Read the kb article.  You may understand better.
0
 
LVL 1

Author Comment

by:spiritwithin
ID: 8107237
Thanks mate. Sorry, but i did that. I think there is a misunderstanding regarding the fact that the text output problem is not the same as the problem described in the article, at least i think so. TextOut does not fail, but the font settings do not work for the printer, they only work for visual elements.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8107374
>>The printer output is small and unrotated, font size or rotation do not affect it. Do you know a solution for this problem? <<

My comments refer to this statement, which describes the problem documented in the link:


lngOldFont = SelectObject(objObject.hDC, lngNewFont)
objObject.ForeColor = vbBlack
objObject.CurrentX = lngX
objObject.CurrentY = lngY
objObject.Print strAngText

Any reference to the printer object (objObject in this case) will cause the font to be reset.  If you wish to print using the API, you either have to store all printer info in variables and access these only after you call selectobject or use the API exclusively.  I've had some success using a combination of the printer object (esp for setting orientation and such) and API for printing rotated text, but you have to follow the guidelines as mentioned in the link.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8107390
Like this:

Dim lngPrintHDC as long
.....
objObject.ForeColor = vbBlack
objObject.CurrentX = lngX
objObject.CurrentY = lngY
lngPrintHDC = objObject.hDC
lngOldFont = SelectObject(lngPrintHDC, lngNewFont)
objObject.Print strAngText
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8107400
You may be more comfortable using pure API methods...described in the link. ;)
0
 
LVL 1

Author Comment

by:spiritwithin
ID: 8107405
Now things are more clear to me.

I got the same results as you, by following those guidelines. I call my configurePrinter() function right after calling the StartDoc() API Function. It works and the text output is rotated correctly. As you say.

But now i face another problem. Because i can't just replace the printer DC with a pictureBox DC, I have to use different routines for printing out the text on the pBox than on the printer. And that creates the new problem of the font sizes and measures being totally different!

How can i print into a pictureBox with the same routine as described in the link?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8111371
You can merge them into the same routine with just a little logical branching.  All the statements used to print on the printer will work on any graphic dc.
0
 
LVL 1

Author Comment

by:spiritwithin
ID: 8111436
Yes. But without having the same measurements, for a printmanagement application it is simply no solution. What if my client wants to print address labels and finds them being printed on 10.000 pieces of paper, but much too big. You know what i mean?
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8111740
Ok.  That's why it's important to keep the same method of printing for screen and printer.  You can use the SetMapMode API to change the mapping mode to a logical mode for printing text (such as points) because pixel measurements do not make much sense for a print application.  Then you can judge your paper size and font sizes on-screen the same as you would on paper.  One point is 1/72 of an inch.  It will not appear that way on screen, but that is not important to your printing application as long as you keep everything in the same scale.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8111767
I wish I had some time to cobble together a sample for you, because I know first hand what a PITA this stuff is.  I very much recommend Dan Appleman's Guide to the Win32 API as it contains all the info you need for this kind of stuff.
0
 
LVL 1

Author Comment

by:spiritwithin
ID: 8111799
Right. I had to face that fact already - fortunatly. I now do use the same methods for printer and screen. This SetMapMode() thing sounds quite nice, i will have a look at it, that might help me out. Thanks to you.
0
 

Expert Comment

by:modulo
ID: 8163639
Added 65 points from:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20534384.html

modulo

Community Support Moderator
Experts Exchange
0
 
LVL 1

Author Comment

by:spiritwithin
ID: 8177790
I finally figured out what was needed. I used the above function by spauljoseph, but i had to change a lot of things because the printers output and the picture box's output were totally different in scales. However, my problem was not based on the problem described in the MSKB Article provided by PaulHews. I do anyway thank you both a lot. Points go anyway to spauljoseph. I hope you are all positive with my decision. If not, let me know and we will find a way. Thanks a lot for your help people!!!

Cheers
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 8178139
Glad you got a resolution. :) Cheers.
0
 
LVL 2

Expert Comment

by:spauljoseph
ID: 8178755
I got a new idea from your question. Thanks a lot for acceptance.
0
 
LVL 1

Author Comment

by:spiritwithin
ID: 8179523
And thanks again to you two! Im glad it solved out. I was stuck with this ***** too long :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question