Important alert: (current site time 6/20/2013 12:08:39 AM EDT)
 

VB icon

Draw Pages Using DrawTextExW

Email
Submitted on: 10/15/2012 5:22:16 PM
By: Je. 
Level: Intermediate
User Rating: By 1 Users
Compatibility: VB 5.0, VB 6.0
Views: 2139
 
     DrawTextExW (or the Ansi version DrawTextEx) returns the length drawn so can be used to easily draw (and print) pages of plain text.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Draw Pages Using DrawTextExW
' Description:DrawTextExW (or the Ansi version DrawTextEx) returns the length drawn so can be used to easily draw (and print) pages of plain text.
' By: Je.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=74587&lngWId=1'for details.'**************************************

'in a form
'PUT SOME TEXT ON THE CLIPBOARD THEN RUN
Private Type DRAWTEXTPARAMS
cbSize As Long
iTabLength As Long
iLeftMargin As Long
iRightMargin As Long
uiLengthDrawn As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Const DT_WORDBREAK = &H10, DT_EDITCONTROL = &H2000
Private Declare Function DrawTextExW Lib "user32" (ByVal hDC As Long, ByVal lpsz As Any, ByVal n As Long, lpRect As RECT, ByVal dwDTFormat As Long, lpDrawTextParams As Any) As Long
Private Declare Function setrect Lib "user32" Alias "SetRect" (lpRect As RECT, ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal X As Long, ByVal Y As Long) As Long
Private Sub Form_Load()
Dim dtt As DRAWTEXTPARAMS, r As RECT, st As String, n As Long, res As Long
With Me
.ScaleMode = vbPixels
.AutoRedraw = True
.ForeColor = vbBlack
End With
dtt.cbSize = Len(dtt)
setrect r, 10, 10, 300, 200
st = Clipboard.GetText
Do Until n >= Len(st) 'Loop till all pages drawn
With r 'DRAWBORDER
Me.Line (.Left, .Top)-(.Right, .Bottom), vbWhite, BF
Me.Line (.Left, .Top)-(.Right, .Bottom), vbBlack, B
End With
'ONE LINE PRINTS PAGE
res = DrawTextExW(Me.hDC, ByVal CLng(StrPtr(st) + (n * 2)), Len(st) - n, r, DT_WORDBREAK Or DT_EDITCONTROL, ByVal VarPtr(dtt))
n = n + dtt.uiLengthDrawn
OffsetRect r, 50, 50
Loop
Me.Refresh
End Sub


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

10/16/2012 2:13:04 AMBonnie West

Here's a small tip:

If a UDT doesn't have a String member, then it can be passed ByRef to either A or W versions of an API which requires a pointer to it.

Thus, ...DT_EDITCONTROL, ByVal VarPtr(dtt)) could simply be ...DT_EDITCONTROL, dtt)
(If this comment was disrespectful, please report it.)

 
10/17/2012 3:07:33 PMJe.

Yeah thanks, I'll leave people to change that if they wish. I ran some tests using SetRectEmpty and passing by ref instead of using Varptr seems to be a little faster compiled and a lot faster in the IDE.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.