Home > other >  How to draw an RPG bar that passes values in .net framework, windows forms, c#
How to draw an RPG bar that passes values in .net framework, windows forms, c#

Time:12-30

How can I draw and get values from a moving bar?

My point is like setting a car, the car will go further depending on the velocity the user gives to him, my point is doing those old school bar with green, yellow and red, where the green is more power and the red less power, and you have an arrow going up and down, when I click sets an amount of power, how much close I am to the green more power I get.

Something like this where the arrow moves along the bar, and when I click it gives me an output:

enter image description here

CodePudding user response:

Here's a quick example:

public partial class Form1 : Form
{
    private int barJump = 5;
    private bool direction = true;
    private int barWidth = 5;
    private int position = 50;
    private ColorBlend cb = new ColorBlend();

    public Form1()
    {
        InitializeComponent();
        cb.Positions = new[] { 0, 0.5f, 1 };
        cb.Colors = new[] { Color.Green, Color.Yellow, Color.Red };
        timer1.Interval = 50;
        timer1.Enabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        timer1.Enabled = true;
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        using (LinearGradientBrush lgb = new LinearGradientBrush(pictureBox1.ClientRectangle, Color.Black, Color.Black, 0, false))
        {
            lgb.InterpolationColors = cb;
            e.Graphics.FillRectangle(lgb, pictureBox1.ClientRectangle);
        }
        int x = (int)(pictureBox1.ClientRectangle.Width * (double)position/100);
        Rectangle rc = new Rectangle(new Point(x, 0), new Size(1, pictureBox1.ClientRectangle.Height-1));
        rc.Inflate(barWidth, 0);
        e.Graphics.DrawRectangle(Pens.Black, rc);
    }

    private void pictureBox1_SizeChanged(object sender, EventArgs e)
    {
        pictureBox1.Invalidate();
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        if (timer1.Enabled && e.Button == MouseButtons.Left)
        {
            timer1.Stop();
            button1.Enabled = true;
            double percentage = (double)position/100;
            label1.Text = $"percentage = {percentage}";
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (direction)
        {
            if (position < 100)
            {
                position = Math.Min(100, position   barJump);
            }
            else
            {
                direction = false;
                position = Math.Max(0, position - barJump);
            }
        }
        else
        {
            if (position > 0)
            {
                position = Math.Max(0, position - barJump);
            }
            else
            {
                direction = true;
                position = Math.Min(100, position   barJump);
            }
        }
        pictureBox1.Invalidate();
    }
}

Example run:

enter image description here

  • Related