一.前言
申明:WPF自定義控件與樣式是一個(gè)系列文章,前后是有些關(guān)聯(lián)的,但大多是按照由簡(jiǎn)到繁的順序逐步發(fā)布的等,若有不明白的地方可以參考本系列前面的文章,文末附有部分文章鏈接。
本文主要內(nèi)容:
- 自定義Window窗體樣式;
- 基于自定義窗體實(shí)現(xiàn)自定義MessageBox消息提示框;
二.自定義Window窗體樣式
自定義的Window窗體效果:
?

因?yàn)閃PF默認(rèn)的窗體比較簡(jiǎn)陋,大都需要自己實(shí)現(xiàn)Window窗體樣式效果,基本思路很簡(jiǎn)單:
- 第一步:干掉默認(rèn)樣式:WindowStyle = WindowStyle.None;
- 第二步:設(shè)置窗體透明:AllowsTransparency = true;
- 第三步:設(shè)置自己的窗體樣式;
這樣從外觀樣式上可以滿足,但做為窗體該具備的基本功能還沒(méi)有,需要另外來(lái)實(shí)現(xiàn)了:
- 窗體Icon、標(biāo)題欄(可以通過(guò)樣式實(shí)現(xiàn));
- 窗體的基本按鈕:最小化、最大化、關(guān)閉按鈕;
- 窗體的鼠標(biāo)拖動(dòng);
- 好像Win8、Win10的功能吧,窗體拖動(dòng)到桌面邊緣自動(dòng)最大化、還原;
- 鼠標(biāo)調(diào)整窗口大??;
- 雙擊標(biāo)題欄最大化、還原;
上面的功能在本文中,一部分是自定義實(shí)現(xiàn)的,還有一部分是用了一個(gè)開(kāi)源庫(kù)(Microsoft.Windows.Shell)用于實(shí)現(xiàn)窗體大小、拖放等窗體基本功能,Microsoft.Windows.Shell文件下載:點(diǎn)我下載。 文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-452068.html
進(jìn)入正題,自定義窗體WindowBase的后臺(tái)C#代碼:??文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-452068.html
/// <summary>
/// WindowBase.xaml 的交互邏輯
/// </summary> public class WindowBase : Window { #region 默認(rèn)Header:窗體字體圖標(biāo)FIcon public static readonly DependencyProperty FIconProperty = DependencyProperty.Register("FIcon", typeof(string), typeof(WindowBase), new PropertyMetadata("\ue62e")); /// <summary> /// 按鈕字體圖標(biāo)編碼 /// </summary> public string FIcon { get { return (string)GetValue(FIconProperty); } set { SetValue(FIconProperty, value); } } #endregion #region 默認(rèn)Header:窗體字體圖標(biāo)大小 public static readonly DependencyProperty FIconSizeProperty = DependencyProperty.Register("FIconSize", typeof(double), typeof(WindowBase), new PropertyMetadata(20D)); /// <summary> /// 按鈕字體圖標(biāo)大小 /// </summary> public double FIconSize { get { return (double)GetValue(FIconSizeProperty); } set { SetValue(FIconSizeProperty, value); } } #endregion #region CaptionHeight 標(biāo)題欄高度 public static readonly DependencyProperty CaptionHeightProperty = DependencyProperty.Register("CaptionHeight", typeof(double), typeof(WindowBase), new PropertyMetadata(26D)); /// <summary> /// 標(biāo)題高度 /// </summary> public double CaptionHeight { get { return (double)GetValue(CaptionHeightProperty); } set { SetValue(CaptionHeightProperty, value); //this._WC.CaptionHeight = value; } } #endregion #region CaptionBackground 標(biāo)題欄背景色 public static readonly DependencyProperty CaptionBackgroundProperty = DependencyProperty.Register( "CaptionBackground", typeof(Brush), typeof(WindowBase), new PropertyMetadata(null)); public Brush CaptionBackground { get { return (Brush)GetValue(CaptionBackgroundProperty); } set { SetValue(CaptionBackgroundProperty, value); } } #endregion #region CaptionForeground 標(biāo)題欄前景景色 public static readonly DependencyProperty CaptionForegroundProperty = DependencyProperty.Register( "CaptionForeground", typeof(Brush), typeof(WindowBase), new PropertyMetadata(null)); public Brush CaptionForeground { get { return (Brush)GetValue(CaptionForegroundProperty); } set { SetValue(CaptionForegroundProperty, value); } } #endregion #region Header 標(biāo)題欄內(nèi)容模板,以提高默認(rèn)模板,可自定義 public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register( "Header", typeof(ControlTemplate), typeof(WindowBase), new PropertyMetadata(null)); public ControlTemplate Header { get { return (ControlTemplate)GetValue(HeaderProperty); } set { SetValue(HeaderProperty, value); } } #endregion #region MaxboxEnable 是否顯示最大化按鈕 public static readonly DependencyProperty MaxboxEnableProperty = DependencyProperty.Register( "MaxboxEnable", typeof(bool), typeof(WindowBase), new PropertyMetadata(true)); public bool MaxboxEnable { get { return (bool)GetValue(MaxboxEnableProperty); } set { SetValue(MaxboxEnableProperty, value); } } #endregion #region MinboxEnable 是否顯示最小化按鈕 public static readonly DependencyProperty MinboxEnableProperty = DependencyProperty.Register( "MinboxEnable", typeof(bool), typeof(WindowBase), new PropertyMetadata(true)); public bool MinboxEnable { get { return (bool)GetValue(MinboxEnableProperty); } set { SetValue(MinboxEnableProperty, value); } } #endregion public WindowBase() { this.WindowStyle = WindowStyle.None; this.AllowsTransparency = true; this.WindowStartupLocation = WindowStartupLocation.CenterScreen; this.Style = this.FindResource("DefaultWindowStyle") as Style; this.Icon = Images.CreateImageSourceFromImage(Properties.Resources.logo); //12=6+6//Margin=6,Border.Effect.BlueRadius=6 this.MaxHeight = SystemParameters.WorkArea.Height + 12 + 2; //bind command this.BindCommand(SystemCommands.CloseWindowCommand, this.CloseCommand_Execute); this.BindCommand(ApplicationCommands.Close,
到了這里,關(guān)于WPF自定義控件與樣式(13)-自定義窗體Window & 自適應(yīng)內(nèi)容大小消息框MessageBox的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!