Example #1
0
 private void ToolBarMinMaxClose_Loaded(object sender, RoutedEventArgs e)
 {
     if (MainWindow == null)
     {
         MainWindow = VisualHelper.GetParentObject <Window>(this);
         if (MainWindow != null)
         {
             MainWindow.StateChanged += (m, n) =>
             {
                 tbtnMax.IsChecked = MainWindow.WindowState == WindowState.Normal
                 ? true : false;
             }
         }
         ;
     }
 }
        /// <summary>
        /// 窗体构造函数
        /// </summary>
        /// <param name="owner">拥有弹出窗体的窗体</param>
        /// <param name="caption">标题</param>
        /// <param name="message">提示信息</param>
        /// <param name="isShowCancel">是否显示“取消”按钮</param>
        public MessageBoxInfoForm(ContentControl contentControl, string caption, string message, bool isShowCancel = false, LoadedType loadType = LoadedType.LoadedScale)
            : this()
        {
            InforLabel.Text = message;
            groupbox.Header = caption;
            Window owner = null;

            if (contentControl is Window)
            {
                owner = Application.Current.MainWindow;
            }
            if (contentControl is UserControl)
            {
                owner = VisualHelper.GetParentObject <Window>(contentControl);
            }
            this.Owner                 = owner;
            this.Width                 = owner.ActualWidth;
            this.Height                = owner.ActualHeight;
            this.ShowInTaskbar         = false;
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;

            // 转换owner的坐标为屏幕坐标
            var    location      = new Point(owner.Left, owner.Top);// owner.PointToScreen(new Point(0, 0));
            double captionHeight = SystemParameters.WindowCaptionHeight;

            // 有边框计算top,left
            if (owner.WindowStyle != WindowStyle.None)
            {
                ownerLeft = location.X - 8;
                ownerTop  = location.Y - captionHeight - 8;
            }
            // 无边框计算top/left,不允许透明
            if (owner.WindowStyle == WindowStyle.None && owner.AllowsTransparency == false)
            {
                ownerLeft = location.X - 8;
                ownerTop  = location.Y - 8;
            }
            // 无边框计算top/left,允许透明
            if (owner.WindowStyle == WindowStyle.None && owner.AllowsTransparency == true)
            {
                ownerLeft = location.X;
                ownerTop  = location.Y;
            }

            this.CancelButton.Visibility = isShowCancel ? Visibility.Visible : Visibility.Collapsed;

            this.Loaded += new RoutedEventHandler((m, n) =>
            {
                // 设置中间border的大小
                double width = (this.ActualWidth - border.ActualWidth) / 2;
                border.SetValue(Canvas.LeftProperty, width);
                double height = (this.ActualHeight - border.ActualHeight) / 2;
                border.SetValue(Canvas.TopProperty, height);
                // 设置窗体的Location
                this.Top  = ownerTop;
                this.Left = ownerLeft;

                if (loadType == LoadedType.LoadedStretch)
                {
                    Storyboard sbLoadedStretch = this.FindResource("sbLoadedStretch") as Storyboard;
                    if (sbLoadedStretch != null)
                    {
                        sbLoadedStretch.Completed -= new EventHandler(sbLoadedStretchComplated);
                        sbLoadedStretch.Completed += new EventHandler(sbLoadedStretchComplated);
                        sbLoadedStretch.Begin();
                    }
                }
                if (loadType == LoadedType.LoadedScale)
                {
                    Storyboard sbLoadedScale = this.FindResource("sbLoadedScale") as Storyboard;
                    if (sbLoadedScale != null)
                    {
                        sbLoadedScale.Completed -= new EventHandler(sbLoadedStretchComplated);
                        sbLoadedScale.Completed += new EventHandler(sbLoadedStretchComplated);
                        sbLoadedScale.Begin();
                    }
                }
            });
            this.Closed += MessageBoxInfoForm_Closed;
        }