using Gestor.Application.Actions; using MaterialDesignThemes.Wpf; using System; using System.CodeDom.Compiler; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Shell; using System.Windows.Threading; namespace Gestor.Application.Views.Generic { public class HosterWindow : Window, IComponentConnector { private bool _buttonClickable; internal System.Windows.Shell.WindowChrome WindowChrome; internal Grid Menu; internal Grid MinimizeButton; internal Path MinimizeButtonPath; internal Grid MaximizeButton; internal Path MaximizeButtonPath; internal Grid CloseButton; internal System.Windows.Controls.ContentControl ContentControl; internal DialogHost HosterDialog; internal MaterialDesignThemes.Wpf.DrawerHost DrawerHost; internal MaterialDesignThemes.Wpf.Snackbar Snackbar; private bool _contentLoaded; public string Tela { get; set; } public HosterWindow(System.Windows.Controls.ContentControl control, string titulo, double? x = null, double? y = null, bool canMaximize = false) { this.InitializeComponent(); Gestor.Application.Actions.Actions.EnableMainMenu = (Action)Delegate.Combine(Gestor.Application.Actions.Actions.EnableMainMenu, new Action(this.HabilitarMenu)); Gestor.Application.Actions.Actions.CloseHoster = (Action)Delegate.Combine(Gestor.Application.Actions.Actions.CloseHoster, new Action(this.Close)); this.MinimizeButton.MouseEnter += new System.Windows.Input.MouseEventHandler(HosterWindow.TopControls_OnMouseEnter); this.MinimizeButton.MouseLeave += new System.Windows.Input.MouseEventHandler(this.TopControls_OnMouseLeave); this.MaximizeButton.MouseEnter += new System.Windows.Input.MouseEventHandler(HosterWindow.TopControls_OnMouseEnter); this.MaximizeButton.MouseLeave += new System.Windows.Input.MouseEventHandler(this.TopControls_OnMouseLeave); this.CloseButton.MouseEnter += new System.Windows.Input.MouseEventHandler(HosterWindow.TopControls_OnMouseEnter); this.CloseButton.MouseLeave += new System.Windows.Input.MouseEventHandler(this.TopControls_OnMouseLeave); this.ContentControl.Content = control.Content; this.ContentControl.DataContext = control.DataContext; this.ContentControl.Tag = titulo; this.MaximizeButtonPath.Data = Geometry.Parse((string)System.Windows.Application.Current.Resources["Restore"]); if ((x.HasValue || y.HasValue) && !canMaximize) { base.WindowState = System.Windows.WindowState.Normal; base.BorderThickness = new Thickness(1); this.WindowChrome.ResizeBorderThickness = new Thickness(4); this.WindowChrome.CaptionHeight = 30; this.MaximizeButtonPath.Data = Geometry.Parse((string)System.Windows.Application.Current.Resources["Maximize"]); } if (x.HasValue) { if (x.Value >= base.Width) { if (!canMaximize) { base.MaxWidth = x.Value; base.Width = x.Value; } base.MinWidth = x.Value; } else { base.MinWidth = x.Value; if (!canMaximize) { base.Width = x.Value; base.MaxWidth = x.Value; } } } if (y.HasValue) { if (y.Value >= base.Height) { if (!canMaximize) { base.MaxHeight = y.Value; base.Height = y.Value; } base.MinHeight = y.Value; } else { base.MinHeight = y.Value; if (!canMaximize) { base.Height = y.Value; base.MaxHeight = y.Value; } } } if (!x.HasValue || !y.HasValue || canMaximize) { base.ResizeMode = System.Windows.ResizeMode.CanResize; } else { this.MaximizeButton.IsEnabled = false; this.MinimizeButton.IsEnabled = false; this.MaximizeButtonPath.Stroke = new SolidColorBrush(Colors.Gray); this.MinimizeButtonPath.Stroke = new SolidColorBrush(Colors.Gray); } base.Title = titulo; this.Tela = control.GetType().Name; this.HosterDialog.set_Identifier(string.Format("Dialog{0}{1}", this.Tela, Guid.NewGuid())); } public void CloseButton_Click() { base.Close(); } [DllImport("user32.dll", CharSet=CharSet.None, ExactSpelling=false)] private static extern bool GetCursorPos(out HosterWindow.Point lpPoint); [DllImport("user32.dll", CharSet=CharSet.None, ExactSpelling=false)] private static extern bool GetMonitorInfo(IntPtr hMonitor, HosterWindow.Monitorinfo lpmi); private void HabilitarMenu(bool enable) { this.Menu.IsEnabled = enable; } private void HosterWindow_OnLoaded(object sender, RoutedEventArgs e) { Task.Run(() => { Thread.Sleep(500); System.Windows.Application.Current.Dispatcher.Invoke(() => base.Topmost = false); }); } [DebuggerNonUserCode] [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] public void InitializeComponent() { if (this._contentLoaded) { return; } this._contentLoaded = true; System.Windows.Application.LoadComponent(this, new Uri("/Gestor.Application;component/views/generic/hosterwindow.xaml", UriKind.Relative)); } public void MaximizeButton_Click() { base.WindowState = (base.WindowState == System.Windows.WindowState.Normal ? System.Windows.WindowState.Maximized : System.Windows.WindowState.Normal); } public void MinimizeButton_Click() { base.WindowState = System.Windows.WindowState.Minimized; } [DllImport("user32.dll", CharSet=CharSet.None, ExactSpelling=false, SetLastError=true)] private static extern IntPtr MonitorFromPoint(HosterWindow.Point pt, HosterWindow.MonitorOptions dwFlags); protected sealed override void OnStateChanged(EventArgs e) { object obj; base.BorderThickness = new Thickness((double)(base.WindowState != System.Windows.WindowState.Maximized)); System.Windows.Shell.WindowChrome windowChrome = this.WindowChrome; if (base.WindowState == System.Windows.WindowState.Maximized) { obj = null; } else { obj = 4; } windowChrome.ResizeBorderThickness = new Thickness((double)obj); this.WindowChrome.CaptionHeight = (double)((base.WindowState == System.Windows.WindowState.Maximized ? 32 : 29)); this.MaximizeButtonPath.Data = (base.WindowState == System.Windows.WindowState.Maximized ? Geometry.Parse((string)System.Windows.Application.Current.Resources["Restore"]) : Geometry.Parse((string)System.Windows.Application.Current.Resources["Maximize"])); base.OnStateChanged(e); } private void SnackbarMessage_ActionClick(object sender, RoutedEventArgs e) { this.Snackbar.set_IsActive(false); } [DebuggerNonUserCode] [EditorBrowsable(EditorBrowsableState.Never)] [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { switch (connectionId) { case 1: { ((HosterWindow)target).Initialized += new EventHandler(this.Window_OnInitialized); ((HosterWindow)target).Loaded += new RoutedEventHandler(this.HosterWindow_OnLoaded); return; } case 2: { this.WindowChrome = (System.Windows.Shell.WindowChrome)target; return; } case 3: { this.Menu = (Grid)target; return; } case 4: { this.MinimizeButton = (Grid)target; this.MinimizeButton.MouseLeftButtonDown += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonDown); this.MinimizeButton.MouseLeftButtonUp += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonUp); return; } case 5: { this.MinimizeButtonPath = (Path)target; return; } case 6: { this.MaximizeButton = (Grid)target; this.MaximizeButton.MouseLeftButtonDown += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonDown); this.MaximizeButton.MouseLeftButtonUp += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonUp); return; } case 7: { this.MaximizeButtonPath = (Path)target; return; } case 8: { this.CloseButton = (Grid)target; this.CloseButton.MouseLeftButtonDown += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonDown); this.CloseButton.MouseLeftButtonUp += new MouseButtonEventHandler(this.TopControls_OnMouseLeftButtonUp); return; } case 9: { this.ContentControl = (System.Windows.Controls.ContentControl)target; return; } case 10: { this.HosterDialog = (DialogHost)target; return; } case 11: { this.DrawerHost = (MaterialDesignThemes.Wpf.DrawerHost)target; return; } case 12: { this.Snackbar = (MaterialDesignThemes.Wpf.Snackbar)target; return; } case 13: { ((SnackbarMessage)target).add_ActionClick(new RoutedEventHandler(this.SnackbarMessage_ActionClick)); return; } } this._contentLoaded = true; } private static void TopControls_OnMouseEnter(object sender, System.Windows.Input.MouseEventArgs e) { ((Grid)sender).Background = (((Grid)sender).Name == "CloseButton" ? new SolidColorBrush(Colors.IndianRed) : new SolidColorBrush(Colors.Gray)); Path child = VisualTreeHelper.GetChild((Grid)sender, 0) as Path; if (child != null) { child.Stroke = new SolidColorBrush(Colors.White); } } private void TopControls_OnMouseLeave(object sender, System.Windows.Input.MouseEventArgs e) { this._buttonClickable = false; ((Grid)sender).Background = new SolidColorBrush(Colors.Transparent); Path child = VisualTreeHelper.GetChild((Grid)sender, 0) as Path; if (child != null) { child.Stroke = new SolidColorBrush(Colors.White); } } private void TopControls_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this._buttonClickable = true; if (((Grid)sender).Name != "CloseButton") { ((Grid)sender).Background = new SolidColorBrush(Colors.DimGray); } else { ((Grid)sender).Background = new SolidColorBrush(Colors.Red); Path child = VisualTreeHelper.GetChild((Grid)sender, 0) as Path; if (child != null) { child.Stroke = new SolidColorBrush(Colors.Black); return; } } } private void TopControls_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (this._buttonClickable) { MethodInfo method = base.GetType().GetMethod(string.Concat(((Grid)sender).Name, "_Click")); if (method == null) { return; } method.Invoke(this, null); } } private void Window_OnInitialized(object sender, EventArgs e) { WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this); windowInteropHelper.EnsureHandle(); HwndSource hwndSource = HwndSource.FromHwnd(windowInteropHelper.Handle); if (hwndSource == null) { return; } hwndSource.AddHook(new HwndSourceHook(this.WindowProc)); } private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == 36) { this.WmGetMinMaxInfo(lParam); } return IntPtr.Zero; } private void WmGetMinMaxInfo(IntPtr lParam) { HosterWindow.Point point; System.Drawing.Rectangle workingArea = Screen.FromHandle((new WindowInteropHelper(this)).Handle).WorkingArea; base.MaxHeight = (double)workingArea.Height; HosterWindow.GetCursorPos(out point); IntPtr intPtr = HosterWindow.MonitorFromPoint(new HosterWindow.Point(0, 0), HosterWindow.MonitorOptions.MonitorDefaulttoprimary); HosterWindow.Monitorinfo monitorinfo = new HosterWindow.Monitorinfo(); if (!HosterWindow.GetMonitorInfo(intPtr, monitorinfo)) { return; } IntPtr intPtr1 = HosterWindow.MonitorFromPoint(point, HosterWindow.MonitorOptions.MonitorDefaulttonearest); HosterWindow.Minmaxinfo structure = (HosterWindow.Minmaxinfo)Marshal.PtrToStructure(lParam, typeof(HosterWindow.Minmaxinfo)); if (!intPtr.Equals(intPtr1)) { structure.ptMaxPosition.X = monitorinfo.rcMonitor.Left; structure.ptMaxPosition.Y = monitorinfo.rcMonitor.Top; structure.ptMaxSize.X = monitorinfo.rcMonitor.Right - monitorinfo.rcMonitor.Left; structure.ptMaxSize.Y = monitorinfo.rcMonitor.Bottom - monitorinfo.rcMonitor.Top; } else { structure.ptMaxPosition.X = monitorinfo.rcWork.Left; structure.ptMaxPosition.Y = monitorinfo.rcWork.Top; structure.ptMaxSize.X = monitorinfo.rcWork.Right - monitorinfo.rcWork.Left; structure.ptMaxSize.Y = monitorinfo.rcWork.Bottom - monitorinfo.rcWork.Top; } Marshal.StructureToPtr(structure, lParam, true); } private struct Minmaxinfo { private readonly HosterWindow.Point ptReserved; public HosterWindow.Point ptMaxSize; public HosterWindow.Point ptMaxPosition; private readonly HosterWindow.Point ptMinTrackSize; private readonly HosterWindow.Point ptMaxTrackSize; } private class Monitorinfo { private readonly int cbSize; public readonly HosterWindow.Rect rcMonitor; public readonly HosterWindow.Rect rcWork; private readonly int dwFlags; public Monitorinfo() { } } private enum MonitorOptions : uint { MonitorDefaulttoprimary = 1, MonitorDefaulttonearest = 2 } public struct Point { public int X; public int Y; public Point(int x, int y) { this.X = x; this.Y = y; } } public struct Rect { public int Left; public int Top; public int Right; public int Bottom; } } }