G'day all,
I was wondering if anyone knows if there are any APIs that print text vertically and horizontally aligned within a given rectangular area?
Printable View
G'day all,
I was wondering if anyone knows if there are any APIs that print text vertically and horizontally aligned within a given rectangular area?
Have you checked the API sites
http://www.allapi.net/
http://www.vbapi.com/
I'm not sure if you mean text placement or text rotation to vertical. Here is both:
Text placement using TextOut api:
Code:'This Project needs
'- two timers, interval=100
'- a button
'in general section
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = True
Timer2.Interval = 100
Timer2.Enabled = True
Command1.Caption = "Draw Text"
End Sub
'This will draw an Ellipse on the active window
Sub Timer1_Timer()
Dim Position As POINTAPI
'Get the cursor position
GetCursorPos Position
'Draw the Ellipse on the Screen's DC
Ellipse GetWindowDC(0), Position.x - 5, Position.y - 5, Position.x + 5, Position.y + 5
End Sub
Sub Command1_Click()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim intCount As Integer, strString As String
strString = "Cool, text on screen !"
For intCount = 0 To 30
'Draw the text on the screen
TextOut GetWindowDC(0), intCount * 20, intCount * 20, strString, Len(strString)
Next intCount
End Sub
Private Sub Timer2_Timer()
'Draw the text to the active window
TextOut GetWindowDC(GetActiveWindow), 50, 50, "This is a form", 14
End Sub
This is font rotation - printing in a PictureBox - PB
Code:from vb2themax.com:
Const LF_FACESIZE = 32
Private Type LOGFONT
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 As String * LF_FACESIZE
End Type
Private Declare Function SetGraphicsMode Lib "gdi32" (ByVal hdc As Long, _
ByVal iMode As Long) As Long
Private Declare Function MulDiv Lib "Kernel32" (ByVal nNumber As Long, _
ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, _
ByVal nIndex As Long) As Long
Private Declare Function CreateFontIndirect Lib "gdi32" Alias _
"CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As _
Long
Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (dest As _
Any, Source As Any, ByVal bytes As Long)
' Print rotated text
' The first argoment can be a form, a picture box, the Printer, and in general
' any VB object that supports the Font and the hDC properties.
' Text is the string to be printed
' Angle is the orientation, in 10th of degrees (default is 90°)
' X and Y are the printing coordinates (omit to use the current coordinates)
'
' Note: you get best results when using TrueType fonts
Sub PrintRotatedText(PB As Object, ByVal Text As String, _
Optional ByVal Angle As Integer = -900, Optional x As Variant, _
Optional y As Variant)
Dim hfont As Long, holdfont As Long
Dim Font As LOGFONT
Const GM_ADVANCED = 2
Const LOGPIXELSY = 90
SetGraphicsMode PB.hdc, GM_ADVANCED
' Create a Font object, similar to the current font in PB
' but with a different orientation
Font.lfHeight = -MulDiv(PB.FontSize, GetDeviceCaps(PB.hdc, LOGPIXELSY), 72)
Font.lfWidth = 0
Font.lfEscapement = Angle
Font.lfOrientation = Angle
Font.lfWeight = IIf(PB.FontBold, 700, 400)
Font.lfItalic = IIf(PB.FontItalic, 1, 0)
Font.lfUnderline = IIf(PB.FontUnderline, 1, 0)
Font.lfStrikeOut = IIf(PB.FontStrikethru, 1, 0)
Font.lfCharSet = 0
Font.lfOutPrecision = 0
Font.lfClipPrecision = 0
Font.lfQuality = 2
Font.lfPitchAndFamily = 33
Font.lfFaceName = PB.FontName & vbNullChar
hfont = CreateFontIndirect(Font)
holdfont = SelectObject(PB.hdc, hfont)
' Account for X,Y coordinates
If Not IsMissing(x) Then PB.CurrentX = x
If Not IsMissing(y) Then PB.CurrentY = y
' do the printing
PB.Print Text
' reselect the old font
SelectObject PB.hdc, holdfont
' destroy the font object just created
DeleteObject hfont
End Sub
Thanks for the replies,
I guess I should have explained the situation a bit more completely.
I have written a control loosely based on a grid concept.
each cell of the grid amongst other things has a text attribute, a vertical alignment attribute, a horizontal alignment attribute and a wordwrap attribute.
I have a function that displays the text within each cell correctly, (using GetTextExtentPoint32) but I am certain it isn't the most efficient implementation. Since wordwrap is so common (control buttons, label captions, text boxes etc) I was hoping there would be an API ( TextOutWrap perhaps) that would accept the arguments/parameters of a rect, a string, a vertical alignment, and a horizontal alignment.
Any ideas?
(I apologise if 2 versions of this reply turn up)
no
actually, yes.
Look up DrawText on MSDN. You can give it a RECT structure to draw your text within, and tell it to word-wrap, amongst other things.
r u brainless?Quote:
Originally posted by Motxopro
no
there is no need for that ffs
he doesn't care if you don't have any ideas and neither does anyone else.
actually maybe i care, because i'm posting this now, to give u a piece of my mind, because i've noticed you never think before you post!
The DrawText API is certainly an easier than the way I was doing it.
But (there's always a 'but' isn't there!) now what I need to do is draw several strings consecutively within the same rectangle formatted as if it was a single string. For instance a sentence with each word in a different colour.
what I get at the moment with DrawText is each word written over the top of each other as they all start printing at the same place
Is there some way of stating a start point for (as with TextOut) and some way of knowing where the drawn string ends so to know where the next string should start?
so I guess what I am looking for is an API that behaves like DrawText but allows me to specify a start point, and returns its end point
Nope, sorry. You'll have to write all the code to handle it yourself unfortunately.
How about something like this:VB Code:
Const TA_BASELINE = 24 Const TA_BOTTOM = 8 Const TA_CENTER = 6 Const TA_LEFT = 0 Const TA_NOUPDATECP = 0 Const TA_RIGHT = 2 Const TA_TOP = 0 Const TA_UPDATECP = 1 Const TA_MASK = (TA_BASELINE + TA_CENTER + TA_UPDATECP) Private Declare Function SetTextAlign Lib "gdi32" (ByVal hdc As Long, ByVal wFlags As Long) As Long Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long Const mStr = "www.allapi.net" Private Sub Form_Paint() 'KPD-Team 2000 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] 'set the form's scalemode to pixels Me.ScaleMode = vbPixels 'set the forecolor to white Me.ForeColor = vbWhite 'draw some lines to show where our TextOut-x- and y-parameters are Me.Line (100, 0)-(100, 100) Me.Line (100, 100)-(0, 100) Me.Line (150, 0)-(150, 150) Me.Line (150, 150)-(0, 150) 'set the forecolor back to black Me.ForeColor = vbBlack 'call textalign to align to the right SetTextAlign Me.hdc, TA_RIGHT Or TA_TOP Or TA_NOUPDATECP 'show some text TextOut Me.hdc, 100, 100, mStr, Len(mStr) 'call textalign to align to the left SetTextAlign Me.hdc, TA_LEFT Or TA_TOP Or TA_NOUPDATECP 'show some text TextOut Me.hdc, 150, 150, mStr, Len(mStr) End Sub
Use the DrawText function, and specify DT_CENTER as the flag.
To jim mcnamara
I have tried the above code, but it just prints it horizontally anyway. I realize you got the code from vb2themax, but does it work for you?