private static void RaiseVisualTreeChangedEvent( EventHandler <VisualTreeChangeEventArgs> visualTreeChanged, VisualTreeChangeEventArgs args, bool isPotentialOuterChange) { bool savedIsVisualTreeChangedInProgress = s_IsVisualTreeChangedInProgress; HwndSource savedActiveHwndSource = s_ActiveHwndSource; try { s_IsVisualTreeChangedInProgress = true; if (isPotentialOuterChange) { s_ActiveHwndSource = PresentationSource.FromDependencyObject(args.Parent) as System.Windows.Interop.HwndSource; } visualTreeChanged(null, args); } finally { s_IsVisualTreeChangedInProgress = savedIsVisualTreeChangedInProgress; s_ActiveHwndSource = savedActiveHwndSource; } }
public static Form FindForm(this DependencyObject element) { if (element == null) { return(null); } var source = PresentationSource.FromDependencyObject(element) as HwndSource; if (source == null) { return(null); } var host = Control.FromChildHandle(source.Handle) as ElementHost; if (host == null) { return(null); } var form = host.TopLevelControl as Form; return(form); }
void ICefWebBrowserInternal.OnPreviewKeyEvent(CefKeyEvent keyEvent) { this.DispatchIfRequired(() => { if (keyEvent.EventType == CefKeyEventType.RawKeyDown) { var keys = (Keys)keyEvent.WindowsKeyCode; if ((keyEvent.Modifiers & CefEventFlags.ControlDown) == CefEventFlags.ControlDown) { keys |= Keys.Control; } if ((keyEvent.Modifiers & CefEventFlags.ShiftDown) == CefEventFlags.ShiftDown) { keys |= Keys.Shift; } if ((keyEvent.Modifiers & CefEventFlags.AltDown) == CefEventFlags.AltDown) { keys |= Keys.Alt; } var source = PresentationSource.FromDependencyObject(this); var key = KeyInterop.KeyFromVirtualKey((int)keys); var args = new System.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice, source, 0, key) { RoutedEvent = PreviewKeyDownEvent }; RaiseEvent(args); } }, true); }
private static void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs args) { if (!IsCurrentThreadMainUiThread()) { return; } if (!(sender is DependencyObject dependencyObject) || !IsRegisteredCommandFocusElement(dependencyObject)) { return; } if (_restoreFocusScope == null && IsCommandContainerGainingFocus(args.OldFocus, args.NewFocus)) { if (!IsAttachedCommandFocusElement(dependencyObject)) { CurrentMenuModeSource = PresentationSource.FromDependencyObject(dependencyObject); } _restoreFocusScope = new CommandRestoreFocusScope(args.OldFocus); } if (PresentationSource.FromDependencyObject(dependencyObject) != null) { return; } args.Handled = true; }
private static void OnKeyboardInputProviderAcquireFocus(object sender, KeyboardInputProviderAcquireFocusEventArgs e) { if (!IsCurrentThreadMainUiThread()) { return; } if (!(sender is DependencyObject dependencyObject) || !IsRegisteredCommandFocusElement(dependencyObject)) { return; } if (dependencyObject is IInputElement inputElement && !inputElement.IsKeyboardFocusWithin) { if (e.RoutedEvent == Keyboard.PreviewKeyboardInputProviderAcquireFocusEvent) { if (!IsAttachedCommandFocusElement(dependencyObject)) { CurrentMenuModeSource = PresentationSource.FromDependencyObject(dependencyObject); } } else if (!e.FocusAcquired) { CurrentMenuModeSource = null; } } if (PresentationSource.FromDependencyObject(dependencyObject) != null) { return; } e.Handled = true; }
public void UpdateDisplayInfo(DependencyObject @object) { var source = PresentationSource.FromDependencyObject(@object).CompositionTarget.TransformToDevice; DPIX = source.M11; DPIY = source.M22; }
public static void CreateNativeWindow(this Control owner, Action <HwndSource> showNativeDialog, Point location) { var screenLoc = owner.PointToScreen(location); var wnd = new Window() { WindowStyle = WindowStyle.None, ShowInTaskbar = false, ResizeMode = ResizeMode.NoResize, Left = screenLoc.X, Top = screenLoc.Y, Width = 0, Height = 0, Owner = Window.GetWindow(owner), }; wnd.SourceInitialized += (s, e) => { showNativeDialog((HwndSource)PresentationSource.FromDependencyObject(wnd)); wnd.Close(); }; wnd.ShowDialog(); wnd.Close(); }
public ClipboardExtensions(object owner) { _host = WpfHost.Current; // This class may be accessed before the Window is loaded // if the Clipboard is somehow accessed really early. if (_host.IsLoaded) { HostLoaded(null, null); } else { // Hook for native events _host.Loaded += HostLoaded; } void HostLoaded(object sender, RoutedEventArgs e) { _host.Loaded -= HostLoaded; var win = Window.GetWindow(_host); var fromDependencyObject = PresentationSource.FromDependencyObject(win); _hwndSource = fromDependencyObject as HwndSource; if (_pendingStartContentChanged) { StartContentChanged(); } } }
// For compat, allow nested changes when: // a. The outer change added a child to an element that is a visual // root of a window. (More precisely - the element has no parent, // but does have an HwndSource.) // b. The inner change affects an element belonging to a different // PresentationSource. // // Handlers for VisualTreeChanged should not cause side-effects - that // creates a situation where the app can behave differently during // diagnosis than it does in production. But some already-shipped // diagnostic assistants cause side-effects in the situation above. // [VS 2015 and 2017 create a new window to hold the "little box" // containing the diagnostic icons/buttons. They should have done this // asynchronously, outside the scope of VisualTreeChanged, but they // already shipped with this flaw.] // private static bool IsChangePermitted(DependencyObject d) { // if the outer change was type (a), OnVisualChildChanged saved // the presentation source in s_ActiveHwndSource return((s_ActiveHwndSource != null) && (d != null) && (s_ActiveHwndSource != PresentationSource.FromDependencyObject(d))); }
/// <summary>Initializes the specified root.</summary> /// <param name="root">The root.</param> public void Initialize(DependencyObject root) { if (root is FrameworkElement frameworkElement) { frameworkElement.DataContextChanged += this.OnFrameworkElementDataContextChanged; frameworkElement.Unloaded += this.OnFrameworkElementUnloaded; } #if WPF if (root is FrameworkContentElement frameworkContentElement) { frameworkContentElement.DataContextChanged += this.OnFrameworkElementDataContextChanged; frameworkContentElement.Unloaded += this.OnFrameworkElementUnloaded; } #endif if (root.Dispatcher != null) { root.Dispatcher.ShutdownFinished += this.OnDispatcherShutdownFinished; } if (PresentationSource.FromDependencyObject(root) is HwndSource hwndSource) { hwndSource.Disposed += this.OnHwndSourceDisposed; } }
public WpfUIElementPointersSupport(object owner) { _ownerEvents = (ICoreWindowEvents)owner; _host = WpfHost.Current; _host.MouseEnter += HostOnMouseEnter; _host.MouseLeave += HostOnMouseLeave; _host.MouseMove += HostOnMouseMove; _host.MouseDown += HostOnMouseDown; _host.MouseUp += HostOnMouseUp; // Hook for native events _host.Loaded += HookNative; void HookNative(object sender, RoutedEventArgs e) { _host.Loaded -= HookNative; var win = Window.GetWindow(_host); var fromDependencyObject = PresentationSource.FromDependencyObject(win); _hwndSource = fromDependencyObject as HwndSource; _hwndSource?.AddHook(OnWmMessage); } }
public static void RegisterCallback(Window window, Action <string> callback) { IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam, ref bool handled) { if (msg == WM_COPYDATA) { var data = Marshal.PtrToStructure <CopyData>(lparam); if (data.dwData == (IntPtr)MessageId) { var value = Marshal.PtrToStringUni(data.lpData); callback(value); handled = true; } } return(IntPtr.Zero); } var source = (HwndSource)PresentationSource.FromDependencyObject(window); if (source is null) { window.SourceInitialized += delegate { RegisterCallback(window, callback); }; return; } source.AddHook(WndProc); }
private void RemoveCloseReasonHook() { if (PresentationSource.FromDependencyObject(this) is HwndSource source) { source.RemoveHook(WindowProc); } }
private void TreeViewItem_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) { var treeViewItem = (TreeViewItem)sender; var treeView = (TreeView)typeof(TreeViewItem).GetProperty("ParentTreeView", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(treeViewItem); if (PresentationSource.FromDependencyObject(treeViewItem) == null) { // the item might have disconnected by the time we run this return; } Point topLeftInTreeViewCoordinates = treeViewItem.TransformToAncestor(treeView).Transform(new Point(0, 0)); var treeViewItemTop = topLeftInTreeViewCoordinates.Y; if (treeViewItemTop < 0 || treeViewItemTop + treeViewItem.ActualHeight > scrollViewer.ViewportHeight || treeViewItem.ActualHeight > scrollViewer.ViewportHeight) { // if the item is not visible or too "tall", don't do anything; let them scroll it into view return; } // if the item is already fully within the viewport vertically, disallow horizontal scrolling e.Handled = true; }
/// <summary> /// Enter Full Screen Mode /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MenuButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (!maximized) { fullScreenWindow = new Window(); this.Content = new TextBlock { Text = "Control Content Maximized" }; fullScreenWindow.Title = "Virtual Earth"; fullScreenWindow.Loaded += delegate { HwndSource source = (HwndSource)PresentationSource.FromDependencyObject(fullScreenWindow); source.AddHook(WindowProc); }; fullScreenWindow.Content = mainGrid; fullScreenWindow.WindowStyle = WindowStyle.None; fullScreenWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen; fullScreenWindow.WindowState = WindowState.Maximized; fullScreenWindow.Closing += new System.ComponentModel.CancelEventHandler(w_Closing); maximized = true; fullScreenWindow.Show(); } else { fullScreenWindow.Content = null; fullScreenWindow.Close(); this.Content = mainGrid; maximized = false; } }
public static FrameworkElement GetRootVisual([NotNull] this DependencyObject item) { Contract.Requires(item != null); Contract.Ensures(Contract.Result <FrameworkElement>() != null); var hwndSource = (HwndSource)PresentationSource.FromDependencyObject(item); if (hwndSource == null) { throw new ArgumentException(@"Item not part of a valid visual tree.", nameof(item)); } var compositionTarget = hwndSource.CompositionTarget; if (compositionTarget == null) { throw new ArgumentException(@"Item not part of a valid visual tree.", nameof(item)); } var rootVisual = (FrameworkElement)compositionTarget.RootVisual; if (rootVisual == null) { throw new ArgumentException(@"Item not part of a valid visual tree.", nameof(item)); } return(rootVisual); }
protected virtual void OnLoaded(object sender, RoutedEventArgs e) { this.Loaded -= new RoutedEventHandler(OnLoaded); _hwndSrc = PresentationSource.FromDependencyObject(this) as HwndSource; _hwndSrcHook = new HwndSourceHook(FilterMessage); _hwndSrc.AddHook(_hwndSrcHook); }
private static bool IsValidRestoreTarget(HwndSource source, IInputElement element) { if (element is DependencyObject dependencyObject) { return(PresentationSource.FromDependencyObject(dependencyObject) == source); } return(false); }
private void FocusDefaultItem() { if (_lastFocusedItem?.Target is IInputElement inputElement && (!(inputElement is DependencyObject dependencyObject) || PresentationSource.FromDependencyObject(dependencyObject) != null)) { inputElement.Focus(); }
/// <summary> /// Gets the window handle of the HwndSource hosting this item. /// </summary> /// <param name="self">The item.</param> /// <returns>The window handle, if the item is part of a valid visual tree, otherwise IntPtr.Zero.</returns> public static IntPtr GetWindowHandle([NotNull] this DependencyObject self) { Contract.Requires(self != null); var hwndSource = PresentationSource.FromDependencyObject(self) as HwndSource; return(hwndSource?.Handle ?? IntPtr.Zero); }
static Rect GetRealRect(FrameworkElement element) { if (element == null || !element.IsLoaded || PresentationSource.FromDependencyObject(element) == null) { return(Rect.Empty); } return(LayoutHelper.GetScreenRect(element)); }
public WindowCloseReason(Window window) { HwndSource source = (HwndSource)PresentationSource.FromDependencyObject(window); source.AddHook(RetornaCloseReason); CloseReason = CloseReason.None; }
public static FrameworkElement TryGetRootVisual([NotNull] this DependencyObject item) { var hwndSource = (HwndSource)PresentationSource.FromDependencyObject(item); var compositionTarget = hwndSource?.CompositionTarget; var rootVisual = (FrameworkElement)compositionTarget?.RootVisual; return(rootVisual); }
private void OnLoaded(object sender, RoutedEventArgs e) { Loaded -= OnLoaded; OnThemeChanged(null, null); this.SetParentToMainWindowOf(Model.Root.Manager); _hwndSrc = PresentationSource.FromDependencyObject(this) as HwndSource; _hwndSrcHook = FilterMessage; _hwndSrc?.AddHook(_hwndSrcHook); }
public CloseReasonWindow() : base() { this.CloseReason = ECloseReason.NotClosed; Loaded += delegate { HwndSource source = (HwndSource)PresentationSource.FromDependencyObject(this); source.AddHook(WindowProc); }; }
private void UpdateWindow() { var hwnd = ((HwndSource)PresentationSource.FromDependencyObject(this)).Handle; RECT rect; if (GetWindowRect(hwnd, out rect)) { SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top, 0); } }
public Player(PlayerWindowType type) { InitializeComponent(); PlayerViewModel = new PlayerViewModel(type); DataContext = PlayerViewModel; SizeChanged += Player_SizeChanged; ///获取控件Handel HwndSource hs = (HwndSource)PresentationSource.FromDependencyObject(mediaElement); }
/// <summary> /// /// </summary> /// <param name="visual"></param> /// <returns></returns> public static IntPtr GetVisualHandle(DependencyObject visual) { IntPtr hwnd = IntPtr.Zero; var source = PresentationSource.FromDependencyObject(visual); if (source != null) { hwnd = ((HwndSource)source).Handle; } return(hwnd); }
private ForegroundSession(UIElement element, Action onLeave) { _element = element; _onLeave = onLeave; AttachLayoutEvent(); HwndSource = PresentationSource.FromDependencyObject(_element) as HwndSource; CallWndProcHookService.Instance.AddListener(this); }
private static IntPtr?GetObjectParentHandle([NotNull] DependencyObject depObj) { if (depObj == null) { throw new ArgumentNullException(nameof(depObj)); } var presentationSource = PresentationSource.FromDependencyObject(depObj) as HwndSource; return(presentationSource?.Handle); }