using BeDesignerSCADA.Common; using BPASmartClient.Compiler; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; namespace BeDesignerSCADA.Controls { /// /// MenuRunCanvas.xaml 的交互逻辑 /// public partial class MenuRunCanvas : UserControl { MenuRunCanvasModel menuRunCanvasModel = null; ResourceDictionary languageResDic = new ResourceDictionary(); DispatcherTimer timer1;//控制菜单显示隐藏功能 public MenuRunCanvas() { InitializeComponent(); languageResDic.Source = new Uri(@"/BPASmartClient.SCADAControl;component/Themes/Generic.xaml", UriKind.RelativeOrAbsolute); menuRunCanvasModel =new MenuRunCanvasModel(); this.DataContext=menuRunCanvasModel; timer1 = new DispatcherTimer(); timer1.Tick += new System.EventHandler(timer1_Tick); timer1.Interval = new TimeSpan(0, 0, 0, 0, 1000); timer1.Start(); this.Loaded += MenuRunCanvas_Loaded; } private void MenuRunCanvas_Loaded(object sender, RoutedEventArgs e) { if (menuRunCanvasModel.MenuModel != null) { switch (menuRunCanvasModel.MenuModel.Alignment) { case MenuAlignment.左边: menuWidth = wrapanl_main.ActualWidth; break; case MenuAlignment.顶部: menuWidth = wrapanl_main.ActualHeight; break; case MenuAlignment.右边: menuWidth = wrapanl_main.ActualWidth; break; case MenuAlignment.底部: menuWidth = wrapanl_main.ActualHeight; break; default: break; } MenuSmall(); } } private void timer1_Tick(object sender, EventArgs e) { times += 1; } /// /// 根据路径运行程序 /// public void LoadingData(string path) { RunMenu(BinaryFile.ReadBinary(path) as MenuModel); } /// /// 运行菜单 /// /// public void RunMenu(MenuModel canvas) { int k = 0; ItemsPanelTemplate itemsPanelTemplate = new ItemsPanelTemplate(); FrameworkElementFactory _StackPanel = new FrameworkElementFactory(typeof(StackPanel)); switch (canvas.Alignment) { case MenuAlignment.左边: if(!string.IsNullOrEmpty(canvas.Name)) k = (canvas.Name.Length / 5 -1+ (canvas.Name.Length % 5 > 0 ? 1 : 0))*15; //左边菜单栏 PageList.HorizontalAlignment = HorizontalAlignment.Stretch; PageList.VerticalAlignment = VerticalAlignment.Top; PageList.Margin = new Thickness(10,70+ k, 10,110); PageList.Padding = new Thickness(0,0,0,0); _StackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Vertical); //标题栏 TitleName.Margin = new Thickness(0, 10, 0, 10); TitleName.HorizontalAlignment = HorizontalAlignment.Stretch; TitleName.VerticalAlignment = VerticalAlignment.Top; border0.Margin = new Thickness(0, 60+k, 0, 0); border0.LayoutTransform = new RotateTransform() { Angle = 0 }; border0.HorizontalAlignment = HorizontalAlignment.Stretch; border0.VerticalAlignment = VerticalAlignment.Top; border1.Margin = new Thickness(0, 0, 0, 100); border1.LayoutTransform = new RotateTransform() { Angle = 0 }; border1.HorizontalAlignment = HorizontalAlignment.Stretch; border1.VerticalAlignment = VerticalAlignment.Bottom; //内部Grid wrapanl_main.HorizontalAlignment = HorizontalAlignment.Left; wrapanl_main.VerticalAlignment = VerticalAlignment.Stretch; wrapanl_main.Margin = new Thickness(0, 0, 0, 25); //外部Stackpanl waibustack.Orientation = Orientation.Horizontal; waibustack.HorizontalAlignment = HorizontalAlignment.Left; //拉出按钮 lachu.HorizontalAlignment = HorizontalAlignment.Left; lachu.VerticalAlignment = VerticalAlignment.Stretch; lachu.BorderThickness = new Thickness(1, 0, 0, 0); //closeGrid closeGrid.HorizontalAlignment = HorizontalAlignment.Center; closeGrid.VerticalAlignment = VerticalAlignment.Bottom; closeGrid.Margin = new Thickness(10); ButClose.Margin = new Thickness(0, 5, 0, 0); break; case MenuAlignment.顶部: //左边菜单栏 PageList.HorizontalAlignment = HorizontalAlignment.Stretch; PageList.VerticalAlignment = VerticalAlignment.Top; PageList.Margin = new Thickness(140,0 , 120, 0); PageList.Padding = new Thickness(0, 0, 0, 0); _StackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); //标题栏 TitleName.LayoutTransform = new RotateTransform() { Angle = 0 }; TitleName.Margin = new Thickness(10, 10, 10, 10); TitleName.HorizontalAlignment = HorizontalAlignment.Left; TitleName.VerticalAlignment = VerticalAlignment.Center; TitleName.Width = 110; border0.Margin = new Thickness(130, 0, 0, 0); border0.HorizontalAlignment = HorizontalAlignment.Left; border0.VerticalAlignment = VerticalAlignment.Stretch; border0.LayoutTransform = new RotateTransform() { Angle = 90 }; border1.Margin = new Thickness(0, 0, 110,0 ); border1.HorizontalAlignment = HorizontalAlignment.Right; border1.VerticalAlignment = VerticalAlignment.Stretch; border1.LayoutTransform = new RotateTransform() { Angle = 90 }; //内部Grid wrapanl_main.HorizontalAlignment = HorizontalAlignment.Stretch; wrapanl_main.VerticalAlignment = VerticalAlignment.Top; wrapanl_main.Margin = new Thickness(0, 0, 0, 0); //外部Stackpanl waibustack.Orientation = Orientation.Vertical; waibustack.VerticalAlignment = VerticalAlignment.Top; //拉出按钮 lachu.HorizontalAlignment = HorizontalAlignment.Stretch; lachu.VerticalAlignment = VerticalAlignment.Top; lachu.LayoutTransform = new RotateTransform() { Angle = 270 }; lachu.BorderThickness = new Thickness(0, 0, 1, 0); //closeGrid closeGrid.HorizontalAlignment = HorizontalAlignment.Right; closeGrid.VerticalAlignment = VerticalAlignment.Center; closeGrid.Margin = new Thickness(10, 0, 10, 0); ButClose.Margin = new Thickness(0,5, 0, 0); break; case MenuAlignment.右边: k = (canvas.Name.Length / 5 - 1 + (canvas.Name.Length % 5 > 0 ? 1 : 0)) * 15; //左边菜单栏 PageList.HorizontalAlignment = HorizontalAlignment.Right; PageList.VerticalAlignment = VerticalAlignment.Stretch; PageList.Margin = new Thickness(10, 70 + k, 10, 110); PageList.Padding = new Thickness(0, 0, 0, 0); _StackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Vertical); //标题栏 TitleName.Margin = new Thickness(0, 10, 0, 10); TitleName.HorizontalAlignment = HorizontalAlignment.Stretch; TitleName.VerticalAlignment = VerticalAlignment.Top; border0.Margin = new Thickness(0, 60 + k, 0, 0); border0.LayoutTransform = new RotateTransform() { Angle = 0 }; border0.HorizontalAlignment = HorizontalAlignment.Stretch; border0.VerticalAlignment = VerticalAlignment.Top; border1.Margin = new Thickness(0, 0, 0, 100); border1.LayoutTransform = new RotateTransform() { Angle = 0 }; border1.HorizontalAlignment = HorizontalAlignment.Stretch; border1.VerticalAlignment = VerticalAlignment.Bottom; //内部Grid wrapanl_main.HorizontalAlignment = HorizontalAlignment.Right; wrapanl_main.VerticalAlignment = VerticalAlignment.Stretch; wrapanl_main.Margin = new Thickness(0, 0, 0, 25); //外部Stackpanl waibustack.Orientation = Orientation.Horizontal; waibustack.HorizontalAlignment = HorizontalAlignment.Right; //拉出按钮 lachu.HorizontalAlignment = HorizontalAlignment.Right; lachu.VerticalAlignment = VerticalAlignment.Stretch; lachu.BorderThickness = new Thickness(0, 0, 1, 0); //closeGrid closeGrid.HorizontalAlignment = HorizontalAlignment.Center; closeGrid.VerticalAlignment = VerticalAlignment.Bottom; closeGrid.Margin = new Thickness(10); ButClose.Margin = new Thickness(0, 5, 0, 0); FrameworkElement border = null; foreach (FrameworkElement item in waibustack.Children) { if (item is Border) border = item; } waibustack.Children.Remove(border); waibustack.Children.Insert(0,border); break; case MenuAlignment.底部: //左边菜单栏 PageList.HorizontalAlignment = HorizontalAlignment.Stretch; PageList.VerticalAlignment = VerticalAlignment.Bottom; PageList.Margin = new Thickness(140, 0, 120, 0); PageList.Padding = new Thickness(0, 0, 0, 0); _StackPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); //标题栏 TitleName.LayoutTransform = new RotateTransform() { Angle = 0 }; TitleName.Margin = new Thickness(10, 10, 10, 10); TitleName.HorizontalAlignment = HorizontalAlignment.Left; TitleName.VerticalAlignment = VerticalAlignment.Center; TitleName.Width = 110; border0.Margin = new Thickness(130, 0, 0, 0); border0.HorizontalAlignment = HorizontalAlignment.Left; border0.VerticalAlignment = VerticalAlignment.Stretch; border0.LayoutTransform = new RotateTransform() { Angle = 90 }; border1.Margin = new Thickness(0, 0, 110, 0); border1.HorizontalAlignment = HorizontalAlignment.Right; border1.VerticalAlignment = VerticalAlignment.Stretch; border1.LayoutTransform = new RotateTransform() { Angle = 90 }; //内部Grid wrapanl_main.HorizontalAlignment = HorizontalAlignment.Stretch; wrapanl_main.VerticalAlignment = VerticalAlignment.Bottom; wrapanl_main.Margin = new Thickness(0, 0, 0, 0); //外部Stackpanl waibustack.Orientation = Orientation.Vertical; waibustack.VerticalAlignment = VerticalAlignment.Bottom; waibustack.Margin = new Thickness(0, 0, 0, 25); //拉出按钮 lachu.HorizontalAlignment = HorizontalAlignment.Stretch; lachu.VerticalAlignment = VerticalAlignment.Bottom; lachu.LayoutTransform = new RotateTransform() { Angle = 270 }; lachu.BorderThickness = new Thickness(1, 0, 0, 0); lachu.Margin = new Thickness(0, 0, 0, 0); //closeGrid closeGrid.HorizontalAlignment = HorizontalAlignment.Right; closeGrid.VerticalAlignment = VerticalAlignment.Center; closeGrid.Margin = new Thickness(10, 0, 10, 0); ButClose.Margin = new Thickness(0, 5, 0, 0); border = null; foreach (FrameworkElement item in waibustack.Children) { if (item is Border) border = item; } waibustack.Children.Remove(border); waibustack.Children.Insert(0, border); break; default: break; } if(canvas.IsVisibility) waibustack.Visibility = Visibility.Visible; else waibustack.Visibility = Visibility.Collapsed; itemsPanelTemplate.VisualTree = _StackPanel; PageList.ItemsPanel = itemsPanelTemplate; menuRunCanvasModel.MenuModel = canvas; menuRunCanvasModel.border = main; menuRunCanvasModel.MenuModel?.pageModels?.ToList().ForEach(par => { List Children = new List(); par.visual = new CanvasPanel(); par?.ChildrenStr?.ForEach(k => { FrameworkElement item = XamlReader.Parse(k) as FrameworkElement; par.visual.Children.Add(item); FrameworkElement item1 = XamlReader.Parse(k) as FrameworkElement; Children.Add(item1); }); RunCanvas runCanvas = new RunCanvas(); runCanvas.Init(Children); par.runCanvas = runCanvas; }); if (menuRunCanvasModel.MenuModel.pageModels.Count > 0) { menuRunCanvasModel.MenuModel.SelectPageModels = menuRunCanvasModel.MenuModel.pageModels[0]; main.Child = menuRunCanvasModel.MenuModel.SelectPageModels.runCanvas; menuRunCanvasModel.MenuModel.SelectPageModels.runCanvas.RunSelect(); } } /// /// 父窗体控制事件 /// /// public void PandClick(Window obj) { this.ButClose.Click += (o, e) => { MessageBoxResult res = MessageBox.Show("确定关闭上位机软件?", "警告", MessageBoxButton.OKCancel); if (res == MessageBoxResult.OK) { obj.Close(); } }; this.waibustack.MouseLeftButtonDown += (o, e) => { if (e.ClickCount > 1) { if (obj.WindowState == WindowState.Maximized) obj.WindowState = WindowState.Normal; else if (obj.WindowState == WindowState.Normal) obj.WindowState = WindowState.Maximized; } if (e.LeftButton == MouseButtonState.Pressed) obj.DragMove(); }; } bool ismove = true; int times = 0; /// /// 鼠标移动 /// /// /// private void wrapanl_main_MouseMove(object sender, MouseEventArgs e) { if (times < 1) { return; } if (ismove) { ismove = false; MenuBig(); } times = 0; } /// /// 鼠标离开 /// /// /// private void wrapanl_main_MouseLeave(object sender, MouseEventArgs e) { menuWidthDefault = 0; if (!ismove) { ismove = true; MenuSmall(); } times = 0; } private int menuWidthDefault = 0; private double menuWidth = 86; private void MenuBig() { DoubleAnimation da = new DoubleAnimation(); da.From = menuWidthDefault; da.To = menuWidth; da.Duration = TimeSpan.FromSeconds(0.1); if (menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.左边 || menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.右边) { wrapanl_main.BeginAnimation(WidthProperty, da); if (languageResDic.Contains("image左1")) lachuStyle.Style = languageResDic["image左1"] as Style; } else if (menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.顶部 || menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.底部) { wrapanl_main.BeginAnimation(HeightProperty, da); if (languageResDic.Contains("image右1")) lachuStyle.Style = languageResDic["image右1"] as Style; } } private void MenuSmall() { DoubleAnimation da = new DoubleAnimation(); da.From = menuWidth; da.To = menuWidthDefault; da.Duration = TimeSpan.FromSeconds(0.1); if (menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.左边 || menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.右边) { if (languageResDic.Contains("image右1")) lachuStyle.Style = languageResDic["image右1"] as Style; wrapanl_main.BeginAnimation(WidthProperty, da); } else if (menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.顶部 || menuRunCanvasModel.MenuModel.Alignment == MenuAlignment.底部) { wrapanl_main.BeginAnimation(HeightProperty, da); if (languageResDic.Contains("image左1")) lachuStyle.Style = languageResDic["image左1"] as Style; } } } /// /// 运行Model /// public class MenuRunCanvasModel : ObservableObject { /// /// 菜单Model /// private MenuModel _MenuModel; public MenuModel MenuModel { get { return _MenuModel; } set { _MenuModel = value; OnPropertyChanged("MenuModel"); } } public RelayCommand MenSelectPageCommand { get; set; } public Border border { get; set; } public MenuRunCanvasModel() { MenuModel = new MenuModel(); MenuModel.pageModels = new ObservableCollection(); MenuModel.pageModels.Add(new PageModel { Name = "首页面", Sort = 0 }); MenuModel.SelectPageModels = MenuModel.pageModels[0]; MenSelectPageCommand = new RelayCommand(new Action(SelectPageHeader)); } /// /// 选中该页 /// /// public void SelectPageHeader(object obj) { try { //MenuModel.SelectPageModels.runCanvas.Destory(); MenuModel.SelectPageModels = obj as PageModel; border.Child = MenuModel.SelectPageModels.runCanvas; MenuModel.SelectPageModels.runCanvas.RunSelect(); } catch (Exception ex) { } } } }