精华内容
下载资源
问答
  • wpf自定义窗体resize

    2011-12-20 17:49:00
    捕获一般事件实现,很容易出BUG,捕获window消息比较安全。方法如下: 一、消息枚举 public enum HitTest : int { HTERROR = -2, HTTRANSPARENT = -1, HTNOWHERE = 0, HTCLIENT = 1, HTCAPTION = 2, HTSYSMENU = 3...

    捕获一般事件实现,很容易出BUG,捕获window消息比较安全。方法如下:

    一、消息枚举

    public enum HitTest : int
    {
    HTERROR = -2,
    HTTRANSPARENT = -1,
    HTNOWHERE = 0,
    HTCLIENT = 1,
    HTCAPTION = 2,
    HTSYSMENU = 3,
    HTGROWBOX = 4,
    HTSIZE = HTGROWBOX,
    HTMENU = 5,
    HTHSCROLL = 6,
    HTVSCROLL = 7,
    HTMINBUTTON = 8,
    HTMAXBUTTON = 9,
    HTLEFT = 10,
    HTRIGHT = 11,
    HTTOP = 12,
    HTTOPLEFT = 13,
    HTTOPRIGHT = 14,
    HTBOTTOM = 15,
    HTBOTTOMLEFT = 16,
    HTBOTTOMRIGHT = 17,
    HTBORDER = 18,
    HTREDUCE = HTMINBUTTON,
    HTZOOM = HTMAXBUTTON,
    HTSIZEFIRST = HTLEFT,
    HTSIZELAST = HTBOTTOMRIGHT,
    HTOBJECT = 19,
    HTCLOSE = 20,
    HTHELP = 21,
    }

     

    二、注册消息处理函数WndProc

    protected override void OnSourceInitialized(EventArgs e)
    {
      base.OnSourceInitialized(e);
      HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
      if (hwndSource != null)
      {
        hwndSource.AddHook(new HwndSourceHook(this.WndProc));
      }
    }

     

    三、消息处理函数WndProc定义

    private const int WM_NCHITTEST = 0x0084;
    private Point mousePoint = new Point(); //鼠标坐标

    private const int ResizeBorderAGWidth=12;//转角宽度 

    private const int ResizeBorderThickness=4;//边框宽度

    protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {

      switch (msg)
      {
        case WM_NCHITTEST:
          this.mousePoint.X = (lParam.ToInt32() & 0xFFFF);
          this.mousePoint.Y = (lParam.ToInt32() >> 16);

          // 窗口左上角
          if (this.mousePoint.Y - this.Top <= this.ResizeBorderAGWidth
          && this.mousePoint.X - this.Left <= this.ResizeBorderAGWidth)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTTOPLEFT);
          }
          // 窗口左下角  
          else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.ResizeBorderAGWidth
          && this.mousePoint.X - this.Left <= this.ResizeBorderAGWidth)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTBOTTOMLEFT);
          }
          // 窗口右上角
          else if (this.mousePoint.Y - this.Top <= this.ResizeBorderAGWidth
          && this.ActualWidth + this.Left - this.mousePoint.X <= this.ResizeBorderAGWidth)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTTOPRIGHT);
          }
          // 窗口右下角
          else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.ResizeBorderAGWidth
          && this.ActualHeight + this.Top - this.mousePoint.Y <= this.ResizeBorderAGWidth)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
          }
          // 窗口左侧
          else if (this.mousePoint.X - this.Left <= this.ResizeBorderThickness)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTLEFT);
          }
          // 窗口右侧
          else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.ResizeBorderThickness)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTRIGHT);
          }
          // 窗口上方
          else if (this.mousePoint.Y - this.Top <= this.ResizeBorderThickness)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTTOP);
          }
          // 窗口下方
          else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.ResizeBorderThickness)
          {
            handled = true;
            return new IntPtr((int)HitTest.HTBOTTOM);
          }
          else // 窗口移动
          {
            //handled = true;
            //return new IntPtr((int)HitTest.HTCAPTION);
            return IntPtr.Zero;
          }
      }

      return IntPtr.Zero;
    }

    转载于:https://www.cnblogs.com/luohu/archive/2011/12/20/2294989.html

    展开全文
  • WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox 原文:WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox一.前言  申明:WPF...
    原文:WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox

    一.前言

      申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接。

      本文主要内容:

    • 自定义Window窗体样式;
    • 基于自定义窗体实现自定义MessageBox消息提示框;

    二.自定义Window窗体样式

      自定义的Window窗体效果:

     

      因为WPF默认的窗体比较简陋,大都需要自己实现Window窗体样式效果,基本思路很简单:

    • 第一步:干掉默认样式:WindowStyle = WindowStyle.None;
    • 第二步:设置窗体透明:AllowsTransparency = true;
    • 第三步:设置自己的窗体样式;

      这样从外观样式上可以满足,但做为窗体该具备的基本功能还没有,需要另外来实现了:

    • 窗体Icon、标题栏(可以通过样式实现);
    • 窗体的基本按钮:最小化、最大化、关闭按钮;
    • 窗体的鼠标拖动;
    • 好像Win8、Win10的功能吧,窗体拖动到桌面边缘自动最大化、还原;
    • 鼠标调整窗口大小;
    • 双击标题栏最大化、还原;

      上面的功能在本文中,一部分是自定义实现的,还有一部分是用了一个开源库(Microsoft.Windows.Shell)用于实现窗体大小、拖放等窗体基本功能,Microsoft.Windows.Shell文件下载:点我下载

      进入正题,自定义窗体WindowBase的后台C#代码:  

        /// <summary>
        /// WindowBase.xaml 的交互逻辑
        /// </summary>
        public class WindowBase : Window
        {
            #region 默认Header:窗体字体图标FIcon
    
            public static readonly DependencyProperty FIconProperty =
                DependencyProperty.Register("FIcon", typeof(string), typeof(WindowBase), new PropertyMetadata("\ue62e"));
    
            /// <summary>
            /// 按钮字体图标编码
            /// </summary>
            public string FIcon
            {
                get { return (string)GetValue(FIconProperty); }
                set { SetValue(FIconProperty, value); }
            }
    
            #endregion
    
            #region  默认Header:窗体字体图标大小
    
            public static readonly DependencyProperty FIconSizeProperty =
                DependencyProperty.Register("FIconSize", typeof(double), typeof(WindowBase), new PropertyMetadata(20D));
    
            /// <summary>
            /// 按钮字体图标大小
            /// </summary>
            public double FIconSize
            {
                get { return (double)GetValue(FIconSizeProperty); }
                set { SetValue(FIconSizeProperty, value); }
            }
    
            #endregion
    
            #region CaptionHeight 标题栏高度
    
            public static readonly DependencyProperty CaptionHeightProperty =
                DependencyProperty.Register("CaptionHeight", typeof(double), typeof(WindowBase), new PropertyMetadata(26D));
    
            /// <summary>
            /// 标题高度
            /// </summary>
            public double CaptionHeight
            {
                get { return (double)GetValue(CaptionHeightProperty); }
                set
                {
                    SetValue(CaptionHeightProperty, value);
                    //this._WC.CaptionHeight = value;
                }
            }
    
            #endregion
    
            #region CaptionBackground 标题栏背景色
    
            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 标题栏前景景色
    
            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 标题栏内容模板,以提高默认模板,可自定义
    
            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, this.CloseCommand_Execute);
                this.BindCommand(SystemCommands.MaximizeWindowCommand, this.MaxCommand_Execute);
                this.BindCommand(SystemCommands.MinimizeWindowCommand, this.MinCommand_Execute);
            }
    
            private void CloseCommand_Execute(object sender, ExecutedRoutedEventArgs e)
            {
                SystemCommands.CloseWindow(this);
            }
    
            private void MaxCommand_Execute(object sender, ExecutedRoutedEventArgs e)
            {
                this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
                e.Handled = true;
            }
    
            private void MinCommand_Execute(object sender, ExecutedRoutedEventArgs e)
            {
                this.WindowState = WindowState.Minimized;
                e.Handled = true;
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                base.OnMouseLeftButtonDown(e);
                if (e.ButtonState == MouseButtonState.Pressed)
                {
                    this.DragMove();
                }
            }
        }
    View Code

      绑定命令的扩展方法:

            /// <summary>
            /// 绑定命令和命令事件到宿主UI
            /// </summary>
            public static void BindCommand(this UIElement @ui, ICommand com, Action<object, ExecutedRoutedEventArgs> call)
            {
                var bind = new CommandBinding(com);
                bind.Executed += new ExecutedRoutedEventHandler(call);
                @ui.CommandBindings.Add(bind);
            }

      WindowBase的样式有两个,一个是基础样式BaseWindowStyle,可以自定义头部标题栏,提供更多定制需求。另一个样式DefaultWindowStyle为默认窗体样式,用于大多数不要特别定制的窗体需求。BaseWindowStyle样式代码:  

        <!--WindowBase基本样式,可以自定义头部-->
        <Style x:Key="BaseWindowStyle" TargetType="{x:Type local:WindowBase}">
            <Setter Property="Background" Value="{StaticResource WindowBackground}"/>
            <Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
            <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
            <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
            <Setter Property="Width" Value="480"/>
            <Setter Property="Height" Value="320"/>
            <Setter Property="BorderBrush" Value="{StaticResource WindowBorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="FIconSize" Value="20"/>
            <Setter Property="CaptionHeight" Value="26"/>
            <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
            <!--标题栏背景色-->
            <Setter Property="CaptionBackground" Value="{StaticResource CaptionBackground}" />
            <Setter Property="CaptionForeground" Value="{StaticResource CaptionForeground}" />
            <Setter Property="FIcon" Value="&#xe62e;"/>
            <Setter Property="MaxboxEnable" Value="True"/>
            <Setter Property="MinboxEnable" Value="True"/>
            <!--建议内边框=3:ResizeBorderThickness = new Thickness(3);-->
            <Setter Property="Padding" Value="3"/>
            <Setter Property="local:ControlAttachProperty.CornerRadius" Value="0"/>
            <!--窗体基本设置 shell:WindowChrome-->
            <Setter Property="shell:WindowChrome.WindowChrome">
                <Setter.Value>
                    <shell:WindowChrome  CaptionHeight="{Binding Path=CaptionHeight,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:WindowBase}}}" 
                                         ResizeBorderThickness="8"/>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:WindowBase}">
                        <Grid Margin="6">
                            <!--背景,边框-->
                            <Border x:Name="Bg" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Effect="{StaticResource WindowDropShadow}"
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <Border  x:Name="BgInner" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{StaticResource WindowInnerBackground}" 
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <!--内容区域-->
                            <AdornerDecorator>
                                <ContentPresenter Margin="1" Content="{TemplateBinding Content}" />
                            </AdornerDecorator>
                            <!--窗体功能按钮:最小、最大、关闭-->
                            <Border VerticalAlignment="Top" Height="{TemplateBinding CaptionHeight}" Width="Auto" Margin="1" HorizontalAlignment="Right">
                                <StackPanel Orientation="Horizontal" VerticalAlignment="{TemplateBinding VerticalAlignment}" Height="30">
                                    <local:FButton x:Name="btnMin"  Width="26" VerticalAlignment="Center" Margin="1,2,1,2" 
                                                   Visibility="{Binding Path=MinboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                   Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60d;" FIconSize="16"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MinimizeWindowCommand}"/>
    
                                    <local:FButton x:Name="btnMax"  Width="26" VerticalAlignment="Center" Margin="1,2,1,2" 
                                                    Visibility="{Binding Path=MaxboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                    Style="{StaticResource FButton_Transparency}" FIcon="&#xe62b;" FIconSize="16"
                                                    shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MaximizeWindowCommand}"/>
    
                                    <local:FButton x:Name="btnClose"  Width="38" VerticalAlignment="Center"  Margin="1,2,3,2" CornerRadius="0"
                                                   MouseOverBackground="Red" MouseOverForeground="White" PressedBackground="#AA0D0D" PressedForeground="White"
                                                   AllowsAnimation="True" Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60a;" FIconSize="16"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" FIconMargin="0" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.CloseWindowCommand}"/>
                                </StackPanel>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="WindowState" Value="Maximized">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe62b;"></Setter>
                            </Trigger>
                            <Trigger Property="WindowState" Value="Normal">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe65b;"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

      DefaultWindowStyle样式代码:  

        <!--默认WindowBase的样式-->
        <Style x:Key="DefaultWindowStyle" TargetType="{x:Type local:WindowBase}">
            <Setter Property="Background" Value="{StaticResource WindowBackground}"/>
            <Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
            <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
            <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
            <Setter Property="Width" Value="480"/>
            <Setter Property="Height" Value="320"/>
            <Setter Property="BorderBrush" Value="{StaticResource WindowBorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="FIconSize" Value="20"/>
            <Setter Property="CaptionHeight" Value="26"/>
            <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
            <!--标题栏背景色-->
            <Setter Property="CaptionBackground" Value="{StaticResource CaptionBackground}" />
            <Setter Property="CaptionForeground" Value="{StaticResource CaptionForeground}" />
            <Setter Property="FIcon" Value="&#xe62e;"/>
            <Setter Property="MaxboxEnable" Value="True"/>
            <Setter Property="MinboxEnable" Value="True"/>
            <!--建议内边框=3:ResizeBorderThickness = new Thickness(3);-->
            <Setter Property="Padding" Value="3"/>
            <Setter Property="local:ControlAttachProperty.CornerRadius" Value="0"/>
            <!--窗体基本设置 shell:WindowChrome-->
            <Setter Property="shell:WindowChrome.WindowChrome">
                <Setter.Value>
                    <shell:WindowChrome  CaptionHeight="{Binding Path=CaptionHeight,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:WindowBase}}}" 
                                         ResizeBorderThickness="8"/>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:WindowBase}">
                        <Grid Margin="6">
                            <!--背景-->
                            <Border x:Name="Bg" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Effect="{StaticResource WindowDropShadow}"
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <Border  x:Name="BgInner" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{StaticResource WindowInnerBackground}" 
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <Grid Margin="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition MinHeight="18" Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <!--Caption标题部分-->
                                <Border Margin="0" Grid.Row="0" Background="{TemplateBinding CaptionBackground}">
                                    <Grid Margin="2,1,2,0">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <!--Header部分-->
                                        <StackPanel x:Name="PART_Header" Height="{TemplateBinding CaptionHeight}" Margin="5,1,5,1" Orientation="Horizontal" VerticalAlignment="Center">
                                            <TextBlock Style="{StaticResource FIcon}" Foreground="{TemplateBinding CaptionForeground}" FontSize="{TemplateBinding FIconSize}" Text="{TemplateBinding FIcon}"/>
                                            <TextBlock VerticalAlignment="Center" Margin="5" FontSize="{StaticResource HeaderFontSize}" Foreground="{TemplateBinding CaptionForeground}" Text="{TemplateBinding Title}"/>
                                        </StackPanel>
                                        <!--窗体功能按钮:最小、最大、关闭-->
                                        <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top" Margin="1" HorizontalAlignment="Right" Height="{TemplateBinding CaptionHeight}">
                                            <local:FButton x:Name="btnMin"  Width="26" VerticalAlignment="Center" Margin="1" 
                                                   Visibility="{Binding Path=MinboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                   Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60d;" FIconSize="14"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MinimizeWindowCommand}"/>
    
                                            <local:FButton x:Name="btnMax"  Width="26" VerticalAlignment="Center" Margin="1" 
                                                    Visibility="{Binding Path=MaxboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                    Style="{StaticResource FButton_Transparency}" FIcon="&#xe62b;" FIconSize="14"
                                                    shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MaximizeWindowCommand}"/>
    
                                            <local:FButton x:Name="btnClose"  Width="35" VerticalAlignment="Center" Margin="1" CornerRadius="0" Padding="1 2 1 2"
                                                   MouseOverBackground="Red" MouseOverForeground="White" PressedBackground="#AA0D0D" PressedForeground="White"
                                                   AllowsAnimation="True" Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60a;" FIconSize="14"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" FIconMargin="0" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.CloseWindowCommand}"/>
                                        </StackPanel>
                                    </Grid>
                                </Border>
                                <!--窗体内容区域-->
                                <AdornerDecorator Grid.Row="1" Margin="3,0,3,3">
                                    <ContentPresenter Content="{TemplateBinding Content}" />
                                </AdornerDecorator>
                            </Grid>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="WindowState" Value="Maximized">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe62b;"></Setter>
                            </Trigger>
                            <Trigger Property="WindowState" Value="Normal">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe65b;"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    View Code

      上面效果的背景色彩、边框的资源:  

        <!--Window窗体-->
        <SolidColorBrush x:Key="WindowBackground" Color="#093B5D"></SolidColorBrush>
        <SolidColorBrush x:Key="WindowInnerBackground" Color="Transparent"></SolidColorBrush>
        <!--<ImageBrush x:Key="WindowInnerBackground" Stretch="Fill"  
                    ImageSource="pack://application:,,,/XLY.Framework.WPFTest;component/Images/back/b2.jpg"  Opacity="1"
                    Viewport="0,0,1,1" ViewportUnits="Absolute" TileMode="Tile" AlignmentX="Left" AlignmentY="Top"/>-->
        
        <SolidColorBrush x:Key="WindowBorderBrush" Color="#920892"></SolidColorBrush>
        <DropShadowEffect x:Key="WindowDropShadow" Color="#F472F4" BlurRadius="8" ShadowDepth="0" Direction="0" Opacity="0.7" />
        <SolidColorBrush x:Key="CaptionForeground" Color="White"></SolidColorBrush>
        <!--<LinearGradientBrush x:Key="CaptionBackground" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#571457" Offset="0"/>
            <GradientStop Color="#6A196A" Offset="1"/>
        </LinearGradientBrush>-->
        <ImageBrush x:Key="CaptionBackground" 
                    ImageSource="pack://application:,,,/XLY.Framework.WPFTest;component/Images/back/b2.jpg"  Opacity="1"
                    Viewport="0,0,202,143" ViewportUnits="Absolute" TileMode="Tile" AlignmentX="Left" AlignmentY="Top"/>

     

    三.实现自定义MessageBox消息提示框

      基于第二节自定义的窗体WindowBase,实现自定义的MessageBox就简单了,效果图:

      仔细观察,不难发现上面的窗体大小是根据内容的多少自适应的。窗体自适应内容的的关键设置就是SizeToContent="WidthAndHeight",但为了达到更好的效果控制,还需要控制内容的大小范围,范围可以自己调整,看了样式代码你就了解了:  

    <local:WindowBase x:Class="System.Windows.MessageBoxX"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
            xmlns:local="clr-namespace:XLY.Framework.Controls" 
            MaxboxEnable="False" MinboxEnable="False" ResizeMode="NoResize" FIcon="&#xe608;" x:Name="mb" 
            Title="MessageBox" ShowInTaskbar="False" SizeToContent="WidthAndHeight"  Style="{StaticResource DefaultWindowStyle}" CaptionHeight="24"> 
        <local:WindowBase.InputBindings>
            <KeyBinding Key="Escape" Command="{x:Static shell:SystemCommands.CloseWindowCommand}"/>
        </local:WindowBase.InputBindings>
        <Grid>
            <Grid Margin="5,8,5,5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="60"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="ficon" HorizontalAlignment="Right" FontSize="80" Text="&#xe61c;" Foreground="{Binding Foreground,ElementName=mb}" Style="{StaticResource FIcon}" Margin="5"/>
                <TextBlock x:Name="txtMessage" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="{Binding Foreground,ElementName=mb}" 
                           FontSize="{Binding FontSize,ElementName=mb}" TextWrapping="Wrap" 
                       Margin="5,20,10,20" MinWidth="260" MaxWidth="420">新建一个WPF程序在Windows8下面就会出现左边的窗口边框,颜色取决于Windows主题我想在想创建一个右边那样的窗口,要么是窄边,要么没有边</TextBlock>
    
                <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="1" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center">
                    <local:FButton x:Name="btnOK" FIcon="&#xe646;" Width="85" Height="30" CornerRadius="0" Margin="5,5,20,5" Click="btnOK_Click">确定</local:FButton>
                    <local:FButton x:Name="btnCancel" FIcon="&#xe60a;" Width="85" Height="30" CornerRadius="0"
                               Margin="20,5,10,5" Click="btnCancel_Click">取消</local:FButton>
                </StackPanel>
            </Grid>
        </Grid>
    
    </local:WindowBase>

      上面不同消息类型的颜色资源:  

        <!--MessageBoxX-->
        <SolidColorBrush x:Key="InfoForeground" Color="White"></SolidColorBrush>
        <SolidColorBrush x:Key="QuestionForeground" Color="#74B80C"></SolidColorBrush>
        <SolidColorBrush x:Key="WarningForeground" Color="DarkOrange"></SolidColorBrush>
        <SolidColorBrush x:Key="ErrorForeground" Color="#E74E4E"></SolidColorBrush>

      后台C#代码  

        /// <summary>
        /// MessageBoxXxaml.xaml 的交互逻辑
        /// </summary>
        public partial class MessageBoxX : WindowBase
        {
            /// <summary>
            /// 结果,用户点击确定Result=true;
            /// </summary>
            public bool Result { get; private set; }
    
            private static readonly Dictionary<string, Brush> _Brushes = new Dictionary<string, Brush>();
    
            public MessageBoxX(EnumNotifyType type, string mes)
            {
                InitializeComponent();
                this.txtMessage.Text = mes;
                //type
                btnCancel.Visibility = Visibility.Collapsed;
                this.SetForeground(type);
                switch (type)
                {
                    case EnumNotifyType.Error:
                        this.ficon.Text = "\ue644";
                        break;
                    case EnumNotifyType.Warning:
                        this.ficon.Text = "\ue60b";
                        break;
                    case EnumNotifyType.Info:
                        this.ficon.Text = "\ue659";
                        break;
                    case EnumNotifyType.Question:
                        this.ficon.Text = "\ue60e";
                        this.btnCancel.Visibility = Visibility.Visible;
                        break;
                }
            }
    
            private void SetForeground(EnumNotifyType type)
            {
                string key = type.ToSafeString() + "Foreground";
                if (!_Brushes.ContainsKey(key))
                {
                    var b = this.TryFindResource(key) as Brush;
                    _Brushes.Add(key, b);
                }
                this.Foreground = _Brushes[key];
            }
    
            private void btnOK_Click(object sender, RoutedEventArgs e)
            {
                this.Result = true;
                this.Close();
                e.Handled = true;
            }
    
            private void btnCancel_Click(object sender, RoutedEventArgs e)
            {
                this.Result = false;
                this.Close();
                e.Handled = true;
            }
    
            /********************* public static method **************************/
    
            /// <summary>
            /// 提示错误消息
            /// </summary>
            public static void Error(string mes, Window owner = null)
            {
                Show(EnumNotifyType.Error, mes, owner);
            }
    
            /// <summary>
            /// 提示普通消息
            /// </summary>
            public static void Info(string mes, Window owner = null)
            {
                Show(EnumNotifyType.Info, mes, owner);
            }
    
            /// <summary>
            /// 提示警告消息
            /// </summary>
            public static void Warning(string mes, Window owner = null)
            {
                Show(EnumNotifyType.Warning, mes, owner);
            }
    
            /// <summary>
            /// 提示询问消息
            /// </summary>
            public static bool Question(string mes, Window owner = null)
            {
                return Show(EnumNotifyType.Question, mes, owner);
            }
    
            /// <summary>
            /// 显示提示消息框,
            /// owner指定所属父窗体,默认参数值为null,则指定主窗体为父窗体。
            /// </summary>
            private static bool Show(EnumNotifyType type, string mes, Window owner = null)
            {
                var res = true;
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    MessageBoxX nb = new MessageBoxX(type, mes) { Title = type.GetDescription() };
                    nb.Owner = owner ?? Application.Current.MainWindow;
                    nb.ShowDialog();
                    res = nb.Result;
                }));
                return res;
            }
    
            /// <summary>
            /// 通知消息类型
            /// </summary>
            public enum EnumNotifyType
            {
                [Description("错误")]
                Error,
                [Description("警告")]
                Warning,
                [Description("提示信息")]
                Info,
                [Description("询问信息")]
                Question,
            }
        }
    View Code

     

    附录:参考引用

    WPF自定义控件与样式(1)-矢量字体图标(iconfont)

    WPF自定义控件与样式(2)-自定义按钮FButton

    WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展

    WPF自定义控件与样式(4)-CheckBox/RadioButton自定义样式

    WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

    WPF自定义控件与样式(6)-ScrollViewer与ListBox自定义样式

    WPF自定义控件与样式(7)-列表控件DataGrid与ListView自定义样式

    WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox

    WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu

    WPF自定义控件与样式(10)-进度控件ProcessBar自定义样 

    WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现

    WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表

      

    版权所有,文章来源:http://www.cnblogs.com/anding

    个人能力有限,本文内容仅供学习、探讨,欢迎指正、交流。

    posted on 2015-12-02 09:55 NET未来之路 阅读(...) 评论(...) 编辑 收藏

    转载于:https://www.cnblogs.com/lonelyxmas/p/5012056.html

    展开全文
  • 一....基于自定义窗体实现自定义MessageBox消息提示框; 二.自定义Window窗体样式  自定义的Window窗体效果:  因为WPF默认的窗体比较简陋,大都需要自己实现Window窗体样式效果,基...

    一.前言

      申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等。

      本文主要内容:

    • 自定义Window窗体样式;
    • 基于自定义窗体实现自定义MessageBox消息提示框;

    二.自定义Window窗体样式

      自定义的Window窗体效果:

     

      因为WPF默认的窗体比较简陋,大都需要自己实现Window窗体样式效果,基本思路很简单:

    • 第一步:干掉默认样式:WindowStyle = WindowStyle.None;
    • 第二步:设置窗体透明:AllowsTransparency = true;
    • 第三步:设置自己的窗体样式;

      这样从外观样式上可以满足,但做为窗体该具备的基本功能还没有,需要另外来实现了:

    • 窗体Icon、标题栏(可以通过样式实现);
    • 窗体的基本按钮:最小化、最大化、关闭按钮;
    • 窗体的鼠标拖动;
    • 好像Win8、Win10的功能吧,窗体拖动到桌面边缘自动最大化、还原;
    • 鼠标调整窗口大小;
    • 双击标题栏最大化、还原;

      上面的功能在本文中,一部分是自定义实现的,还有一部分是用了一个开源库(Microsoft.Windows.Shell)用于实现窗体大小、拖放等窗体基本功能,Microsoft.Windows.Shell文件下载:点我下载

      进入正题,自定义窗体WindowBase的后台C#代码:  

    /// <summary>
        /// WindowBase.xaml 的交互逻辑
        /// </summary>
        public class WindowBase : Window
        {
            #region 默认Header:窗体字体图标FIcon
    
            public static readonly DependencyProperty FIconProperty =
                DependencyProperty.Register("FIcon", typeof(string), typeof(WindowBase), new PropertyMetadata("\ue62e"));
    
            /// <summary>
            /// 按钮字体图标编码
            /// </summary>
            public string FIcon
            {
                get { return (string)GetValue(FIconProperty); }
                set { SetValue(FIconProperty, value); }
            }
    
            #endregion
    
            #region  默认Header:窗体字体图标大小
    
            public static readonly DependencyProperty FIconSizeProperty =
                DependencyProperty.Register("FIconSize", typeof(double), typeof(WindowBase), new PropertyMetadata(20D));
    
            /// <summary>
            /// 按钮字体图标大小
            /// </summary>
            public double FIconSize
            {
                get { return (double)GetValue(FIconSizeProperty); }
                set { SetValue(FIconSizeProperty, value); }
            }
    
            #endregion
    
            #region CaptionHeight 标题栏高度
    
            public static readonly DependencyProperty CaptionHeightProperty =
                DependencyProperty.Register("CaptionHeight", typeof(double), typeof(WindowBase), new PropertyMetadata(26D));
    
            /// <summary>
            /// 标题高度
            /// </summary>
            public double CaptionHeight
            {
                get { return (double)GetValue(CaptionHeightProperty); }
                set
                {
                    SetValue(CaptionHeightProperty, value);
                    //this._WC.CaptionHeight = value;
                }
            }
    
            #endregion
    
            #region CaptionBackground 标题栏背景色
    
            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 标题栏前景景色
    
            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 标题栏内容模板,以提高默认模板,可自定义
    
            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, this.CloseCommand_Execute);
                this.BindCommand(SystemCommands.MaximizeWindowCommand, this.MaxCommand_Execute);
                this.BindCommand(SystemCommands.MinimizeWindowCommand, this.MinCommand_Execute);
            }
    
            private void CloseCommand_Execute(object sender, ExecutedRoutedEventArgs e)
            {
                SystemCommands.CloseWindow(this);
            }
    
            private void MaxCommand_Execute(object sender, ExecutedRoutedEventArgs e)
            {
                this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
                e.Handled = true;
            }
    
            private void MinCommand_Execute(object sender, ExecutedRoutedEventArgs e)
            {
                this.WindowState = WindowState.Minimized;
                e.Handled = true;
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                base.OnMouseLeftButtonDown(e);
                if (e.ButtonState == MouseButtonState.Pressed)
                {
                    this.DragMove();
                }
            }
        }

    绑定命令的扩展方法:

    /// <summary>
            /// 绑定命令和命令事件到宿主UI
            /// </summary>
            public static void BindCommand(this UIElement @ui, ICommand com, Action<object, ExecutedRoutedEventArgs> call)
            {
                var bind = new CommandBinding(com);
                bind.Executed += new ExecutedRoutedEventHandler(call);
                @ui.CommandBindings.Add(bind);
            }

    WindowBase的样式有两个,一个是基础样式BaseWindowStyle,可以自定义头部标题栏,提供更多定制需求。另一个样式DefaultWindowStyle为默认窗体样式,用于大多数不要特别定制的窗体需求。BaseWindowStyle样式代码:  

    <!--WindowBase基本样式,可以自定义头部-->
        <Style x:Key="BaseWindowStyle" TargetType="{x:Type local:WindowBase}">
            <Setter Property="Background" Value="{StaticResource WindowBackground}"/>
            <Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
            <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
            <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
            <Setter Property="Width" Value="480"/>
            <Setter Property="Height" Value="320"/>
            <Setter Property="BorderBrush" Value="{StaticResource WindowBorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="FIconSize" Value="20"/>
            <Setter Property="CaptionHeight" Value="26"/>
            <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
            <!--标题栏背景色-->
            <Setter Property="CaptionBackground" Value="{StaticResource CaptionBackground}" />
            <Setter Property="CaptionForeground" Value="{StaticResource CaptionForeground}" />
            <Setter Property="FIcon" Value="&#xe62e;"/>
            <Setter Property="MaxboxEnable" Value="True"/>
            <Setter Property="MinboxEnable" Value="True"/>
            <!--建议内边框=3:ResizeBorderThickness = new Thickness(3);-->
            <Setter Property="Padding" Value="3"/>
            <Setter Property="local:ControlAttachProperty.CornerRadius" Value="0"/>
            <!--窗体基本设置 shell:WindowChrome-->
            <Setter Property="shell:WindowChrome.WindowChrome">
                <Setter.Value>
                    <shell:WindowChrome  CaptionHeight="{Binding Path=CaptionHeight,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:WindowBase}}}" 
                                         ResizeBorderThickness="8"/>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:WindowBase}">
                        <Grid Margin="6">
                            <!--背景,边框-->
                            <Border x:Name="Bg" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Effect="{StaticResource WindowDropShadow}"
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <Border  x:Name="BgInner" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{StaticResource WindowInnerBackground}" 
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <!--内容区域-->
                            <AdornerDecorator>
                                <ContentPresenter Margin="1" Content="{TemplateBinding Content}" />
                            </AdornerDecorator>
                            <!--窗体功能按钮:最小、最大、关闭-->
                            <Border VerticalAlignment="Top" Height="{TemplateBinding CaptionHeight}" Width="Auto" Margin="1" HorizontalAlignment="Right">
                                <StackPanel Orientation="Horizontal" VerticalAlignment="{TemplateBinding VerticalAlignment}" Height="30">
                                    <local:FButton x:Name="btnMin"  Width="26" VerticalAlignment="Center" Margin="1,2,1,2" 
                                                   Visibility="{Binding Path=MinboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                   Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60d;" FIconSize="16"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MinimizeWindowCommand}"/>
    
                                    <local:FButton x:Name="btnMax"  Width="26" VerticalAlignment="Center" Margin="1,2,1,2" 
                                                    Visibility="{Binding Path=MaxboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                    Style="{StaticResource FButton_Transparency}" FIcon="&#xe62b;" FIconSize="16"
                                                    shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MaximizeWindowCommand}"/>
    
                                    <local:FButton x:Name="btnClose"  Width="38" VerticalAlignment="Center"  Margin="1,2,3,2" CornerRadius="0"
                                                   MouseOverBackground="Red" MouseOverForeground="White" PressedBackground="#AA0D0D" PressedForeground="White"
                                                   AllowsAnimation="True" Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60a;" FIconSize="16"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" FIconMargin="0" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.CloseWindowCommand}"/>
                                </StackPanel>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="WindowState" Value="Maximized">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe62b;"></Setter>
                            </Trigger>
                            <Trigger Property="WindowState" Value="Normal">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe65b;"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    DefaultWindowStyle样式代码:  

    <!--默认WindowBase的样式-->
        <Style x:Key="DefaultWindowStyle" TargetType="{x:Type local:WindowBase}">
            <Setter Property="Background" Value="{StaticResource WindowBackground}"/>
            <Setter Property="Foreground" Value="{StaticResource TextForeground}"/>
            <Setter Property="FontSize" Value="{StaticResource FontSize}"/>
            <Setter Property="FontFamily" Value="{StaticResource FontFamily}"/>
            <Setter Property="Width" Value="480"/>
            <Setter Property="Height" Value="320"/>
            <Setter Property="BorderBrush" Value="{StaticResource WindowBorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="FIconSize" Value="20"/>
            <Setter Property="CaptionHeight" Value="26"/>
            <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
            <!--标题栏背景色-->
            <Setter Property="CaptionBackground" Value="{StaticResource CaptionBackground}" />
            <Setter Property="CaptionForeground" Value="{StaticResource CaptionForeground}" />
            <Setter Property="FIcon" Value="&#xe62e;"/>
            <Setter Property="MaxboxEnable" Value="True"/>
            <Setter Property="MinboxEnable" Value="True"/>
            <!--建议内边框=3:ResizeBorderThickness = new Thickness(3);-->
            <Setter Property="Padding" Value="3"/>
            <Setter Property="local:ControlAttachProperty.CornerRadius" Value="0"/>
            <!--窗体基本设置 shell:WindowChrome-->
            <Setter Property="shell:WindowChrome.WindowChrome">
                <Setter.Value>
                    <shell:WindowChrome  CaptionHeight="{Binding Path=CaptionHeight,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type local:WindowBase}}}" 
                                         ResizeBorderThickness="8"/>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:WindowBase}">
                        <Grid Margin="6">
                            <!--背景-->
                            <Border x:Name="Bg" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Effect="{StaticResource WindowDropShadow}"
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <Border  x:Name="BgInner" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" 
                                Background="{StaticResource WindowInnerBackground}" 
                                BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  />
                            <Grid Margin="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition MinHeight="18" Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <!--Caption标题部分-->
                                <Border Margin="0" Grid.Row="0" Background="{TemplateBinding CaptionBackground}">
                                    <Grid Margin="2,1,2,0">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="Auto"/>
                                        </Grid.ColumnDefinitions>
                                        <!--Header部分-->
                                        <StackPanel x:Name="PART_Header" Height="{TemplateBinding CaptionHeight}" Margin="5,1,5,1" Orientation="Horizontal" VerticalAlignment="Center">
                                            <TextBlock Style="{StaticResource FIcon}" Foreground="{TemplateBinding CaptionForeground}" FontSize="{TemplateBinding FIconSize}" Text="{TemplateBinding FIcon}"/>
                                            <TextBlock VerticalAlignment="Center" Margin="5" FontSize="{StaticResource HeaderFontSize}" Foreground="{TemplateBinding CaptionForeground}" Text="{TemplateBinding Title}"/>
                                        </StackPanel>
                                        <!--窗体功能按钮:最小、最大、关闭-->
                                        <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top" Margin="1" HorizontalAlignment="Right" Height="{TemplateBinding CaptionHeight}">
                                            <local:FButton x:Name="btnMin"  Width="26" VerticalAlignment="Center" Margin="1" 
                                                   Visibility="{Binding Path=MinboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                   Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60d;" FIconSize="14"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MinimizeWindowCommand}"/>
    
                                            <local:FButton x:Name="btnMax"  Width="26" VerticalAlignment="Center" Margin="1" 
                                                    Visibility="{Binding Path=MaxboxEnable,RelativeSource={RelativeSource TemplatedParent},Converter={x:Static local:XConverter.BooleanToVisibilityConverter}}"
                                                    Style="{StaticResource FButton_Transparency}" FIcon="&#xe62b;" FIconSize="14"
                                                    shell:WindowChrome.IsHitTestVisibleInChrome="True" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.MaximizeWindowCommand}"/>
    
                                            <local:FButton x:Name="btnClose"  Width="35" VerticalAlignment="Center" Margin="1" CornerRadius="0" Padding="1 2 1 2"
                                                   MouseOverBackground="Red" MouseOverForeground="White" PressedBackground="#AA0D0D" PressedForeground="White"
                                                   AllowsAnimation="True" Style="{StaticResource FButton_Transparency}"  FIcon="&#xe60a;" FIconSize="14"
                                                   shell:WindowChrome.IsHitTestVisibleInChrome="True" FIconMargin="0" Foreground="{TemplateBinding CaptionForeground}"
                                                   Command="{x:Static shell:SystemCommands.CloseWindowCommand}"/>
                                        </StackPanel>
                                    </Grid>
                                </Border>
                                <!--窗体内容区域-->
                                <AdornerDecorator Grid.Row="1" Margin="3,0,3,3">
                                    <ContentPresenter Content="{TemplateBinding Content}" />
                                </AdornerDecorator>
                            </Grid>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="WindowState" Value="Maximized">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe62b;"></Setter>
                            </Trigger>
                            <Trigger Property="WindowState" Value="Normal">
                                <Setter Property="FIcon" TargetName="btnMax" Value="&#xe65b;"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    上面效果的背景色彩、边框的资源:  

    <!--Window窗体-->
        <SolidColorBrush x:Key="WindowBackground" Color="#093B5D"></SolidColorBrush>
        <SolidColorBrush x:Key="WindowInnerBackground" Color="Transparent"></SolidColorBrush>
        <!--<ImageBrush x:Key="WindowInnerBackground" Stretch="Fill"  
                    ImageSource="pack://application:,,,/XLY.Framework.WPFTest;component/Images/back/b2.jpg"  Opacity="1"
                    Viewport="0,0,1,1" ViewportUnits="Absolute" TileMode="Tile" AlignmentX="Left" AlignmentY="Top"/>-->
        
        <SolidColorBrush x:Key="WindowBorderBrush" Color="#920892"></SolidColorBrush>
        <DropShadowEffect x:Key="WindowDropShadow" Color="#F472F4" BlurRadius="8" ShadowDepth="0" Direction="0" Opacity="0.7" />
        <SolidColorBrush x:Key="CaptionForeground" Color="White"></SolidColorBrush>
        <!--<LinearGradientBrush x:Key="CaptionBackground" StartPoint="0.5,0" EndPoint="0.5,1">
            <GradientStop Color="#571457" Offset="0"/>
            <GradientStop Color="#6A196A" Offset="1"/>
        </LinearGradientBrush>-->
        <ImageBrush x:Key="CaptionBackground" 
                    ImageSource="pack://application:,,,/XLY.Framework.WPFTest;component/Images/back/b2.jpg"  Opacity="1"
                    Viewport="0,0,202,143" ViewportUnits="Absolute" TileMode="Tile" AlignmentX="Left" AlignmentY="Top"/>

    三.实现自定义MessageBox消息提示框

      基于第二节自定义的窗体WindowBase,实现自定义的MessageBox就简单了,效果图:

      仔细观察,不难发现上面的窗体大小是根据内容的多少自适应的。窗体自适应内容的的关键设置就是SizeToContent="WidthAndHeight",但为了达到更好的效果控制,还需要控制内容的大小范围,范围可以自己调整,看了样式代码你就了解了:  

    <local:WindowBase x:Class="System.Windows.MessageBoxX"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
            xmlns:local="clr-namespace:XLY.Framework.Controls" 
            MaxboxEnable="False" MinboxEnable="False" ResizeMode="NoResize" FIcon="&#xe608;" x:Name="mb" 
            Title="MessageBox" ShowInTaskbar="False" SizeToContent="WidthAndHeight"  Style="{StaticResource DefaultWindowStyle}" CaptionHeight="24"> 
        <local:WindowBase.InputBindings>
            <KeyBinding Key="Escape" Command="{x:Static shell:SystemCommands.CloseWindowCommand}"/>
        </local:WindowBase.InputBindings>
        <Grid>
            <Grid Margin="5,8,5,5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="60"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock x:Name="ficon" HorizontalAlignment="Right" FontSize="80" Text="&#xe61c;" Foreground="{Binding Foreground,ElementName=mb}" Style="{StaticResource FIcon}" Margin="5"/>
                <TextBlock x:Name="txtMessage" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="{Binding Foreground,ElementName=mb}" 
                           FontSize="{Binding FontSize,ElementName=mb}" TextWrapping="Wrap" 
                       Margin="5,20,10,20" MinWidth="260" MaxWidth="420">新建一个WPF程序在Windows8下面就会出现左边的窗口边框,颜色取决于Windows主题我想在想创建一个右边那样的窗口,要么是窄边,要么没有边</TextBlock>
    
                <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="1" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Center">
                    <local:FButton x:Name="btnOK" FIcon="&#xe646;" Width="85" Height="30" CornerRadius="0" Margin="5,5,20,5" Click="btnOK_Click">确定</local:FButton>
                    <local:FButton x:Name="btnCancel" FIcon="&#xe60a;" Width="85" Height="30" CornerRadius="0"
                               Margin="20,5,10,5" Click="btnCancel_Click">取消</local:FButton>
                </StackPanel>
            </Grid>
        </Grid>
    
    </local:WindowBase>

    上面不同消息类型的颜色资源:  

    <!--MessageBoxX-->
        <SolidColorBrush x:Key="InfoForeground" Color="White"></SolidColorBrush>
        <SolidColorBrush x:Key="QuestionForeground" Color="#74B80C"></SolidColorBrush>
        <SolidColorBrush x:Key="WarningForeground" Color="DarkOrange"></SolidColorBrush>
        <SolidColorBrush x:Key="ErrorForeground" Color="#E74E4E"></SolidColorBrush>

    后台C#代码  

    /// <summary>
        /// MessageBoxXxaml.xaml 的交互逻辑
        /// </summary>
        public partial class MessageBoxX : WindowBase
        {
            /// <summary>
            /// 结果,用户点击确定Result=true;
            /// </summary>
            public bool Result { get; private set; }
    
            private static readonly Dictionary<string, Brush> _Brushes = new Dictionary<string, Brush>();
    
            public MessageBoxX(EnumNotifyType type, string mes)
            {
                InitializeComponent();
                this.txtMessage.Text = mes;
                //type
                btnCancel.Visibility = Visibility.Collapsed;
                this.SetForeground(type);
                switch (type)
                {
                    case EnumNotifyType.Error:
                        this.ficon.Text = "\ue644";
                        break;
                    case EnumNotifyType.Warning:
                        this.ficon.Text = "\ue60b";
                        break;
                    case EnumNotifyType.Info:
                        this.ficon.Text = "\ue659";
                        break;
                    case EnumNotifyType.Question:
                        this.ficon.Text = "\ue60e";
                        this.btnCancel.Visibility = Visibility.Visible;
                        break;
                }
            }
    
            private void SetForeground(EnumNotifyType type)
            {
                string key = type.ToSafeString() + "Foreground";
                if (!_Brushes.ContainsKey(key))
                {
                    var b = this.TryFindResource(key) as Brush;
                    _Brushes.Add(key, b);
                }
                this.Foreground = _Brushes[key];
            }
    
            private void btnOK_Click(object sender, RoutedEventArgs e)
            {
                this.Result = true;
                this.Close();
                e.Handled = true;
            }
    
            private void btnCancel_Click(object sender, RoutedEventArgs e)
            {
                this.Result = false;
                this.Close();
                e.Handled = true;
            }
    
            /********************* public static method **************************/
    
            /// <summary>
            /// 提示错误消息
            /// </summary>
            public static void Error(string mes, Window owner = null)
            {
                Show(EnumNotifyType.Error, mes, owner);
            }
    
            /// <summary>
            /// 提示普通消息
            /// </summary>
            public static void Info(string mes, Window owner = null)
            {
                Show(EnumNotifyType.Info, mes, owner);
            }
    
            /// <summary>
            /// 提示警告消息
            /// </summary>
            public static void Warning(string mes, Window owner = null)
            {
                Show(EnumNotifyType.Warning, mes, owner);
            }
    
            /// <summary>
            /// 提示询问消息
            /// </summary>
            public static bool Question(string mes, Window owner = null)
            {
                return Show(EnumNotifyType.Question, mes, owner);
            }
    
            /// <summary>
            /// 显示提示消息框,
            /// owner指定所属父窗体,默认参数值为null,则指定主窗体为父窗体。
            /// </summary>
            private static bool Show(EnumNotifyType type, string mes, Window owner = null)
            {
                var res = true;
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    MessageBoxX nb = new MessageBoxX(type, mes) { Title = type.GetDescription() };
                    nb.Owner = owner ?? Application.Current.MainWindow;
                    nb.ShowDialog();
                    res = nb.Result;
                }));
                return res;
            }
    
            /// <summary>
            /// 通知消息类型
            /// </summary>
            public enum EnumNotifyType
            {
                [Description("错误")]
                Error,
                [Description("警告")]
                Warning,
                [Description("提示信息")]
                Info,
                [Description("询问信息")]
                Question,
            }
        }

     

    原文地址:https://www.cnblogs.com/anding/p/5011981.html

    转载于:https://www.cnblogs.com/mqxs/p/10142514.html

    展开全文
  • WPF 自定义消息

    2017-02-10 11:29:00
    说明:做一个简单的消息框,能够根据自定义显示图片和按钮。 步骤: 1.其实消息框就是一个Window,首先设置窗体的显示位置,显示大小,要让它看着有消息框的样子。这个很容易,代码如下: Height="206" Width=...

    说明:做一个简单的消息框,能够根据自定义显示图片和按钮。

    步骤:

    1.其实消息框就是一个Window,首先设置窗体的显示位置,显示大小,要让它看着有消息框的样子。这个很容易,代码如下:

    Height="206" Width="420" WindowStartupLocation="CenterScreen" BorderThickness="1,0,1,1" ShowIcon="False" Closed="DXRibbonWindow_Closed" ResizeMode="NoResize"

    2.然后我们要画出消息框,就是一个Grid,分配好空间显示图片和弹框提示,底部显示按钮,这里要注意按钮的显示是自定义选择的,所以位置设置为相对位置。代码如下:

     <Grid x:Name="grid">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="50"/>
            </Grid.RowDefinitions>
            <!--显示图片和文本-->
            <StackPanel Grid.Row="0" VerticalAlignment="Center" Orientation="Horizontal">
                <Image Source="{Binding ImagePath}" Width="62" Height="62" Margin="40,20,20,20"/>
                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" TextWrapping="WrapWithOverflow" Width="280" TextAlignment="Left"
                           Text="{Binding MessageBoxText}" FontSize="12"/>
            </StackPanel>
            <!--Button Margin(坐上右下)-->
            <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Content="确 定" x:Name="OkButton" Width="80" Height="25" Click="OkButton_Click" Margin="10,0,15,0" IsDefault="True"
                        Visibility="{Binding OkButtonVisibility,Mode=OneWay}"/>
                <Button Content="" x:Name="YesButton" Width="80" Height="25" Click="YesButton_Click" Margin="10,0,15,0" 
                        Visibility="{Binding YesButtonVisibility,Mode=OneWay}"/>
                <Button Content="" x:Name="NoButton" Width="80" Height="25" Click="NoButton_Click" Margin="10,0,15,0"
                        Visibility="{Binding NoButtonVisibility,Mode=OneWay}"/>
                <Button Content="取消" x:Name="CancelButton" Width="80" Height="25" Click="CancelButton_Click" Margin="10,0,15,0"
                        Visibility="{Binding CancelButtonVisibility}"/>
            </StackPanel>
        </Grid>

    3.定义枚举值,分别为按钮的返回值,消息框的返回值以及图标类型。代码如下:

      /// <summary>
        /// 显示按钮类型
        /// </summary>
        public enum CustomMessageBoxButton
        {
            OK=0,
            OKCancel=1,
            YesNo=2,
            YesNoCancel=3
        }
        /// <summary>
        /// 消息框的返回值
        /// </summary>
        public enum CustomMessageBoxResult
        {
            //用户直接关闭了消息窗口
            None = 0,
            //用户点击确定按钮
            OK = 1,
            //用户点击取消按钮
            Cancel = 2,
            //用户点击是按钮
            Yes = 3,
            //用户点击否按钮
            No = 4
        }
        /// <summary>
        /// 图标类型
        /// </summary>
        public enum CustomMessageBoxIcon
        {
            None = 0,
            Error = 1,
            Question = 2,
            Warning = 3
        }

    4.定义属性,并且在后置代码的构造函数中将按钮全部显示为不可见。代码如下:

        #region Filed
            /// <summary>
            /// 显示的内容
            /// </summary>
            public string MessageBoxText { get; set; }
            /// <summary>
            /// 显示的图片
            /// </summary>
            public string ImagePath { get; set; }
            /// <summary>
            /// 控制显示 OK 按钮
            /// </summary>
            public Visibility OkButtonVisibility { get; set; }
            /// <summary>
            /// 控制显示 Cacncel 按钮
            /// </summary>
            public Visibility CancelButtonVisibility { get; set; }
            /// <summary>
            /// 控制显示 Yes 按钮
            /// </summary>
            public Visibility YesButtonVisibility { get; set; }
            /// <summary>
            /// 控制显示 No 按钮
            /// </summary>
            public Visibility NoButtonVisibility { get; set; }
            /// <summary>
            /// 消息框的返回值
            /// </summary>
            public CustomMessageBoxResult Result { get; set; }
    
            #endregion
      public CustomMessageBoxWindow()
            {            
                InitializeComponent();
                this.DataContext = this;
    
                OkButtonVisibility = Visibility.Collapsed;
                CancelButtonVisibility = Visibility.Collapsed;
                YesButtonVisibility = Visibility.Collapsed;
                NoButtonVisibility = Visibility.Collapsed;
    
                Result = CustomMessageBoxResult.None;
            }

    5.在按钮的事件中为按下按钮返回值赋值并且关闭窗口:

      private void OkButton_Click(object sender, RoutedEventArgs e)
            {
                Result = CustomMessageBoxResult.OK;
                this.Close();
            }
    
            private void YesButton_Click(object sender, RoutedEventArgs e)
            {
                Result = CustomMessageBoxResult.Yes;
                this.Close();
            }
    
            private void NoButton_Click(object sender, RoutedEventArgs e)
            {
                Result = CustomMessageBoxResult.No;
                this.Close();
            }
    
            private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                Result = CustomMessageBoxResult.Cancel;
                this.Close();
            }

    6.定义Show方法,根据用户自定义弹出消息框显示:

       public static CustomMessageBoxResult Show(string messageBoxText, CustomMessageBoxButton messageBoxButton, CustomMessageBoxIcon messageBoxImage)
            {
                CustomMessageBoxWindow window = new CustomMessageBoxWindow();
                window.Owner = Application.Current.MainWindow;
                window.Topmost = true;
                window.MessageBoxText = messageBoxText;
                switch (messageBoxImage)
                {
                    case CustomMessageBoxIcon.Question:
                        window.ImagePath = @"/Images/question.png";
                        break;
                    case CustomMessageBoxIcon.Error:
                    case CustomMessageBoxIcon.Warning:
                        window.ImagePath = @"/Images/alert.png";
                        break;
                }
                switch (messageBoxButton)
                {
                    case CustomMessageBoxButton.OK:
                        window.OkButtonVisibility = Visibility.Visible;
                        break;
                    case CustomMessageBoxButton.OKCancel:
                        window.OkButtonVisibility = Visibility.Visible;
                        window.CancelButtonVisibility = Visibility.Visible;
                        break;
                    case CustomMessageBoxButton.YesNo:
                        window.YesButtonVisibility = Visibility.Visible;
                        window.NoButtonVisibility = Visibility.Visible;
                        break;
                    case CustomMessageBoxButton.YesNoCancel:
                        window.YesButtonVisibility = Visibility.Visible;
                        window.NoButtonVisibility = Visibility.Visible;
                        window.CancelButtonVisibility = Visibility.Visible;
                        break;
                    default:
                        window.OkButtonVisibility = Visibility.Visible;
                        break;
                }
    
                window.ShowDialog();
                return window.Result;
            }

    7.效果如下:

    8.消息框本身就很简单,但是网上的好多例子说的有些复杂了,所以自己做了一个。消息框的显示方法我只写了一个,如果有更多需求可以重载方法就好。另外,我的VS上装了DevExpress,我用的是Dev的Ribbon Window,没装DEV的可以用普通Window,装了Dev的还是要删除引用添加自己的引用,最后源码中附上了改变窗体显示主题的代码。源码在最后上传给大家参考。

     源码下载:http://files.cnblogs.com/files/damon-xu/Demo1.rar

    转载于:https://www.cnblogs.com/damon-xu/p/6385554.html

    展开全文
  • wpf自定义控件库,包含列表,按钮,文本框,滚动条,进度条,消息窗体,气泡组件等多种自定义控件样式。
  • WPF自定义"消息框式"的窗体 原文 http://www.cnblogs.com/ListenFly/archive/2013/02/19/2917744.html <Window x:Class="mumu_nonRectangularwindow.MainWindow" xmlns="http://schema...
  • 窗体传值 先定义一个变量 那边传至即可 传值得 写好 就可以了 最后效果
  • 一个自定义窗体: 或者一些窗体上的Popup控件 不管是不是这个业务场景,都可以在不相干的后台中去获取控件并关闭。 /// <summary> /// Windows钩子 /// </summary> private IntPtr WndProc...
  • 1.重载了几个新的Show方法,和官方的MessageBox相差无几了。...3.窗体高度可以随着内容的多少随之变化(如果超过了屏幕的高度那还是没办法) 4.消息框的图标的支持 5.消息框弹出音的支持 6.增加了几个开关:
  • 自定义窗体Window实现以下步骤: 在C#代码中弹出窗体时,使用 window.Show() 而不是 window.ShowDialog(); 最好设置 window.Topmost = true; 可以在XAML顶部写、也可以在C#代码中设置。否则该窗体可以被主界面...
  • wpf实现仿qq消息提示框

    千次阅读 2016-01-15 13:34:46
    在主窗体中调用,先处理一下消息框的展示问题, AllowsTransparency="True" WindowStyle="None" WindowStartupLocation="Manual",实现允许透明,隐藏自带的工具条(关闭、最大/最小化),自定义窗口位置。...
  • WPF之模板样式

    2012-11-26 21:57:00
    以前学WPF时弄的个自定义窗体,关于截断系统消息的一部分代码来源于网上。 源文件在这里。 说起来也容易,整个窗体就是个border,渲染下,然后在上面加层DockPanel,放那些标题和按钮。 下面是自定义窗体的...
  • 消息的发送是通过一个自定义消息发送和订阅类来完成的(程序中将定义为:LoginSentEvent)。  2.Shell通过VM接受LoginSentEven传递的loginState来接收登陆成功与否的消息,如果登陆成功,则使用RegionManager...
  • ArcGIS Pro的控件样式

    2018-10-18 21:52:02
     2、Pro自定义的控件风格 2.1 预览 2.2 视窗 2.3 按钮样式 2.4 单选框 2.5 表格 2.6 Expander 2.7 Gallery 2.8 HyperLink 2.9 TextBlock 3、Pro封装的窗体 3.1 消息框 3.2 通知框 3.3 数据加载窗 3.4...
  • 5.17.5 自定义逻辑验证控件(CustomValidator) 5.17.6 验证组控件(ValidationSummary) 5.18 导航控件 5.19 其他控件 5.19.1 隐藏输入框控件(HiddenField) 5.19.2 图片热点控件(ImageMap) 5.19.3 静态标签控件...
  • ASP.NET 3.5 开发大全

    2012-02-12 17:36:55
    5.17.5 自定义逻辑验证控件(CustomValidator) 5.17.6 验证组控件(ValidationSummary) 5.18 导航控件 5.19 其他控件 5.19.1 隐藏输入框控件(HiddenField) 5.19.2 图片热点控件(ImageMap) 5.19.3 静态标签控件...
  • ASP.NET 3.5 开发大全1-5

    2012-02-12 17:03:33
    5.17.5 自定义逻辑验证控件(CustomValidator) 5.17.6 验证组控件(ValidationSummary) 5.18 导航控件 5.19 其他控件 5.19.1 隐藏输入框控件(HiddenField) 5.19.2 图片热点控件(ImageMap) 5.19.3 静态标签控件...
  • ASP.NET3.5开发大全

    2011-09-07 22:38:28
    5.17.5 自定义逻辑验证控件(CustomValidator) 5.17.6 验证组控件(ValidationSummary) 5.18 导航控件 5.19 其他控件 5.19.1 隐藏输入框控件(HiddenField) 5.19.2 图片热点控件(ImageMap) 5.19.3 静态标签控件...
  • 5.17.5 自定义逻辑验证控件(CustomValidator) 5.17.6 验证组控件(ValidationSummary) 5.18 导航控件 5.19 其他控件 5.19.1 隐藏输入框控件(HiddenField) 5.19.2 图片热点控件(ImageMap) 5.19.3 静态标签控件...
  • ASP.NET3.5 开发大全

    2010-07-27 10:02:46
     5.17.5 自定义逻辑验证控件(CustomValidator)  5.17.6 验证组控件(ValidationSummary)  5.18 导航控件  5.19 其他控件  5.19.1 隐藏输入框控件(HiddenField)  5.19.2 图片热点控件(ImageMap)  5.19....
  •  5.17.5 自定义逻辑验证控件(CustomValidator)  5.17.6 验证组控件(ValidationSummary)  5.18 导航控件  5.19 其他控件  5.19.1 隐藏输入框控件(HiddenField)  5.19.2 图片热点控件(ImageMap)  5.19....

空空如也

空空如也

1 2 3
收藏数 56
精华内容 22
关键字:

wpf自定义窗体消息