Re: Find The Text :-)



"Ivar" <Ivar.ekstromer000@xxxxxxx> wrote in message news:5iQoi.638$mZ5.378@xxxxxxxxxxxxxxxxxxxxxxx

Hi ya Mike :-) It's bed time for lil ol me, so I don't have time
at the mo to test out what you posted. But I think you have
forgotten my golden rule for VB programming, No ActiveX,
no external references.

In that case you're just gonna' have to do it the hard way ;-) In fact personally I would suggest writing your own "wrap" code anyway (instead of using the DrawText API to both wrap it and draw it) because you will get much more control that way and much more flexibility, including the ability to do things that the DrawText API cannot do. I mean, if you're going to do it the hard way then you might as well do it properly ;-) For example you could include the ability to perform full justification (an often needed requirement) as well as the standard left, right and centre justification.

If you don't fancy writing the "wrap" code yourself then a very simple alternative would be to use a borderless standard VB TextBox Control and set its size to the same as your required "wrap rectangle" and just dump your string into its Text property. You can then send it an EM_GETLINECOUNT to count the number of wrapped lines and EM_GETLINE messages to get those lines into a VB array of Strings. You can then very easily draw those individual strings onto any device. When doing so the Left, right and centre justifications will effectively be a "one liner" and the full justification will be almost as easy (I can think of at least three different methods to easily perform that task). Here's some code (below) that performs the first task for you (getting the individual lines from the TextBox into an array of Strings). You can draw the final output either to a Form or a Picture Box or a Printer, although for a Printer you would probably be better off doing the "wrap" manually in code so as to perform the wrap at exactly the same "end of line words" as would a standard word processor when outputting the same text to the same printer (which is often different on a printer than it is on the display because of the different dpi settings, and you cannot send an EM_SETTARGET message to a standard TextBox in the way that you can to a RichTextBox). It would still wrap fine though on all devices even using this simple TextBox method, and you would still be able to get accurate left, right, centre and full justification on all devices. Once you've got your individual lines of text in the array of Strings it is a piece of cake to draw them and to find a word or a phrase and to determine its exact pixel position in the printed output (your original requirement).

Anyway, here's some "get the lines" code to start you off (on a Form with a ListBox and a TextBox):

Mike

Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg _
As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_GETLINE = &HC4

Private Sub CheckText()
Dim nLines As Long, usedLines As Long
Dim nLength As Long, n As Long
Dim sBuffer As String * 100, sInitial As String * 2
Dim LineArray() As String
sInitial = Chr$(100 - 1) & Chr(0)
nLines = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
ReDim LineArray(1 To nLines)
For n = 1 To nLines
Mid(sBuffer, 1, 2) = sInitial
nLength = SendMessage(Text1.hwnd, EM_GETLINE, _
n - 1, ByVal sBuffer)
LineArray(n) = Trim(Left(sBuffer, nLength))
Next n
For usedLines = nLines To 1 Step -1
If LineArray(usedLines) <> "" Then Exit For
Next usedLines
ReDim Preserve LineArray(1 To usedLines)
For n = 1 To UBound(LineArray)
List1.AddItem LineArray(n)
Next n
' Note: an empty string in the final array
' indicates a blank line.
End Sub

Private Sub Form_Load()
' Set the Text Box Multiline property to True
' at design time.
Me.ScaleMode = vbPixels
Text1.BorderStyle = vbBSNone
Text1.Width = 100
Text1.Height = 400
Text1.Text = "This is some text just to check that the routine " _
& "to return individual lines exactly as displayed in a Text " _
& "Box works okay." & vbCrLf & vbCrLf + "Okay!"
CheckText
End Sub




.



Relevant Pages

  • Re: Measuring Text in TextBox - MeasureString "falls short"!
    ... quickly ran into problems related to accurately measuring strings ... destined for the dreaded TextBox. ... Turns out you can't use .Net to accurately measure strings for controls ... private const int EM_SETMARGINS = 0xD3; ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: How to call HTML (with POST) page from Web form
    ... private mshtml.HTMLDocument ieDoc; ... //use the following for getting at HTML in the document. ... You can see I've wrapped the IE Navigate method just to check for null ... strings and other validations. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: OnPaint automatically call e.Graphics.Clear()?
    ... private Brush _brush = Brushes.Black; ... I expect the OnPaint method will do nothing after the first call. ... my form contains many individual text strings. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Rendering a font
    ... the strings must fillthe entire screen (Therefore I must strech the fonts ... Derive a new font based on the new scaling ... private String _sampleString; ... private int _scaledWidth; ...
    (comp.lang.java.gui)
  • Re: Rendering a font
    ... > the strings must fillthe entire screen (Therefore I must strech the fonts ... Derive a new font based on the new scaling ... > private String _sampleString; ... > private int _scaledWidth; ...
    (comp.lang.java.gui)