private void InitializeThumbnail() { if (IntPtr.Zero != _thumb) { // release the old thumbnail ReleaseThumbnail(); } if (IntPtr.Zero == Handle) { return; } // find our parent hwnd _target = (HwndSource)HwndSource.FromVisual(this); // if we have one, we can attempt to register the thumbnail if (_target == null || 0 != DwmUtils.DwmRegisterThumbnail(_target.Handle, Handle, out this._thumb)) { return; } DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = false; props.SourceClientAreaOnly = false; props.Opacity = (byte)(255 * this.Opacity); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.SourceClientAreaOnly | ThumbnailFlags.Opacity; DwmUtils.DwmUpdateThumbnailProperties(_thumb, ref props); }
private void LoadWindows(Dictionary <Guid, Desktop> desktops) { Dictionary <IntPtr, string> windows = DwmUtils.LoadWindows(); foreach (KeyValuePair <IntPtr, string> entry in windows) { var vDesktop = VirtualDesktop.FromHwnd(entry.Key); if (vDesktop == null) { continue; // vDesktop = VirtualDesktop.Current; // show in current desktop as fallback ?? } Desktop desktop; if (!desktops.TryGetValue(vDesktop.Id, out desktop)) { // this desktop is not in out desktops dict, how can a window be not in the dict of all desktops?? MessageBox.Show(this, "Found window in non-existent desktop!"); return; } var windowItem = new WindowItem(entry.Key, entry.Value); desktop.windows.Add(windowItem); } foreach (var desktop in desktops) { List <WindowItem> desktopWindows = desktop.Value.windows; Grid grid = CreateGrid(desktopWindows.Count); ShowWindowsInDesktop(desktopWindows, grid, desktop.Value); } }
private void InitialiseThumbnail(IntPtr source) { if (IntPtr.Zero != thumb) { // release the old thumbnail ReleaseThumbnail(); } if (IntPtr.Zero != source) { // find our parent hwnd target = (HwndSource)HwndSource.FromVisual(this); // if we have one, we can attempt to register the thumbnail if (target != null && 0 == DwmUtils.DwmRegisterThumbnail(target.Handle, source, out this.thumb)) { DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = false; props.SourceClientAreaOnly = this.ClientAreaOnly; props.Opacity = (byte)(255 * this.Opacity); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.SourceClientAreaOnly | ThumbnailFlags.Opacity; DwmUtils.DwmUpdateThumbnailProperties(thumb, ref props); } } }
// this is where the magic happens private void Thumbnail_LayoutUpdated(object sender, EventArgs e) { if (IntPtr.Zero == thumb) { InitialiseThumbnail(this.Source); } if (IntPtr.Zero != thumb) { if (!target.RootVisual.IsAncestorOf(this)) { //we are no longer in the visual tree ReleaseThumbnail(); return; } GeneralTransform transform = TransformToAncestor(target.RootVisual); Point a = transform.Transform(new Point(0, 0)); Point b = transform.Transform(new Point(this.ActualWidth, this.ActualHeight)); DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = true; props.Destination = new Rect( (int)Math.Ceiling(a.X), (int)Math.Ceiling(a.Y), (int)Math.Ceiling(b.X), (int)Math.Ceiling(b.Y)); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.RectDetination; DwmUtils.DwmUpdateThumbnailProperties(thumb, ref props); } }
public void DrawRectForWindow() { if (IntPtr.Zero == _thumb) { InitializeThumbnail(); } if (IntPtr.Zero == _thumb) { return; } if (!_target.RootVisual.IsAncestorOf(this)) { //we are no longer in the visual tree ReleaseThumbnail(); return; } double scale = DwmUtils.GetSystemScale(); // keep original window aspect ratio double windowWidth = WindowRect.Right - WindowRect.Left; double windowHeight = WindowRect.Bottom - WindowRect.Top; Point origin = new Point(Canvas.GetLeft(this), Canvas.GetTop(this)); double scaleFactor = Height / windowHeight; windowHeight = scaleFactor * windowHeight; windowWidth = scaleFactor * windowWidth; // origins remain the same as provided var rect = new Rect( (int)(origin.X), (int)(origin.Y), (int)(origin.X + windowWidth), (int)(origin.Y + windowHeight)); var scaledRect = new Rect( (int)(origin.X * scale), (int)(origin.Y * scale), (int)((origin.X + windowWidth) * scale), (int)((origin.Y + windowHeight) * scale)); DwmThumbnailProperties props = new DwmThumbnailProperties(); props.Visible = true; props.Destination = scaledRect; // props.Destination = new DWM.Rect( // (int) Math.Ceiling(a.X), (int) Math.Ceiling(a.Y), // (int) Math.Ceiling(b.X), (int) Math.Ceiling(b.Y)); props.Flags = ThumbnailFlags.Visible | ThumbnailFlags.RectDetination; DwmUtils.DwmUpdateThumbnailProperties(_thumb, ref props); }
private void UpdateThumbnail() { if (IntPtr.Zero != thumb) { DwmThumbnailProperties props = new DwmThumbnailProperties(); props.SourceClientAreaOnly = this.ClientAreaOnly; props.Opacity = (byte)(255 * this.Opacity); props.Flags = ThumbnailFlags.SourceClientAreaOnly | ThumbnailFlags.Opacity; DwmUtils.DwmUpdateThumbnailProperties(thumb, ref props); } }
protected override Size ArrangeOverride(Size finalSize) { Psize size; DwmUtils.DwmQueryThumbnailSourceSize(this.thumb, out size); // scale to fit whatever size we were allocated double scale = finalSize.Width / size.Width; scale = Math.Min(scale, finalSize.Height / size.Height); return(new Size(size.Width * scale, size.Height * scale)); }
public WindowItem(IntPtr handle, string title) { this.Handle = handle; this.Title = title; BorderBrush = Brushes.OrangeRed; BorderThickness = new Thickness(2); // Using this makes clicking work inside the box Background = new SolidColorBrush(Colors.Blue); Background.Opacity = 1; DwmUtils.GetWindowRect(handle, out WindowRect); // clicking only works inside the WindowItem bounds this.MouseLeftButtonUp += (sender, args) => { DwmUtils.SetForegroundWindow(handle); }; // this.LayoutUpdated += new EventHandler(OnLayoutUpdated); // this.Unloaded += new RoutedEventHandler(OnUnloaded); }
protected override Size MeasureOverride(Size availableSize) { Psize size; DwmUtils.DwmQueryThumbnailSourceSize(this.thumb, out size); double scale = 1; // our preferred size is the thumbnail source size // if less space is available, we scale appropriately if (size.Width > availableSize.Width) { scale = availableSize.Width / size.Width; } if (size.Height > availableSize.Height) { scale = Math.Min(scale, availableSize.Height / size.Height); } return(new Size(size.Width * scale, size.Height * scale)); ; }
private void ReleaseThumbnail() { DwmUtils.DwmUnregisterThumbnail(_thumb); this._thumb = IntPtr.Zero; this._target = null; }