Exemple #1
0
        private static void RemoveWndProcUsage(HwndHost hwndHost)
        {
            int refCount = (int)hwndHost.GetValue(WindowHookRefCountProperty);

            refCount--;
            hwndHost.SetValue(WindowHookRefCountProperty, refCount);

            if (refCount == 0)
            {
                HwndHostExtensionsWindowHook hook = (HwndHostExtensionsWindowHook)hwndHost.GetValue(WindowHookProperty);
                hook.Dispose();
                hwndHost.ClearValue(WindowHookProperty);
            }
        }
Exemple #2
0
 internal override InteropAutomationProvider GetInteropChild()
 {
     if (this._interopProvider == null)
     {
         HostedWindowWrapper wrapper  = null;
         HwndHost            hwndHost = (HwndHost)base.Owner;
         IntPtr criticalHandle        = hwndHost.CriticalHandle;
         if (criticalHandle != IntPtr.Zero)
         {
             wrapper = HostedWindowWrapper.CreateInternal(criticalHandle);
         }
         this._interopProvider = new InteropAutomationProvider(wrapper, this);
     }
     return(this._interopProvider);
 }
        /// <summary>
        /// Grabs a snapshot of a WPF UIElement and returns the image as Bitmap.
        /// </summary>
        /// <param name="element">Represents the element to take the snapshot from.</param>
        /// <param name="dpiX">Represents the X DPI value used to capture this snapshot.</param>
        /// <param name="dpiY">Represents the Y DPI value used to capture this snapshot.</param>
        /// <param name="width">The requested bitmap width.</param>
        /// <param name="height">The requested bitmap height.</param>
        /// <returns>Returns the bitmap (PNG format).</returns>
        public static Bitmap GrabWindowBitmap(UIElement element, int dpiX, int dpiY, int width, int height)
        {
            // Special case for HwndHost controls
            HwndHost host = element as HwndHost;

            if (host != null)
            {
                IntPtr handle = host.Handle;
                return(GrabWindowBitmap(handle, new System.Drawing.Size(width, height)));
            }

            Rect bounds = VisualTreeHelper.GetDescendantBounds(element);

            // create the renderer.
            if (bounds.Height == 0 || bounds.Width == 0)
            {
                return(null);    // 0 sized element. Probably hidden
            }

            RenderTargetBitmap rendertarget = new RenderTargetBitmap((int)(bounds.Width * dpiX / 96.0),
                                                                     (int)(bounds.Height * dpiY / 96.0), dpiX, dpiY, PixelFormats.Default);

            DrawingVisual dv = new DrawingVisual();

            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(element);
                ctx.DrawRectangle(vb, null, new Rect(new System.Windows.Point(), bounds.Size));
            }

            rendertarget.Render(dv);

            BitmapEncoder bmpe = new PngBitmapEncoder();

            bmpe.Frames.Add(BitmapFrame.Create(rendertarget));

            Bitmap bmp;

            // Create a MemoryStream with the image.
            using (MemoryStream fl = new MemoryStream())
            {
                bmpe.Save(fl);
                fl.Position = 0;
                bmp         = new Bitmap(fl);
            }

            return((Bitmap)bmp.GetThumbnailImage(width, height, null, IntPtr.Zero));
        }
Exemple #4
0
        private static void AddWndProcUsage(HwndHost hwndHost)
        {
            int refCount = (int)hwndHost.GetValue(WindowHookRefCountProperty);

            refCount++;
            hwndHost.SetValue(WindowHookRefCountProperty, refCount);

            if (refCount == 1)
            {
                if (!TryHookWndProc(hwndHost))
                {
                    // Try again later, when the HwndHost is loaded.
                    hwndHost.Loaded += (s, e) => TryHookWndProc((HwndHost)s);
                }
            }
        }
Exemple #5
0
        private static bool TryHookWndProc(HwndHost hwndHost)
        {
            if (hwndHost.Handle != IntPtr.Zero)
            {
                // Hook the window messages so we can intercept the
                // various messages.
                HwndHostExtensionsWindowHook hook = new HwndHostExtensionsWindowHook(hwndHost);

                // Keep our hook alive.
                hwndHost.SetValue(WindowHookProperty, hook);

                return(true);
            }

            return(false);
        }
Exemple #6
0
        private static void OnRaiseMouseActivateCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HwndHost hwndHost = d as HwndHost;

            if (hwndHost != null)
            {
                bool newValue = (bool)e.NewValue;
                if (newValue)
                {
                    AddWndProcUsage(hwndHost);
                }
                else
                {
                    RemoveWndProcUsage(hwndHost);
                }
            }
        }
Exemple #7
0
        private static void OnCopyBitsBehaviorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HwndHost hwndHost = d as HwndHost;

            if (hwndHost != null)
            {
                CopyBitsBehavior newValue = (CopyBitsBehavior)e.NewValue;
                if (newValue != CopyBitsBehavior.Default)
                {
                    AddWndProcUsage(hwndHost);
                }
                else
                {
                    RemoveWndProcUsage(hwndHost);
                }
            }
        }
Exemple #8
0
        private static void WindowFocusChanging(object sender, FocusChangeEventArgs e)
        {
            Func <HwndHost, bool> func = null;

            foreach (DockingManager _manager in FocusElementManager._managers)
            {
                IEnumerable <HwndHost> hwndHosts = _manager.FindLogicalChildren <HwndHost>();
                Func <HwndHost, bool>  func1     = func;
                if (func1 == null)
                {
                    Func <HwndHost, bool> func2 = (HwndHost hw) => Win32Helper.IsChild(hw.Handle, e.GotFocusWinHandle);
                    Func <HwndHost, bool> func3 = func2;
                    func  = func2;
                    func1 = func3;
                }
                HwndHost hwndHost = hwndHosts.FirstOrDefault <HwndHost>(func1);
                if (hwndHost == null)
                {
                    continue;
                }
                LayoutAnchorableControl gotFocusWinHandle = hwndHost.FindVisualAncestor <LayoutAnchorableControl>();
                if (gotFocusWinHandle == null)
                {
                    LayoutDocumentControl layoutDocumentControl = hwndHost.FindVisualAncestor <LayoutDocumentControl>();
                    if (layoutDocumentControl == null)
                    {
                        continue;
                    }
                    FocusElementManager._modelFocusedWindowHandle[layoutDocumentControl.Model] = e.GotFocusWinHandle;
                    if (layoutDocumentControl.Model == null)
                    {
                        continue;
                    }
                    layoutDocumentControl.Model.IsActive = true;
                }
                else
                {
                    FocusElementManager._modelFocusedWindowHandle[gotFocusWinHandle.Model] = e.GotFocusWinHandle;
                    if (gotFocusWinHandle.Model == null)
                    {
                        continue;
                    }
                    gotFocusWinHandle.Model.IsActive = true;
                }
            }
        }
Exemple #9
0
        override internal InteropAutomationProvider GetInteropChild()
        {
            if (_interopProvider == null)
            {
                HostedWindowWrapper wrapper = null;

                HwndHost host = (HwndHost)Owner;
                IntPtr   hwnd = host.CriticalHandle;

                if (hwnd != IntPtr.Zero)
                {
                    wrapper = HostedWindowWrapper.CreateInternal(hwnd);
                }

                _interopProvider = new InteropAutomationProvider(wrapper, this);
            }

            return(_interopProvider);
        }
Exemple #10
0
        public void AddBottom(IntPtr handle)
        {
            if (this.panel.InvokeRequired)
            {
                this.panel.BeginInvoke(new Action <IntPtr>(AddBottom), handle);
                return;
            }

            IRCProviders.Win32.RECT rect;
            IRCProviders.Win32.GetWindowRect(handle, out rect);

            var hostPanel = new HwndHost {
                Width = rect.right - rect.left, Height = rect.bottom - rect.top + 2, Dock = System.Windows.Forms.DockStyle.Bottom
            };

            hostPanel.Child = handle;

            index[handle] = hostPanel;
            this.panel.AddControl(hostPanel);
        }
Exemple #11
0
        public ICIRCeItem AddItem(ICIRCeItem parent, IntPtr handle, string title, IntPtr hIcon)
        {
            if (this.parent.InvokeRequired)
            {
                return((ICIRCeItem)this.parent.EndInvoke(this.parent.BeginInvoke(new Func <ICIRCeItem, IntPtr, string, IntPtr, ICIRCeItem>(AddItem), parent, handle, title, hIcon)));
            }

            var parentItem = parent as CIRCeItem;

            if (parentItem == null)
            {
                return(null);
            }

            IRCProviders.Win32.RECT rect;
            IRCProviders.Win32.GetWindowRect(handle, out rect);

            var icon = hIcon != IntPtr.Zero ? Icon.FromHandle(hIcon) : null;

            var hostForm = new IRCProviders.IRCForm {
                Icon = icon, Text = title, Width = rect.right - rect.left, Height = rect.bottom - rect.top + 2
            };

            var hostPanel = new HwndHost {
                Dock = System.Windows.Forms.DockStyle.Fill
            };

            hostPanel.Child = handle;

            hostForm.Controls.Add(hostPanel);

            this.parent.RegisterAsMDIChild(parentItem.Form, hostForm, null);

            if (icon != null)
            {
                IRCProviders.Win32.DestroyIcon(icon.Handle);
            }

            return(new CIRCeItem(hostForm));
        }
Exemple #12
0
 /// <summary>
 ///     Attached property getter for the CopyBitsBehavior property.
 /// </summary>
 public static CopyBitsBehavior GetCopyBitsBehavior(this HwndHost @this)
 {
     return((CopyBitsBehavior)@this.GetValue(CopyBitsBehaviorProperty));
 }
Exemple #13
0
 public HwndHostExtensionsWindowHook(HwndHost hwndHost)
     : base(new HWND(hwndHost.Handle))
 {
     _hwndHost = hwndHost;
 }
 public static IObservable <EventPattern <EventArgs> > LayoutUpdatedObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <EventHandler, EventArgs>(h => This.LayoutUpdated += h, h => This.LayoutUpdated -= h));
 }
 public static IObservable <EventPattern <RoutedEventArgs> > LostFocusObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <RoutedEventHandler, RoutedEventArgs>(h => This.LostFocus += h, h => This.LostFocus -= h));
 }
 public static IObservable <EventPattern <ManipulationBoundaryFeedbackEventArgs> > ManipulationBoundaryFeedbackObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <EventHandler <ManipulationBoundaryFeedbackEventArgs>, ManipulationBoundaryFeedbackEventArgs>(h => This.ManipulationBoundaryFeedback += h, h => This.ManipulationBoundaryFeedback -= h));
 }
Exemple #17
0
 ///
 public HwndHostAutomationPeer(HwndHost owner) : base(owner)
 {
     IsInteropPeer = true;
 }
 public static IObservable <EventPattern <EventArgs> > InitializedObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <EventHandler, EventArgs>(h => This.Initialized += h, h => This.Initialized -= h));
 }
 public static IObservable <EventPattern <RoutedEventArgs> > UnloadedObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <RoutedEventHandler, RoutedEventArgs>(h => This.Unloaded += h, h => This.Unloaded -= h));
 }
 public static IObservable <EventPattern <MouseButtonEventArgs> > MouseUpObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <MouseButtonEventHandler, MouseButtonEventArgs>(h => This.MouseUp += h, h => This.MouseUp -= h));
 }
 public static IObservable <EventPattern <ContextMenuEventArgs> > ContextMenuClosingObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <ContextMenuEventHandler, ContextMenuEventArgs>(h => This.ContextMenuClosing += h, h => This.ContextMenuClosing -= h));
 }
 public static IObservable <EventPattern <ToolTipEventArgs> > ToolTipClosingObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <ToolTipEventHandler, ToolTipEventArgs>(h => This.ToolTipClosing += h, h => This.ToolTipClosing -= h));
 }
 public static IObservable <EventPattern <ManipulationCompletedEventArgs> > ManipulationCompletedObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <EventHandler <ManipulationCompletedEventArgs>, ManipulationCompletedEventArgs>(h => This.ManipulationCompleted += h, h => This.ManipulationCompleted -= h));
 }
Exemple #24
0
 /// <summary>
 ///     Attached property setter for the CopyBitsBehavior property.
 /// </summary>
 public static void SetCopyBitsBehavior(this HwndHost @this, CopyBitsBehavior value)
 {
     @this.SetValue(CopyBitsBehaviorProperty, value);
 }
 public static IObservable <EventPattern <TouchEventArgs> > TouchLeaveObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <EventHandler <TouchEventArgs>, TouchEventArgs>(h => This.TouchLeave += h, h => This.TouchLeave -= h));
 }
Exemple #26
0
 /// <summary>
 ///     Attached property getter for the RaiseMouseActivateCommand property.
 /// </summary>
 public static bool GetRaiseMouseActivateCommand(this HwndHost @this)
 {
     return((bool)@this.GetValue(RaiseMouseActivateCommandProperty));
 }
Exemple #27
0
 /// <summary>
 ///     Attached property setter for the RaiseMouseActivateCommand property.
 /// </summary>
 public static void SetRaiseMouseActivateCommand(this HwndHost @this, bool value)
 {
     @this.SetValue(RaiseMouseActivateCommandProperty, value);
 }
 public static IObservable <EventPattern <DependencyPropertyChangedEventArgs> > FocusableChangedObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <DependencyPropertyChangedEventHandler, DependencyPropertyChangedEventArgs>(h => This.FocusableChanged += h, h => This.FocusableChanged -= h));
 }
 public static IObservable <EventPattern <MouseButtonEventArgs> > PreviewMouseLeftButtonDownObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <MouseButtonEventHandler, MouseButtonEventArgs>(h => This.PreviewMouseLeftButtonDown += h, h => This.PreviewMouseLeftButtonDown -= h));
 }
 public static IObservable <EventPattern <ManipulationInertiaStartingEventArgs> > ManipulationInertiaStartingObserver(this HwndHost This)
 {
     return(Observable.FromEventPattern <EventHandler <ManipulationInertiaStartingEventArgs>, ManipulationInertiaStartingEventArgs>(h => This.ManipulationInertiaStarting += h, h => This.ManipulationInertiaStarting -= h));
 }