-1

I am trying to create an application with WinForms .NET 5.0 C#.

I have a panel in which, five buttons appear above the other controls(buttons, labels etc.) when button A is clicked. What I want is to make the user only focus on those 5 five buttons. And for that I need to make the rest of the controls in the panel and the panel itself a little darker.

One solution to this is that I can make the Back Color of those controls a little darker, but for that I am not able to find the right RGB values or the right color.

I also tried these steps to do so -

  1. Add the buttons and their code in a separate child form
  2. Make the child form black and reduce the child form's opacity(to make it a little transparent)
  3. Add the child form in the panel when the button is clicked

Expectations: I expected that since the child form's color is black and opacity is low, it would automatically create a dark background and highlight the buttons

Reality: When I clicked the button the child form appeared and also showed the buttons, but it was opaque and it hid the other controls beneath it(yes I used BringToFront() method).

So, could there be any other solution to this or am I missing something here?

EDIT

This is what I want to do: Image of my expectations

Here I did it manually with the help of 2 forms just for the sake of demo(one has low opacity). You can see that the background is dark. You can also see that the link label is slightly visible through the button(which I don't want). I want that only the background should be dark(and the button should be opaque) that's why I am not using another child form with it and there's one more reason why I am not using the child form(see above steps which I tried).

I can see only two options - Either make the controls behind it dark or follow your answers.

For making the controls dark, I need to handle the RGB values which I am not able to.

I hope now you know what I mean...

NoNAME
  • 65
  • 8

2 Answers2

1

include this bit of code near the start of the project

public int VARIABLE_NAME_HERE = 255

check when the button is clicked, then include this code:

VARIABLE_NAME_HERE - 50 //change the value by 50 each time program is run
BUTTON_NAME.Color = Color.FromArgb(COLOR_R_HERE, COLOR_G_HERE, VARIABLE_NAME_HERE);

configure this for your project and let me know how it goes!

emeraldXen
  • 41
  • 2
0

If you just want all other controls look darker, you can changing all other control it BackColor and it ForeColor to darker.

Firstly, you need some class property to storing all other control it BackColor and ForeColor before changing it, remember using System.Collections.Generic.

//The dictionary to contain the color.
private Dictionary<Control, KeyValuePair<Color, Color>> controlsColor = new Dictionary<Control, KeyValuePair<Color, Color>>();
//The list that contain the Control you don't want to be darken.
private List<Control> noDarkenControl = new List<Control>();
//The percentage to darken other control, make sure it between 0 and 100, here is 30 percentage.
private int darkPercent = 30;
//The form BackColor and ForeColor.
private Color formBackColor, formForeColor;

Then, you need a algorithm to darken our color, here I'm using linear algebra:

private Color DarkenColor(Color color, int percent){
    float red = color.R, green = color.G, blue = color.B;
    //Calculate the t value for using linear, so if percent = 100 then t = 0 and the result RGB is (0, 0, 0), with completely black, if percent = 0 then t = 1 and the result is the original color.
    float t = (100 - percent) / 100f;
    //The original is 0 + (red - 0) * t.
    red *= t;
    //The original is 0 + (green - 0) * t.
    green *= t;
    //The original is 0 + (blue - 0) * t.
    blue *= t;
    return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
}

Note: Maybe you want to use ControlPaint.Dark instead of my DarkenColor.

After that, inside your button click event:

//Renew the variable
controlsColor = new Dictionary<Control, KeyValuePair<Color, Color>>();
noDarkenControl = new List<Control>();
formBackColor = this.BackColor;
formForeColor = this.ForeColor;
//Assign the value
foreach (Control a in this.Controls){
    controlsColor.Add(a, new KeyValuePair<Color, Color>(a.BackColor, a.ForeColor));
}
//Assign the control that you didn't want to be darken, here is my button1 and button2 i don't want it to be darken.

//Bring the button to front.
button1.BringToFront();
button2.BringToFront();
//Assign
noDarkenControl.Add(button1);
noDarkenControl.Add(button2);
//Main code
foreach (Control a in this.Controls){
    if (!noDarkenControl.Contains(a)){
        a.BackColor = DarkenColor(a.BackColor, darkPercent);
        a.ForeColor = DarkenColor(a.ForeColor, darkPercent);
    }
}
this.BackColor = DarkenColor(a.BackColor, darkPercent);
this.ForeColor = DarkenColor(a.ForeColor, darkPercent);

There was some control that using other color beside BackColor or ForeColor like button with it Border Color so it not darken, you can darken it using the pattern:

Color_You_Want_To_Darken = DarkenColor(Color_You_Want_To_Darken, darkPercent);
//Or
Color_You_Want_To_Darken = ControlPaint.Dark(Color_You_Want_To_Darken, darkPercent);

But remember assign it color to a variable so you can change it color back to before darken it.

Finally, you will want to change all the color to the original:

foreach(Control a in controlsColor.Keys){
    a.BackColor = controlsColor[a].Key;
    a.ForeColor = controlsColor[a].Value;
}

Note: Maybe this is not what you want!

Higg Higg
  • 79
  • 8