翻譯|使用教程|編輯:龔雪|2023-11-22 10:55:36.137|閱讀 119 次
概述:本文將介紹如何使用Telerik UI for WinForms的Fluent Dark主題創建桌面應用程序的明暗模式,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
黑暗模式現在在很多應用程序中都挺常見的,但如何在桌面應用程序中實現它呢?這很簡單,本文將為大家介紹如何使用一個類和命令行調用來實現!
Telerik UI for WinForms擁有適用Windows Forms的110多個令人驚嘆的UI控件。所有的UI for WinForms控件都具有完整的主題支持,可以輕松地幫助開發人員在桌面和平板電腦應用程序提供一致美觀的下一代用戶體驗。
技術交流群:726377843 歡迎一起進群討論
為了實現黑暗主題,我們將使用Fluent Dark,對于day主題將使用Fluent,如果您喜歡也可以指定另一個主題。
下面是一個Fluent主題控件的示例:
下面是Fluent Dark Theme的一個示例:
在演示應用程序中,白天和夜晚主題看起來像這樣:
在您的主表單中,添加這行代碼:
public RadForm1() { InitializeComponent(); _ = new DayNight(this); }
創建并添加DayNight類到您的項目中:
using System; using Telerik.WinControls; using Telerik.WinControls.UI; namespace TelerikWinFormsApp1; internal class DayNight { private bool themeDay; private RadImageButtonElement signInButton; public DayNight(RadForm form) { new Telerik.WinControls.Themes.FluentTheme().DeserializeTheme(); new Telerik.WinControls.Themes.FluentDarkTheme().DeserializeTheme(); CreateDayNightButton(form); themeDay = Telerik.WinControls.WindowsSettings.AppsUseLightTheme; SetDayNight(); } private void CreateDayNightButton(RadForm form) { daynightButton = new RadImageButtonElement { ThemeRole = "TitleBarMinimizeButton", Text = "?", DisplayStyle = DisplayStyle.Text, ShowBorder = false, AutoSize = false, Size = form.FormElement.TitleBar.MinimizeButton.Size }; daynightButton.Click += DayNight_Click; form.FormElement.TitleBar.SystemButtons.Children.Insert(0, daynightButton); } private void DayNight_Click(object sender, EventArgs e) { themeDay = !themeDay; SetDayNight(); } private void SetDayNight() { if (themeDay) { ThemeResolutionService.ApplicationThemeName = "Fluent"; signInButton.Text = "?"; } else { ThemeResolutionService.ApplicationThemeName = "FluentDark"; signInButton.Text = "?"; } } }
如果您不喜歡用Windows默認主題啟動應用程序,可以刪除從Windows注冊表讀取設置的代碼:
themeDay = Telerik.WinControls.WindowsSettings.AppsUseLightTheme;
并將themeDay變量設置為白天為true,夜晚為false:
themeDay = true;
RadForm的標題欄中的按鈕是由CreateDayNightButton函數創建的:
private void CreateDayNightButton(RadForm form) { daynightButton = new RadImageButtonElement { ThemeRole = "TitleBarMinimizeButton", Text = "?", DisplayStyle = DisplayStyle.Text, ShowBorder = false, AutoSize = false, Size = form.FormElement.TitleBar.MinimizeButton.Size }; daynightButton.Click += DayNight_Click; form.FormElement.TitleBar.SystemButtons.Children.Insert(0, daynightButton); }
DayNight_Click在主題之間切換:
private void DayNight_Click(object sender, EventArgs e) { themeDay = !themeDay; SetDayNight(); }
SetDayNight()執行這些更改:
private void SetDayNight() { if (themeDay) { ThemeResolutionService.ApplicationThemeName = "Fluent"; signInButton.Text = "?"; } else { ThemeResolutionService.ApplicationThemeName = "FluentDark"; signInButton.Text = "?"; } }
這個命令改變了整個應用程序的主題:
ThemeResolutionService.ApplicationThemeName = "Fluent";
Telerik UI for WinForms和它的主題使實現WinForms應用程序的暗模式變得很容易,可以在自己的應用程序中使用這個DayNight類。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網