[英]How do I set the Font color of a label to the same as the caption color of a GroupBox?
我希望在表格上有一些標簽,其字體顏色與我的組框上的標題相同,而且如果用戶在其系統上應用了不同的主題,我希望這些顏色更改。
我可以在不更改默認的GroupBox標題的情況下執行此操作嗎?
更新:
我已經嘗試將Label ForeColor設置為ActiveCaption,這對於默認(藍色)方案看起來沒問題,但是當我將方案更改為Olive Green時,標簽和組框標題不一樣。
此外,GroupBox正常行為是將FlatStyle設置為Standard將標題顏色設置為ForeColor,但是要創建新的GroupBox並將其ForeColor設置為ControlText,您必須首先將其設置為ControlText以外的其他內容,然后再將其設置回來。 (如果你不遵循我的意思,那就試試吧。)
嗯,同樣的問題? 我會重復我的帖子:
using System.Windows.Forms.VisualStyles;
...
public Form1()
{
InitializeComponent();
if (Application.RenderWithVisualStyles)
{
VisualStyleRenderer rndr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
Color c = rndr.GetColor(ColorProperty.TextColor);
label1.ForeColor = c;
}
}
從它的外觀來看,GroupBox使用SystemColors.ActiveCaption繪制它的Caption,但你必須用其他主題來檢查它。
奇怪的是,這並沒有反映在ForeColor屬性中,但是如果你設置了它接管的屬性。
所以答案是:
private void Form1_Load(object sender, EventArgs e)
{
label1.ForeColor = SystemColors.ActiveCaption;
}
標簽公開ForeColorChanged事件。 然后你可以做這樣的事情:
this.label1.ForeColorChanged += (o,e) => { this.groupBox1.ForeColor = this.label1.ForeColor;};
但是,如果您嘗試檢測用戶何時更改其主題,則可以掛鈎可以在Microsoft.Win32命名空間中找到的SystemEvents。 像這樣的東西:
Microsoft.Win32.SystemEvents.UserPreferenceChanged += new Microsoft.Win32.UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);
void SystemEvents_UserPreferenceChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
{
this.groupBox1.ForeColor = this.label1.ForeColor;
}
我假設您使用Windows窗體而不是WPF。 當您應用顏色時使用系統顏色(例如Control或HighlightText),當用戶切換Windows主題時,這些顏色將被更改。 以下是將組框顏色設置為系統顏色的代碼,然后將此顏色應用於標簽:
groupBox1.ForeColor = SystemColors.ActiveBorder;
label1.ForeColor = groupBox1.ForeColor;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.