简体   繁体   English

单击按钮时如何使其他控件看起来更暗?

[英]How to make other controls look darker when button is clicked?

I am trying to create an application with WinForms .NET 5.0 C#.我正在尝试使用 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.我有一个面板,当单击按钮A时,五个按钮出现在其他控件(按钮、标签等)上方。 What I want is to make the user only focus on those 5 five buttons.我想要的是让用户只关注这 5 个五个按钮。 And for that I need to make the rest of the controls in the panel and the panel itself a little darker.为此,我需要使面板中控件的 rest 和面板本身更暗一些。

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.对此的一种解决方案是,我可以使这些控件的背景颜色更暗一些,但为此我无法找到正确的 RGB 值或正确的颜色。

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).现实:当我单击按钮时,子窗体出现并显示按钮,但它是不透明的,它隐藏了它下面的其他控件(是的,我使用BringToFront()方法)。

So, could there be any other solution to this or am I missing something here?那么,是否有任何其他解决方案,或者我在这里遗漏了什么?

EDIT编辑

This is what I want to do:这就是我想要做的: 我的期望的形象

Here I did it manually with the help of 2 forms just for the sake of demo(one has low opacity).在这里,我在 2 forms 的帮助下手动完成,只是为了演示(一个不透明度低)。 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).您还可以看到link label通过按钮稍微可见(我不想要)。 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.为了使控件变暗,我需要处理我无法处理的 RGB 值。

I hope now you know what I mean...我希望你现在明白我的意思...

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!为您的项目配置它,让我知道它是如何进行的!

If you just want all other controls look darker, you can changing all other control it BackColor and it ForeColor to darker.如果您只想让所有其他控件看起来更暗,您可以将所有其他控件更改为BackColorForeColor为更暗。

Firstly, you need some class property to storing all other control it BackColor and ForeColor before changing it, remember using System.Collections.Generic .首先,在更改它之前,您需要一些 class 属性来存储所有其他控件BackColorForeColor ,记住使用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 .注意:也许您想使用ControlPaint.Dark而不是我的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:有一些控件可以在BackColorForeColor旁边使用其他颜色,例如带有边框颜色的按钮,因此它不会变暗,您可以使用模式将其变暗:

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!注意:也许这不是你想要的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM