簡體   English   中英

單擊按鈕時如何使其他控件看起來更暗?

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

我正在嘗試使用 WinForms .NET 5.0 C# 創建一個應用程序。

我有一個面板,當單擊按鈕A時,五個按鈕出現在其他控件(按鈕、標簽等)上方。 我想要的是讓用戶只關注這 5 個五個按鈕。 為此,我需要使面板中控件的 rest 和面板本身更暗一些。

對此的一種解決方案是,我可以使這些控件的背景顏色更暗一些,但為此我無法找到正確的 RGB 值或正確的顏色。

我也嘗試了這些步驟 -

  1. 在單獨的子窗體中添加按鈕及其代碼
  2. 使子窗體變黑並降低子窗體的不透明度(使其有點透明)
  3. 單擊按鈕時在面板中添加子窗體

期望:我希望由於子窗體的顏色是黑色且不透明度低,它會自動創建深色背景並突出顯示按鈕

現實:當我單擊按鈕時,子窗體出現並顯示按鈕,但它是不透明的,它隱藏了它下面的其他控件(是的,我使用BringToFront()方法)。

那么,是否有任何其他解決方案,或者我在這里遺漏了什么?

編輯

這就是我想要做的: 我的期望的形象

在這里,我在 2 forms 的幫助下手動完成,只是為了演示(一個不透明度低)。 你可以看到背景是暗的。 您還可以看到link label通過按鈕稍微可見(我不想要)。 我希望只有背景應該是暗的(並且按鈕應該是不透明的),這就是為什么我不使用另一個子表單的原因,還有一個我不使用子表單的原因(請參閱上面我嘗試過的步驟)。

我只能看到兩個選項 - 要么將其背后的控件變暗,要么按照你的答案。

為了使控件變暗,我需要處理我無法處理的 RGB 值。

我希望你現在明白我的意思...

在項目開始附近包含這段代碼

public int VARIABLE_NAME_HERE = 255

檢查單擊按鈕的時間,然后包含以下代碼:

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);

為您的項目配置它,讓我知道它是如何進行的!

如果您只想讓所有其他控件看起來更暗,您可以將所有其他控件更改為BackColorForeColor為更暗。

首先,在更改它之前,您需要一些 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;

然后,你需要一個算法來加深我們的顏色,這里我使用線性代數:

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);
}

注意:也許您想使用ControlPaint.Dark而不是我的DarkenColor

之后,在您的按鈕單擊事件中:

//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);

有一些控件可以在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);

但是請記住將它的顏色分配給一個變量,這樣您就可以在變暗之前將它的顏色改回。

最后,您需要將所有顏色更改為原始顏色:

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

注意:也許這不是你想要的!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM