Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleOverlay Rich Text on a picture in Windows XP
DescriptionThis example shows how to overlay Rich Text on a picture in Windows XP in Visual Basic.
KeywordsRich Text, RTF, RichTextBox, overlay, picture
CategoriesGraphics
 
For some reason, overlaying Rich Text directly on a PictureBox doesn't seem to work in Windows XP. To work around this, you can draw the text into a PictureBox and then use PaintPicture to overlay the result on the picture.

The program first creates some Rich Text. It then copies the Rich Text into a hidden RichTextBox control, make that control's text all black, and calls subroutine OverlayText to draw the text into a PictureBox.

This is a black mask that will be used to determine the parts of an image that should be drawn over the picture. The program uses PaintPicture to draw this mask on the picture, specifying the vbMergePaint opcode. That clears the parts of the mask image that are black and leaves the rest of the picture unchanged. The result is the picture has the text erased.

Next the program calls subroutine OvelayText to draw the original Rich Text (with its original colors) into a PictureBox. It then copies the result onto the picture with the vbSrcAnd opcode. That copies the text into the white areas previously cleared in the picture, giving the desired result.

 
Private Sub Form_Load()
    picHidden.AutoRedraw = True
    picHidden.BackColor = vbWhite
    picHidden.Cls
    picHidden.Picture = picHidden.Image

    ' Generate the Rich text.
    With rchOverlay.Font
        .Name = "Times New Roman"
        .Size = 20
        .Bold = True
    End With
    rchOverlay.SelColor = vbCyan
    rchOverlay.Text = "You can do all sorts of interesting " & _
        "things with graphics in Visual Basic. Everything " & _
        "from animation to 3D graphics."
    rchOverlay.SelStart = 0
    rchOverlay.SelLength = Len(rchOverlay.Text)
    rchOverlay.SelColor = vbBlue

    rchOverlay.SelStart = Len("You can do all sorts of ")
    rchOverlay.SelLength = Len("interesting")
    rchOverlay.SelColor = vbYellow
    rchOverlay.SelItalic = True

    rchOverlay.SelStart = Len("You can do all sorts of " & _
        "interesting things with graphics in ")
    rchOverlay.SelLength = Len("Visual Basic")
    rchOverlay.SelColor = vbWhite

    rchOverlay.SelStart = Len("You can do all sorts of " & _
        "interesting things with graphics in Visual Basic. " & _
        "Everything from ")
    rchOverlay.SelLength = Len("animation")
    rchOverlay.SelColor = vbYellow

    rchOverlay.SelStart = Len("You can do all sorts of " & _
        "interesting things with graphics in Visual Basic. " & _
        "Everything from animation to ")
    rchOverlay.SelLength = Len("3D graphics")
    rchOverlay.SelColor = vbYellow

    ' Make the mask.
    rchHidden.TextRTF = rchOverlay.TextRTF
    rchHidden.SelStart = 0
    rchHidden.SelLength = Len(rchHidden.Text)
    rchHidden.SelColor = vbBlack
    rchHidden.BackColor = vbWhite
    picHidden.Width = picCanvas.ScaleWidth
    picHidden.Height = picCanvas.ScaleHeight
    picHidden.Cls
    OverlayText picHidden, rchHidden, _
        picHidden.Width * 0.1, _
        picHidden.Width * 0.9, _
        picHidden.Height * 0.1, _
        picHidden.Height * 0.9
    picHidden.Picture = picHidden.Image

    ' Apply the mask.
    picCanvas.AutoRedraw = True
    picCanvas.PaintPicture picHidden.Picture, _
        0, 0, OpCode:=vbMergePaint

    ' Make the text image.
    picHidden.Cls
    OverlayText picHidden, rchOverlay, _
        picHidden.Width * 0.1, _
        picHidden.Width * 0.9, _
        picHidden.Height * 0.1, _
        picHidden.Height * 0.9
    picHidden.Picture = picHidden.Image

    ' Draw the text on top.
    picCanvas.PaintPicture picHidden.Picture, _
        0, 0, OpCode:=vbSrcAnd
    picCanvas.Picture = picCanvas.Image

    ' Draw a border.
    picCanvas.Line _
        (picCanvas.ScaleWidth * 0.075, _
        picCanvas.ScaleHeight * 0.075)- _
        (picCanvas.ScaleWidth * 0.925, _
        picCanvas.ScaleHeight * 0.925), vbWhite, B

    ' Display the form.
    Show
End Sub
 
The Windows message EM_FORMATRANGE makes a RichTextBox format text for display on a device. This program uses the SendMessage API function to send this message to a RichTextBox, telling it to display its contents on a PictureBox.

Subroutine OverlayText does all the work. It initializes a FORMATRANGE structure that identifies the PictureBox and the area where the text should be drawn. It sets the char_before and char_after values to 0 and -1 to indicate that all of the text should be drawn.

After drawing the text, the routine calls SendMessage again with a NULL final parameter to make the RichTextBox free resources it has allocated.

 
' Overlay the text on the PictureBox inside
' the specified rectangle (in twips).
Private Sub OverlayText(ByVal pic As PictureBox, ByVal rch _
    As RichTextBox, ByVal xmin As Single, ByVal xmax As _
    Single, ByVal ymin As Single, ByVal ymax As Single)
Dim format_range As FORMATRANGE

    ' Prepare the FORMATRANGE structure.
    With format_range
        .hdc = pic.hdc
        .target_hdc = pic.hdc
        With .entire_area
            .Left = 0
            .Right = pic.ScaleX(pic.ScaleWidth, _
                pic.ScaleMode, vbTwips)
            .Top = 0
            .Bottom = pic.ScaleY(pic.ScaleHeight, _
                pic.ScaleMode, vbTwips)
        End With
        With .target_area
            .Left = xmin
            .Right = xmax
            .Top = ymin
            .Bottom = ymax
        End With
        With .char_range
            .char_before = 0
            .char_after = -1
        End With
    End With

    ' Display as much text as will fit.
    ' Setting wParam = True makes the RichTextBox
    ' display the text instead of just measuring it.
    SendMessage rch.hwnd, _
        EM_FORMATRANGE, True, format_range

    ' Clear resources allocated by the RichTextBox.
    SendMessage rch.hwnd, EM_FORMATRANGE, False, ByVal 0&
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated