This commit is contained in:
luxiaoqi 2023-11-28 17:31:16 +08:00
parent b79e63da71
commit 507e844275
3 changed files with 167 additions and 60 deletions

View File

@ -25,32 +25,13 @@ namespace CPF.Toolkit.Demo
this.Background = null;
var frame = this.Children.Add(new WindowFrame(this, new MdiHost
{
//Children =
//{
// new MdiWindow
// {
// Title = "test",
// Content = new WrapPanel
// {
// Children =
// {
// new Button{ Content = "test" ,Width = 100, Height = 35 },
// new Button{ Content = "test" ,Width = 100, Height = 35 },
// new Button{ Content = "test" ,Width = 100, Height = 35 },
// new Button{ Content = "test" ,Width = 100, Height = 35 },
// },
// },
// }
//}
}
.LoopCreate(5,(i) => new MdiWindow
{
Title = $"test{i}",
Content = new WrapPanel
{
}
.LoopCreate(5,k => new Button { Content = $"test{k}",Width = 100, Height = 35 }),
})));
Children =
{
new MdiWindow{ Content = new Grid{ }, Title = "test1"},
//new MdiWindow{ Content = new Grid{ }, Title = "test2"},
//new MdiWindow{ Content = new Grid{ }, Title = "test3"},
},
}));
frame.CaptionBackgrund = "white";
frame.CaptionForeground = "black";
frame.ControlBoxStroke = "black";

View File

@ -1,4 +1,6 @@
using CPF.Controls;
using CPF.Drawing;
using CPF.Platform;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@ -7,46 +9,131 @@ using System.Text;
namespace CPF.Toolkit.Controls
{
public class MdiHost : Panel
public class MdiHost : Grid
{
public MdiHost()
{
Size = SizeField.Fill;
Background = "204,204,204";
this.TaskBarList = new Collection<UIElement>();
this.Size = SizeField.Fill;
this.Background = "204,204,204";
base.RowDefinitions.Add(new RowDefinition { Height = GridLength.Star });
base.RowDefinitions.Add(new RowDefinition { Height = "35", MaxHeight = 35 });
base.Children.Add(this.host);
var taskBar = base.Children.Add(new ListBox
{
Size = SizeField.Fill,
Background = "white",
BorderFill = new SolidColorFill { Color = Color.Silver },
BorderThickness = new Thickness(0, 1, 0, 0),
BorderType = BorderType.BorderThickness,
ItemsPanel = new StackPanel { Orientation = Orientation.Horizontal },
ItemTemplate = new ListBoxItem
{
Height = "100%",
Width = 100,
MarginRight = 1,
FontSize = 16f,
BorderFill = "Silver",
BorderThickness = new Thickness(0, 0, 1, 0),
BorderType = BorderType.BorderThickness,
[nameof(ListBoxItem.Content)] = new BindingDescribe("Title")
},
Items = this.TaskBarList,
[nameof(ListBox.SelectedValue)] = new BindingDescribe(this, nameof(this.SelectWindow), BindingMode.TwoWay),
}, row: 1);
this.host.PropertyChanged += Host_PropertyChanged;
this.host.UIElementAdded += Host_UIElementAdded;
this.host.UIElementRemoved += Host_UIElementRemoved;
}
readonly Panel host = new Panel { Size = SizeField.Fill };
Collection<UIElement> TaskBarList { get => GetValue<Collection<UIElement>>(); set => SetValue(value); }
public new UIElementCollection Children => host.Children;
UIElement SelectWindow { get => GetValue<UIElement>(); set => SetValue(value); }
protected override void OnPropertyChanged(string propertyName, object oldValue, object newValue, PropertyMetadataAttribute propertyMetadata)
{
if (propertyName == nameof(ActualSize))
if (propertyName == nameof(this.SelectWindow))
{
foreach (MdiWindow item in this.Children)
{
if (item.WindowState == WindowState.Maximized) continue;
if (item.MarginLeft.Value + item.ActualSize.Width > this.ActualSize.Width)
{
item.MarginLeft = this.ActualSize.Width - item.ActualSize.Width;
}
if (item.MarginTop.Value + item.ActualSize.Height > this.ActualSize.Height)
{
item.MarginTop = this.ActualSize.Height - item.ActualSize.Height;
}
}
this.Topping(this.SelectWindow);
}
base.OnPropertyChanged(propertyName, oldValue, newValue, propertyMetadata);
}
protected override void OnUIElementAdded(UIElementAddedEventArgs e)
private void Host_PropertyChanged(object sender, CPFPropertyChangedEventArgs e)
{
e.Element.PreviewMouseDown += Element_PreviewMouseDown; ;
base.OnUIElementAdded(e);
if (e.PropertyName.Or(nameof(ActualSize), nameof(Size), nameof(Width), nameof(Height)))
{
foreach (MdiWindow mdi in this.host.Children)
{
if (mdi.WindowState == WindowState.Maximized) continue;
if (mdi.MarginLeft.Value + mdi.ActualSize.Width > this.ActualSize.Width)
{
mdi.MarginLeft = this.ActualSize.Width - mdi.ActualSize.Width;
}
if (mdi.MarginTop.Value + mdi.ActualSize.Height > this.ActualSize.Height)
{
mdi.MarginTop = this.ActualSize.Height - mdi.ActualSize.Height;
}
}
}
}
private void Host_UIElementRemoved(object sender, UIElementRemovedEventArgs e)
{
e.Element.PropertyChanged -= Element_PropertyChanged;
e.Element.PreviewMouseDown -= Element_PreviewMouseDown;
this.TaskBarList.Remove(e.Element);
}
private void Host_UIElementAdded(object sender, UIElementAddedEventArgs e)
{
var view = e.Element as MdiWindow;
view.PropertyChanged += Element_PropertyChanged;
view.PreviewMouseDown += Element_PreviewMouseDown;
this.TaskBarList.Add(view);
e.Element.ZIndex = this.host.Children.Max(x => x.ZIndex) + 1;
this.Topping(e.Element);
}
private void Element_PreviewMouseDown(object sender, Input.MouseButtonEventArgs e)
{
var view = sender as UIElement;
view.ZIndex = this.Children.Max(x => x.ZIndex) + 1;
var ele = (UIElement)sender;
this.Topping(ele);
}
private void Element_PropertyChanged(object sender, CPFPropertyChangedEventArgs e)
{
var view = sender as MdiWindow;
switch (e.PropertyName)
{
case nameof(MdiWindow.WindowState):
if ((WindowState)e.NewValue == WindowState.Minimized)
{
this.SelectWindow = this.host.Children.FindLast(x => x.Visibility == Visibility.Visible);
}
break;
case nameof(ZIndex):
this.SelectWindow = view;
this.SelectWindow.Visibility = Visibility.Visible;
break;
}
}
void Topping(UIElement ele)
{
if (ele == null) return;
var index = this.host.Children.Max(x => x.ZIndex);
if (ele.ZIndex == index)
{
ele.Visibility = Visibility.Visible;
return;
}
ele.ZIndex = index + 1;
}
}
}

View File

@ -19,8 +19,9 @@ namespace CPF.Toolkit.Controls
{
public class MdiWindow : Control
{
[PropertyMetadata(typeof(WindowState), "Normal")]
[PropertyMetadata(typeof(WindowState), "0")]
public WindowState WindowState { get => GetValue<WindowState>(); set => SetValue(value); }
[PropertyMetadata("title")]
public string Title { get => GetValue<string>(); set => SetValue(value); }
public UIElement Content { get => GetValue<UIElement>(); set => SetValue(value); }
[PropertyMetadata(true)]
@ -32,6 +33,9 @@ namespace CPF.Toolkit.Controls
[UIPropertyMetadata((byte)5, UIPropertyOptions.AffectsMeasure)]
public byte ShadowBlur { get { return GetValue<byte>(); } set { SetValue(value); } }
public event EventHandler<ClosingEventArgs> Closing;
WindowState oldState;
SizeField normalSize = new SizeField(500, 500);
Point normalPos = new Point(0, 0);
protected override void InitializeComponent()
@ -208,7 +212,11 @@ namespace CPF.Toolkit.Controls
StrokeStyle = "2",
},
Commands = { { nameof(Button.Click),(s,e) => this.WindowState = WindowState.Maximized } },
[nameof(Visibility)] = new BindingDescribe(this,nameof(WindowState),BindingMode.OneWay,a => ((WindowState)a).Or(WindowState.Maximized,WindowState.FullScreen) ? Visibility.Collapsed : Visibility.Visible),
[nameof(Visibility)] = new BindingDescribe(this,
nameof(WindowState),
BindingMode.OneWay,
a => ((WindowState)a).Or(WindowState.Maximized,WindowState.FullScreen)
? Visibility.Collapsed : Visibility.Visible),
},
new SystemButton
{
@ -244,7 +252,12 @@ namespace CPF.Toolkit.Controls
}
},
Commands = { { nameof(Button.Click),(s,e) => this.WindowState = WindowState.Normal } },
[nameof(Visibility)] = new BindingDescribe(this,nameof(WindowState),BindingMode.OneWay,a => ((WindowState)a).Or( WindowState.Normal, WindowState.Minimized) ? Visibility.Collapsed : Visibility.Visible)
[nameof(Visibility)] = new BindingDescribe(this,
nameof(WindowState),
BindingMode.OneWay,
a => ((WindowState)a).Or( WindowState.Normal, WindowState.Minimized)
? Visibility.Collapsed :
Visibility.Visible)
}
}
},
@ -277,6 +290,21 @@ namespace CPF.Toolkit.Controls
}
}
},
Commands =
{
{
nameof(Button.Click),(ss,ee) =>
{
var e = new ClosingEventArgs();
this.Closing?.Invoke(this,e);
if (!e.Cancel)
{
this.Visibility = Visibility.Collapsed;
this.Dispose();
}
}
}
},
[nameof(Visibility)] = new BindingDescribe(this,nameof(this.CloseBox),BindingMode.OneWay,a=>(bool)a?Visibility.Visible: Visibility.Collapsed)
}
}
@ -289,7 +317,7 @@ namespace CPF.Toolkit.Controls
nameof(Thumb.DragDelta),
(s,e) =>
{
if (this.WindowState.Or(WindowState.Normal, WindowState.Minimized))
if (this.WindowState.Or(WindowState.Normal))
{
var arge = e as DragDeltaEventArgs;
this.MarginLeft += arge.HorizontalChange;
@ -323,8 +351,10 @@ namespace CPF.Toolkit.Controls
}
},
},
[nameof(Border.ShadowBlur)] = new BindingDescribe(this, nameof(WindowState), BindingMode.OneWay, a => ((WindowState)a).Or(WindowState.Maximized, WindowState.FullScreen) ? 0 : ShadowBlur),
[nameof(Border.ShadowBlur)] = new BindingDescribe(this, nameof(ShadowBlur), BindingMode.OneWay, a => ((WindowState)a).Or(WindowState.Maximized, WindowState.FullScreen) ? 0 : (byte)a),
[nameof(Border.ShadowBlur)] = new BindingDescribe(this,
nameof(WindowState),
BindingMode.OneWay,
a => ((WindowState)a).Or(WindowState.Maximized, WindowState.FullScreen,WindowState.Minimized) ? 0 : ShadowBlur),
});
this.Content.Margin = "0";
@ -345,8 +375,8 @@ namespace CPF.Toolkit.Controls
this.MarginTop = this.normalPos.Y;
break;
case WindowState.Minimized:
this.Width = this.MinWidth;
this.Height = this.MinHeight;
this.Visibility = Visibility.Collapsed;
this.oldState = (WindowState)oldValue;
break;
case WindowState.Maximized:
case WindowState.FullScreen:
@ -368,11 +398,10 @@ namespace CPF.Toolkit.Controls
this.normalSize = this.Size;
break;
case WindowState.Minimized:
this.Width = this.MinWidth;
this.Height = this.MinHeight;
//this.Width = this.MinWidth;
//this.Height = this.MinHeight;
break;
case WindowState.Maximized:
break;
case WindowState.FullScreen:
break;
}
@ -396,5 +425,15 @@ namespace CPF.Toolkit.Controls
base.OnPropertyChanged(propertyName, oldValue, newValue, propertyMetadata);
}
public override string ToString()
{
return this.Title;
}
public void ReWindowState()
{
this.WindowState = this.oldState;
}
}
}