Draw text in .net control with negative background color - blend

I would like to draw a progress indicator with a percentage in its center, but I would like the text to have a negative (contrast) background color .

Thus, the part of the text that is above the filled part of the ProgressBar will be white, and the part above the unfilled part will be white.

I could do it simply by "cheating"

  • draw the black part of the text first.
  • draw a stroke rectangle (it will cover the part that should be hidden)
  • draw white text only along the stroke rectangle (crop it)

The performance effect when drawing text twice in this application is negligible, but I wonder if there is an easy way to do this in just two steps (for example, if the progress bar somehow inverts already drawn text) using blending .

+5
source share
2 answers

What you do is beautiful. Older tricks, such as SetROP2 (), no longer work with text receiving anti-aliasing, especially with ClearType rendering. Getting the smoothing pixels with the wrong color is very noticeable. Graphics.CompositingMode accordingly does not support effects anymore.

+4
source

XOR () ;

Public Function InvertColor(color As Long) As Long
    Return (color Xor &HFFFFFF)
End Function
0

All Articles