using System; using System.Linq; using System.Runtime.InteropServices; using System.Runtime.Remoting.Contexts; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interop; using WxOrder.Framework.Commons; using WxOrder.Framework.Commons.Dialogs; namespace WxOrder.Framework.Commons { /// /// DialogWindow.xaml 的交互逻辑 /// public partial class DialogWindow : Window, IDialogWindow { private readonly Style dialogWindowStyle; private bool isConstructorSuccessful = false; private UserControl _content; public DialogWindow(Window owner = null) { InitializeComponent(); Owner = owner ?? Application.Current.MainWindow; isConstructorSuccessful = true; dialogWindowStyle = (Style)TryFindResource("DialogWindowStyle"); Loaded += DialogWindow_Loaded; } private void DialogWindow_Loaded(object sender, RoutedEventArgs e) { Loaded -= DialogWindow_Loaded; } protected override void OnContentChanged(object oldContent, object newContent) { this.Style = dialogWindowStyle; if (newContent is System.Windows.Controls.UserControl control) { _content = control; Setter widthSetter = this.Style.Setters.OfType().FirstOrDefault(a => a.Property == WidthProperty); if (widthSetter == null) { if (double.IsNaN(control.Width)) { this.Width = DialogWindowProperty.GetWidth(control); } else { this.Width = control.Width; control.Width = double.NaN; } } Setter heightSetter = this.Style.Setters.OfType().FirstOrDefault(a => a.Property == HeightProperty); if (heightSetter == null) { if (double.IsNaN(control.Height)) { this.Height = DialogWindowProperty.GetHeight(control); } else { this.Height = control.Height; control.Height = double.NaN; } } ResizeMode resizeExt = DialogWindowProperty.GetResizeMode(control); if (resizeExt != ResizeMode.CanResize) { this.ResizeMode = resizeExt; } WindowStyle wstyleExt = DialogWindowProperty.GetWindowStyle(control); if (wstyleExt != WindowStyle.SingleBorderWindow) { this.WindowStyle = wstyleExt; } } base.OnContentChanged(oldContent, newContent); } protected override void OnStyleChanged(Style oldStyle, Style newStyle) { if (newStyle.TargetType == typeof(Window)) { if (isConstructorSuccessful) { //dialogWindowStyle.Merge(newStyle); } return; } base.OnStyleChanged(oldStyle, newStyle); } #region ShowMiniButton private const int GWL_STYLE = -16; private const int WS_MINIMIZEBOX = 0x00020000; public IDialogResult Result { get; set; } [DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); var hwnd = new WindowInteropHelper(this).Handle; int style = GetWindowLong(hwnd, GWL_STYLE); style &= ~WS_MINIMIZEBOX; if (DialogWindowProperty.GetShowMinButton(this._content) == false) { SetWindowLong(hwnd, GWL_STYLE, style); } } #endregion } }