Results 1 to 4 of 4

Thread: [RESOLVED] Drawstring invert background and font colors

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Resolved [RESOLVED] Drawstring invert background and font colors

    I am trying to figure out how to print a string of text inverted so the background would be black, and the text would be white. Is there any way to do this using drawstring? Been looking around, but am not getting anywhere.

    Thanks.

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Drawstring invert background and font colors

    I think you're saying, if the background color is black draw the inverse (white). So whatever the background color is, draw the inverse.

    You can override the OnPaint method and do this:

    vb.net Code:
    1. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    2.         MyBase.OnPaint(e)
    3.         'Inverse of the background color
    4.         Dim inverse As Color = Color.FromArgb(255 - CInt(Me.BackColor.R), _
    5.                                               255 - CInt(Me.BackColor.G), _
    6.                                               255 - CInt(Me.BackColor.B))
    7.         Using b As New SolidBrush(inverse)
    8.             e.Graphics.DrawString("My text color is the inverse of the background's", _
    9.                                   Me.Font, _
    10.                                   b, 25, 25)
    11.         End Using
    12.  
    13.     End Sub

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Drawstring invert background and font colors

    There used to be a draw mode of "invert" in vb6 and earlier which would literally invert every pixel that is touched in drawing the text, which I assume is what is required here (rather than looking at a flat background colour and calculating the inverse of that and drawing as normal).

    I've never looked to do it in .Net but I'd imagine its still possible - can't find anything immediately but if you do a google search for the terms "drawmode" "invert" and "vb.net" you'll probably be able to come up with something.
    Last edited by keystone_paul; Sep 5th, 2009 at 04:03 PM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2008
    Posts
    84

    Re: Drawstring invert background and font colors

    Well the only method I could figure out was to measure how large the text was going to be using measurestring, and then draw a rectangle in that area with whatever color I wanted for the background, and then draw the text on top of that rectangle.

    There is probably a better way to do it, but it worked.

    vb Code:
    1. Dim rectNotesPrintingArea As New RectangleF(intLeftMargin, intTopMargin, intAreaWidth, intAreaHeight)
    2. rectNotesPrintingArea.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY)
    3. intNoteWidth = CInt(e.Graphics.MeasureString(strNotes, fntNote, New SizeF(rectNotesPrintingArea.Width, rectNotesPrintingArea.Height), fmt, intNoteCols, intNoteLines).Width)
    4. Dim rectNoteBackground As New RectangleF(intLeftMargin, intTopMargin, intNoteWidth, intNoteLines * fntNote.Height)
    5. rectNoteBackground.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY)
    6. e.Graphics.FillRectangle(Brushes.Black, rectNoteBackground)
    7. e.Graphics.DrawString(strNotes, fntNote, white, rectNotesPrintingArea, fmt)
    8. intTopMargin += intNoteLines * fntNote.Height

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width