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.
Printable View
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.
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:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) MyBase.OnPaint(e) 'Inverse of the background color Dim inverse As Color = Color.FromArgb(255 - CInt(Me.BackColor.R), _ 255 - CInt(Me.BackColor.G), _ 255 - CInt(Me.BackColor.B)) Using b As New SolidBrush(inverse) e.Graphics.DrawString("My text color is the inverse of the background's", _ Me.Font, _ b, 25, 25) End Using End Sub
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.
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:
Dim rectNotesPrintingArea As New RectangleF(intLeftMargin, intTopMargin, intAreaWidth, intAreaHeight) rectNotesPrintingArea.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY) intNoteWidth = CInt(e.Graphics.MeasureString(strNotes, fntNote, New SizeF(rectNotesPrintingArea.Width, rectNotesPrintingArea.Height), fmt, intNoteCols, intNoteLines).Width) Dim rectNoteBackground As New RectangleF(intLeftMargin, intTopMargin, intNoteWidth, intNoteLines * fntNote.Height) rectNoteBackground.Offset(-e.PageSettings.HardMarginX, -e.PageSettings.HardMarginY) e.Graphics.FillRectangle(Brushes.Black, rectNoteBackground) e.Graphics.DrawString(strNotes, fntNote, white, rectNotesPrintingArea, fmt) intTopMargin += intNoteLines * fntNote.Height