/// <summary> /// 开始进入全屏模式 /// 进入全屏模式后,窗口可通过 API 方式(也可以用 Win + Shift + Left/Right)移动,调整大小,但会根据目标矩形寻找显示器重新调整到全屏状态。 /// 进入全屏后,不要修改样式等窗口属性,在退出时,会恢复到进入前的状态 /// 进入全屏模式后会禁用 DWM 过渡动画 /// </summary> public static void StartFullScreen(System.Windows.Window window) { if (window == null) { throw new ArgumentNullException(nameof(window), $"{nameof(window)} 不能为 null"); } //确保不在全屏模式 if (window.GetValue(BeforeFullScreenWindowPlacementProperty) == null && window.GetValue(BeforeFullScreenWindowStyleProperty) == null) { var hwnd = new WindowInteropHelper(window).EnsureHandle(); var hwndSource = HwndSource.FromHwnd(hwnd); //获取当前窗口的位置大小状态并保存 var placement = InteropMethods.GetWindowPlacement(hwnd); window.SetValue(BeforeFullScreenWindowPlacementProperty, placement); //修改窗口样式 var style = (InteropValues.WindowStyles) InteropMethods.GetWindowLongPtr(hwnd, InteropValues.GWL_STYLE); window.SetValue(BeforeFullScreenWindowStyleProperty, style); //将窗口恢复到还原模式,在有标题栏的情况下最大化模式下无法全屏, //这里采用还原,不修改标题栏的方式 //在退出全屏时,窗口原有的状态会恢复 //去掉WS_THICKFRAME,在有该样式的情况下不能全屏 //去掉WS_MAXIMIZEBOX,禁用最大化,如果最大化会退出全屏 //去掉WS_MAXIMIZE,使窗口变成还原状态,不使用ShowWindow(hwnd, ShowWindowCommands.SW_RESTORE),避免看到窗口变成还原状态这一过程(也避免影响窗口的Visible状态) style &= ~(InteropValues.WindowStyles.WS_THICKFRAME | InteropValues.WindowStyles.WS_MAXIMIZEBOX | InteropValues.WindowStyles.WS_MAXIMIZE); InteropMethods.SetWindowLong(hwnd, InteropValues.GWL_STYLE, (IntPtr) style); //禁用 DWM 过渡动画 忽略返回值,若DWM关闭不做处理 InteropMethods.DwmSetWindowAttribute(hwnd, InteropValues.DwmWindowAttribute.DWMWA_TRANSITIONS_FORCEDISABLED, 1, sizeof(int)); //添加Hook,在窗口尺寸位置等要发生变化时,确保全屏 hwndSource.AddHook(KeepFullScreenHook); if (InteropMethods.GetWindowRect(hwnd, out var rect)) { //不能用 placement 的坐标,placement是工作区坐标,不是屏幕坐标。 //使用窗口当前的矩形调用下设置窗口位置和尺寸的方法,让Hook来进行调整窗口位置和尺寸到全屏模式 InteropMethods.SetWindowPos(hwnd, (IntPtr) InteropValues.HWND_TOP, rect.Left, rect.Top, rect.Width, rect.Height, (int) InteropValues.WindowPositionFlags.SWP_NOZORDER); } } }
private static void DettachWindowListeners(Window window, object vm) { if(vm != null) { var listener = (WindowMessageListener)window.GetValue(WindowMessageListenerProperty); if(listener != null) { window.SetValue(WindowMessageListenerProperty, null); listener.Dettach(vm, window); } } }
public static bool GetCanMaximize(Window obj) { if (obj == null) { throw new ArgumentNullException("obj"); } return (bool)obj.GetValue(CanMaximizeProperty); }
private static HotkeyServiceHelper EnsureHotkeyServiceHelper(Window window) { HotkeyServiceHelper helper = (HotkeyServiceHelper)window.GetValue(HotkeyServiceHelperPropertyKey.DependencyProperty); if (helper == null) { helper = new HotkeyServiceHelper(window); window.SetValue(HotkeyServiceHelperPropertyKey, helper); } return helper; }
private static WindowExtensions _EnsureAttachedExtensions(Window window) { Assert.IsNotNull(window); var ext = (WindowExtensions)window.GetValue(WindowExtensionsProperty); if (ext == null) { ext = new WindowExtensions(window); window.SetValue(WindowExtensionsProperty, ext); } return ext; }
static Window FindFirstModalDialog(Window ownerWindow) { if (ownerWindow != null) { if (ownerWindow.OwnedWindows.Count != 0) { foreach (Window w in ownerWindow.OwnedWindows) { var window = FindFirstModalDialog(w); if (window != null) { return window; } } } if ((bool)ownerWindow.GetValue(IsModalProperty) && ownerWindow.IsEnabled && ownerWindow.IsVisible && ownerWindow.IsActive) { return ownerWindow; } } return null; }
public static ApplicationBar GetApplicationBar([NotNull] System.Windows.Window obj) { ValidationHelper.NotNull(obj, "obj"); return((ApplicationBar)obj.GetValue(ApplicationBarProperty)); }
public static bool GetIsClosingAsPartOfDragOperation(Window element) { return (bool) element.GetValue(IsClosingAsPartOfDragOperationProperty); }
public static WindowChrome GetInstance(Window window) { return (WindowChrome)window.GetValue(InstanceProperty); }
public static Settings GetSettings(Window element) { return (Settings)element.GetValue(SettingsProperty); }
public static bool GetCloseCondition(Window obj) { return (bool)obj.GetValue(CloseConditionProperty); }
public static bool GetDisableControls(System.Windows.Window wnd) => (bool)wnd.GetValue(DisableControlsProperty);
/// <summary> /// ウィンドウ自身を取得します。 /// </summary> /// <param name="target">取得元</param> /// <returns>ウィンドウそのもの</returns> public static MainWindow GetWindowItself(Window target) { return (MainWindow)target.GetValue(WindowItselfProperty); }
/// <summary> /// 退出全屏模式 /// 窗口会回到进入全屏模式时保存的状态 /// 退出全屏模式后会重新启用 DWM 过渡动画 /// </summary> public static void EndFullScreen(System.Windows.Window window) { if (window == null) { throw new ArgumentNullException(nameof(window), $"{nameof(window)} 不能为 null"); } //确保在全屏模式并获取之前保存的状态 if (window.GetValue(BeforeFullScreenWindowPlacementProperty) is InteropValues.WINDOWPLACEMENT placement && window.GetValue(BeforeFullScreenWindowStyleProperty) is InteropValues.WindowStyles style) { var hwnd = new WindowInteropHelper(window).Handle; if (hwnd == IntPtr.Zero) { // 句柄为 0 只有两种情况: // 1. 虽然窗口已进入全屏,但窗口已被关闭; // 2. 窗口初始化前,在还没有调用 StartFullScreen 的前提下就调用了此方法。 // 所以,直接 return 就好。 return; } var hwndSource = HwndSource.FromHwnd(hwnd); //去除hook hwndSource.RemoveHook(KeepFullScreenHook); //恢复保存的状态 //不要改变Style里的WS_MAXIMIZE,否则会使窗口变成最大化状态,但是尺寸不对 //也不要设置回Style里的WS_MINIMIZE,否则会导致窗口最小化按钮显示成还原按钮 InteropMethods.SetWindowLong(hwnd, InteropValues.GWL_STYLE, (IntPtr) (style & ~(InteropValues.WindowStyles.WS_MAXIMIZE | InteropValues.WindowStyles.WS_MINIMIZE))); if ((style & InteropValues.WindowStyles.WS_MINIMIZE) != 0) { //如果窗口进入全屏前是最小化的,这里不让窗口恢复到之前的最小化状态,而是到还原的状态。 //大多数情况下,都不期望在退出全屏的时候,恢复到最小化。 placement.showCmd = InteropValues.SW.RESTORE; } if ((style & InteropValues.WindowStyles.WS_MAXIMIZE) != 0) { //提前调用 ShowWindow 使窗口恢复最大化,若通过 SetWindowPlacement 最大化会导致闪烁,只靠其恢复 RestoreBounds. InteropMethods.ShowWindow(hwnd, InteropValues.SW.MAXIMIZE); } InteropMethods.SetWindowPlacement(hwnd, ref placement); if ((style & InteropValues.WindowStyles.WS_MAXIMIZE) == 0) //如果窗口是最大化就不要修改WPF属性,否则会破坏RestoreBounds,且WPF窗口自身在最大化时,不会修改 Left Top Width Height 属性 { if (InteropMethods.GetWindowRect(hwnd, out var rect)) { //不能用 placement 的坐标,placement是工作区坐标,不是屏幕坐标。 //确保窗口的 WPF 属性与 Win32 位置一致 var logicalPos = hwndSource.CompositionTarget.TransformFromDevice.Transform( new Point(rect.Left, rect.Top)); var logicalSize = hwndSource.CompositionTarget.TransformFromDevice.Transform( new Point(rect.Width, rect.Height)); window.Left = logicalPos.X; window.Top = logicalPos.Y; window.Width = logicalSize.X; window.Height = logicalSize.Y; } } //重新启用 DWM 过渡动画 忽略返回值,若DWM关闭不做处理 InteropMethods.DwmSetWindowAttribute(hwnd, InteropValues.DwmWindowAttribute.DWMWA_TRANSITIONS_FORCEDISABLED, 0, sizeof(int)); //删除保存的状态 window.ClearValue(BeforeFullScreenWindowPlacementProperty); window.ClearValue(BeforeFullScreenWindowStyleProperty); } }
public static bool?GetDialogResult(Ctrl target) { return((bool?)target.GetValue(DialogResultProperty)); }
public static ApplicationExitingDelegate GetApplicationClosing(Window element) { return (ApplicationExitingDelegate)element.GetValue(ApplicationClosingProperty); }
public static IInputElement GetInitialFocusElement(Window obj) { return (IInputElement) obj.GetValue(InitialFocusElementProperty); }
public static bool GetRemoveIcon(Ctrl target) { return((bool)target.GetValue(RemoveIconProperty)); }
public static bool GetFixMaximize(Window ribbonWindow) { return (bool)ribbonWindow.GetValue(FixMaximizeProperty); }
public static bool GetCloseWindow(Window window) { return (bool)window.GetValue(WindowBehaviour.CloseWindowProperty); ; }
/// <summary>添付プロパティから撫でた量を取得します。</summary> /// <param name="target">取得元</param> /// <returns>撫でた量</returns> public static double GetNadenade(Window target) { return (double)target.GetValue(NadenadeProperty); }
public void ShowDialog(ViewModelBase dialogModel) { var v = new Window(); var vm = dialogModel; Binding tagBinding = null; object windowTagValue=null; v.DataContext = vm; vm.HostWindow =v; if (!string.IsNullOrWhiteSpace(vm._DirtyDetectBindingPath) && vm._EditingData!=null) { tagBinding = new Binding(vm._DirtyDetectBindingPath); tagBinding.UpdateSourceTrigger=UpdateSourceTrigger.PropertyChanged; BindingOperations.SetBinding(v, Window.TagProperty, tagBinding); v.Closing += (o, e) => { if (vm._CloseTipFlag) { windowTagValue = v.GetValue(Window.TagProperty); if (windowTagValue != null && windowTagValue is bool && (bool)windowTagValue) { if (MessageBox.Show("所编辑的数据将被取消,是否继续?", "操作提示", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { e.Cancel = true; } else { vm._EditingData.StopDirtyDetect(); vm._IsEditingDataEnableValidation = false; vm._EditingData.IsEnableValidation = false; } } } else { vm._EditingData.StopDirtyDetect(); vm._IsEditingDataEnableValidation = false; vm._EditingData.IsEnableValidation = false; } }; } vm.ParentModel = this; vm.MainWindow = this.MainWindow; BindingOperations.SetBinding(v,Window.ContentProperty,new Binding()); BindingOperations.SetBinding(v,Window.TitleProperty,new Binding("Title")); v.SizeToContent=SizeToContent.WidthAndHeight; v.ResizeMode=ResizeMode.NoResize; v.Icon=this.MainWindow.Icon; v.Owner=this.HostWindow; v.WindowStartupLocation=WindowStartupLocation.CenterOwner; v.Loaded += (o, e) => { if (vm._EditingData != null) { vm._EditingData.IsEnableValidation = vm._IsEditingDataEnableValidation; } }; v.ShowDialog(); }
private static TooltipPopup GetTooltipPopup (Window d) { return (TooltipPopup)d.GetValue(TooltipPopupProperty); }
public static bool GetIsModalProperty(Window window) { return (bool)window.GetValue(IsModalProperty); }
private static FrameworkElement GetCurrentValidatedControl (Window d) { return (FrameworkElement)d.GetValue(CurrentValidatedControlProperty); }
/// <summary> /// Gets the value of the WindowMovement.IsDraggable attached property on a Window. /// </summary> /// <param name="window">The Window to set the property on.</param> /// <returns>The value of the property on the Window.</returns> public static bool GetIsDraggable(Window window) { return (bool) window.GetValue(IsDraggableProperty); }
/// <summary> /// Gets the DialogResult value from the specified target. /// </summary> /// <param name="target">The target.</param> /// <returns>The value.</returns> public static bool? GetDialogResult(Window target) { return (bool?)target.GetValue(DialogResultProperty); }
public static bool GetIsEnabled(Window window) => (bool) window.GetValue(IsEnabledProperty);
/// <summary> /// Gets the extra content on the caption area. /// </summary> /// <param name="window">The object.</param> /// <returns></returns> public static object GetCaptionExtraContent(Window window) { if (window == null) { throw new ArgumentNullException("window"); } return (object)window.GetValue(CaptionExtraContentProperty); }
public static WindowChrome GetWindowChrome(Window window) { Verify.IsNotNull(window, "window"); return (WindowChrome)window.GetValue(WindowChromeProperty); }
public static bool GetHasMaximizeButton(Window element) { return (bool)element.GetValue(ControlBox.HasMaximizeButtonProperty); }
public static bool GetIsHiddenCloseButton(Window obj) { return (bool)obj.GetValue(IsHiddenCloseButtonProperty); }
public static bool GetIsMainWindow([NotNull] System.Windows.Window obj) { ValidationHelper.NotNull(obj, "obj"); return(BooleanBoxingHelper.Unbox(obj.GetValue(IsMainWindowProperty))); }
public static Chrome GetChrome(Window window) { if (window == null) { throw new ArgumentNullException("window"); } return (Chrome)window.GetValue(Chrome.ChromeProperty); }
public static bool GetHideCloseButton(Window obj) { return (bool)obj.GetValue(HideCloseButtonProperty); }
public static bool GetHasMinimizeButton(Window element) { return (bool)element.GetValue(HasMinimizeButtonProperty); }
/// <summary>Gets the behaviors. /// </summary> /// <param name="window">The window.</param> /// <returns></returns> private static WindowBehaviors GetInternalBehaviors(Window window) { if (window == null) { throw new ArgumentNullException("window"); } var behaviors = (WindowBehaviors)window.GetValue(BehaviorsProperty); if(behaviors == null) { behaviors = new WindowBehaviors { Window = window }; window.SetValue(BehaviorsProperty, behaviors); } else if(behaviors.Window != null) { behaviors.Window = window; window.SetValue(BehaviorsProperty, behaviors); } return behaviors; }