9

I have a ProgressBar control like the following two:

enter image description here

The first is painted properly. As you can see, the second only has one 0, it's supposed to have two but the other cannot be seen because ProgressBar's ForeColor is the same as the TextColor. Is there a way I can paint the text in black when the ProgressBar below is painted in Lime and paint the text in Lime when the background is black?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Asad Ali
  • 684
  • 6
  • 17
  • Which progress Bar is it? Is it built-in or you're using some custom or third party control? – vendettamit Dec 03 '16 at 23:30
  • @vendettamit Sorry, forgot to specify, I'm using https://www.codeproject.com/tips/645899/csharp-alternative-progressbar – Asad Ali Dec 03 '16 at 23:38
  • 1
    If the text is one part it isn't possible unless you would use a bitmap and toggle pixels. If you can split it up in two parts you will need to decide if the split needs to divide characters. Not exactly easy in any way. Oh but for the old xor-ing draw modes.. The only cheap and easy solution I see is to compromise on the colors and not pick the same (brightness) for bar and text.. – TaW Dec 04 '16 at 00:06
  • 1
    Another trick would be to toggle not only the color but also the alignment with the value above or below 50% – TaW Dec 04 '16 at 00:14

1 Answers1

17

You can first draw the background and text, then draw the foreground lime rectangle using PatBlt method with PATINVERT parameter to combine foreground drawing with background drawing:

enter image description here

enter image description here

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyProgressBar : Control
{
    public MyProgressBar() 
    {
        DoubleBuffered = true;
        Minimum = 0; Maximum = 100; Value = 50;
    }
    public int Minimum { get; set; }
    public int Maximum { get; set; }
    public int Value { get; set; }
    protected override void OnPaint(PaintEventArgs e) 
    {
        base.OnPaint(e);
        Draw(e.Graphics);
    }
    private void Draw(Graphics g) 
    {
        var r = this.ClientRectangle;
        using (var b = new SolidBrush(this.BackColor))
            g.FillRectangle(b, r);
        TextRenderer.DrawText(g, this.Value.ToString(), this.Font, r, this.ForeColor);
        var hdc = g.GetHdc();
        var c = this.ForeColor;
        var hbrush = CreateSolidBrush(((c.R | (c.G << 8)) | (c.B << 16)));
        var phbrush = SelectObject(hdc, hbrush);
        PatBlt(hdc, r.Left, r.Y, (Value * r.Width / Maximum), r.Height, PATINVERT);
        SelectObject(hdc, phbrush);
        DeleteObject(hbrush);
        g.ReleaseHdc(hdc);
    }
    public const int PATINVERT = 0x005A0049;
    [DllImport("gdi32.dll")]
    public static extern bool PatBlt(IntPtr hdc, int nXLeft, int nYLeft,
        int nWidth, int nHeight, int dwRop);
    [DllImport("gdi32.dll")]
    public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
    [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    public static extern bool DeleteObject(IntPtr hObject);
    [DllImport("gdi32.dll")]
    public static extern IntPtr CreateSolidBrush(int crColor);
}

Note: The controls is just for demonstrating the paint logic. For a real world application, you need to add some validation on Minimum, Maximum and Value properties.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398