private void Form1_Load(object sender, EventArgs e) { if (Settings.Default.Top) { this.TopMost = true; topToolStripMenuItem.Checked = true; } this.Width = Settings.Default.Width; this.Height = Settings.Default.Height; dwm_THUMBNAIL_PROPERTIES = new DWM_THUMBNAIL_PROPERTIES { Opacity = byte.MaxValue, Visible = true, SourceClientAreaOnly = false, }; if (Settings.Default.Landscape) { x9ToolStripMenuItem.Checked = true; dwm_THUMBNAIL_PROPERTIES.Destination = GetLandscapeRect(); } else { dwm_THUMBNAIL_PROPERTIES.Destination = new RECT(0, 0, this.Width - offsetWidth, this.Height - offsetHeight); } }
private void UpdateThumb() { if (thumb != IntPtr.Zero) { PSIZE size; DwmQueryThumbnailSourceSize(thumb, out size); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.fVisible = true; props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY; props.opacity = (byte)255;//opacity.Value; props.rcDestination = new Rect(image.Left, image.Top, image.Right, image.Bottom); if (size.x < image.Width) { props.rcDestination.Right = props.rcDestination.Left + size.x; } if (size.y < image.Height) { props.rcDestination.Bottom = props.rcDestination.Top + size.y; } DwmUpdateThumbnailProperties(thumb, ref props); } }
public static void UpdateThumb() { if (thumb == IntPtr.Zero) { return; } PSIZE size; DwmQueryThumbnailSourceSize(thumb, out size); int testX; if (DebugForm.WindowRect.X > DebugForm.WindowRect.Width) { testX = DebugForm.WindowRect.X - DebugForm.WindowRect.Width; } else { testX = DebugForm.WindowRect.Width - DebugForm.WindowRect.X; } var testY = DebugForm.WindowRect.Height - DebugForm.WindowRect.Y; DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES { fVisible = true, dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY, opacity = 255, rcDestination = new Rectangle(0, 0, testX, testY) }; DwmUpdateThumbnailProperties(thumb, ref props); }
private void UpdateThumb() { if (_thumb != IntPtr.Zero) { DwmQueryThumbnailSourceSize(_thumb, out var size); var top = splitLeft.Top + splitLeft.Panel2.Top; var bottom = splitLeft.Top + picBox.Bottom; var right = splitLeft.Panel2.Right; var props = new DWM_THUMBNAIL_PROPERTIES { fVisible = true, dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY, opacity = 255, rcDestination = new Rect(splitLeft.Panel2.Left, top, right, bottom) }; if (size.x < picBox.Width) { props.rcDestination.Right = props.rcDestination.Left + size.x; } if (size.y < picBox.Height) { props.rcDestination.Bottom = props.rcDestination.Top + size.y; } DwmUpdateThumbnailProperties(_thumb, ref props); } }
private void ConnectWindowToXboxApp() { if (_thumbnailId != IntPtr.Zero) { DwmApi.DwmUnregisterThumbnail(_thumbnailId); _thumbnailId = IntPtr.Zero; } var xboxAppHwnd = User32.FindWindow("ApplicationFrameWindow", "Xbox"); var handle = DwmApi.DwmRegisterThumbnail(new WindowInteropHelper(this).Handle, xboxAppHwnd); var props = new DWM_THUMBNAIL_PROPERTIES { dwFlags = DWM_TNP.DWM_TNP_VISIBLE | DWM_TNP.DWM_TNP_RECTSOURCE | DWM_TNP.DWM_TNP_RECTDESTINATION, fVisible = true }; var captionHeight = Metrics.Frame.Height - (int)LayoutRoot.ActualHeight; props.rcSource = new RECT(0, captionHeight, Metrics.Frame.Width, Metrics.Frame.Height); props.rcDestination = new RECT(0, 0, Metrics.Frame.Width, (int)LayoutRoot.ActualHeight); DwmUpdateThumbnailProperties(handle, props); DwmQueryThumbnailSourceSize(handle, out var sz); Trace.WriteLine($"DwmRegisterThumbnail: {handle} {sz}"); }
public void BackgroundUpdate(IntPtr dest, int width, int height) { if (dest == IntPtr.Zero) { return; } DWMApi.DwmQueryThumbnailSourceSize(dest, out PSIZE size); var props = new DWM_THUMBNAIL_PROPERTIES { fVisible = true, dwFlags = DWMApi.DWM_TNP_VISIBLE | DWMApi.DWM_TNP_RECTDESTINATION | DWMApi.DWM_TNP_OPACITY, opacity = 255, rcDestination = new RECT(0, 0, width, height), }; if (size.x < width) { props.rcDestination.Right = props.rcDestination.Left + size.x; } if (size.y < height) { props.rcDestination.Bottom = props.rcDestination.Top + size.y; } DWMApi.DwmUpdateThumbnailProperties(dest, ref props); }
//DWM method to get the thumbnail from target application private IntPtr getSource(Process proc, PictureBox pBox) { RECT rc; IntPtr hc; GetWindowRect(proc.MainWindowHandle, out rc); DwmRegisterThumbnail(this.Handle, proc.MainWindowHandle, out hc); //Connect target window with this window DwmQueryThumbnailSourceSize(hc, out PSIZE size); //setting properties for thumbnail DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.dwFlags = DWMApi.DWM_TNP_VISIBLE | DWMApi.DWM_TNP_RECTDESTINATION | DWMApi.DWM_TNP_OPACITY; props.opacity = 255; props.fVisible = true; props.rcDestination = new Rect(pBox.Left, pBox.Top, pBox.Right, pBox.Bottom); if (size.x < pBox.Width) { props.rcDestination.Right = props.rcDestination.Left + size.x; } if (size.y < pBox.Height) { props.rcDestination.Bottom = props.rcDestination.Top + size.y; } DwmUpdateThumbnailProperties(hc, ref props); return(hc); }
private void UpdateThumb() { if (thumb != IntPtr.Zero) { PSIZE size; DwmQueryThumbnailSourceSize(thumb, out size); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.fVisible = true; props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY | DWM_TNP_SOURCECLIENTAREAONLY; props.opacity = 255; props.fSourceClientAreaOnly = true; Rect thumbRect = new Rect(targetControl.Left, targetControl.Top, targetControl.Right, targetControl.Bottom); Control parent = targetControl.Parent; while (parent != null && parent != mainWindow) { thumbRect.Left += parent.Left; thumbRect.Top += parent.Top; thumbRect.Right += parent.Left; thumbRect.Bottom += parent.Top; parent = parent.Parent; } props.rcDestination = thumbRect; DwmUpdateThumbnailProperties(thumb, ref props); } }
public void Register(IntPtr destination, IntPtr source) { this._properties = new DWM_THUMBNAIL_PROPERTIES(); this._properties.dwFlags = DWM_TNP_CONSTANTS.DWM_TNP_VISIBLE + DWM_TNP_CONSTANTS.DWM_TNP_OPACITY + DWM_TNP_CONSTANTS.DWM_TNP_RECTDESTINATION + DWM_TNP_CONSTANTS.DWM_TNP_SOURCECLIENTAREAONLY; this._properties.opacity = 255; this._properties.fVisible = true; this._properties.fSourceClientAreaOnly = true; if (!this._windowManager.IsCompositionEnabled) { return; } try { this._handle = DwmNativeMethods.DwmRegisterThumbnail(destination, source); } catch (ArgumentException) { // This exception is raised if the source client is already closed // Can happen on a really slow CPU's that the window is still being // listed in the process list yet it already cannot be used as // a thumbnail source this._handle = IntPtr.Zero; } catch (COMException) { // This exception is raised if DWM is suddenly not available // (f.e. when switching between Windows user accounts) this._handle = IntPtr.Zero; } }
private void Render(int index, int left, int top, int height, int width, RECT?rect = null) { if (Thumbnails[index].Handle == IntPtr.Zero) { return; } var props = new DWM_THUMBNAIL_PROPERTIES { fVisible = true, dwFlags = (int)(DWM_TNP.DWM_TNP_VISIBLE | DWM_TNP.DWM_TNP_OPACITY | DWM_TNP.DWM_TNP_RECTDESTINATION | DWM_TNP.DWM_TNP_SOURCECLIENTAREAONLY), opacity = (byte)(255 / (index == 0 ? 2 : 1)), rcDestination = new RECT { left = left, top = top, right = left + width, bottom = top + height }, fSourceClientAreaOnly = true }; if (rect != null && !rect.Value.IsEmpty()) { props.rcSource = rect.Value; props.dwFlags |= (int)DWM_TNP.DWM_TNP_RECTSOURCE; } NativeMethods.DwmUpdateThumbnailProperties(Thumbnails[index].Handle, ref props); }
// 更新缩略图 private void UpdateThumb() { // 解除注册缩略图 foreach (var thumb in thumbs) { DwmUnregisterThumbnail(thumb); } thumbs.Clear(); // 重新注册缩略图 for (var i = 0; i < btns.Count; ++i) { IntPtr thumb; DwmRegisterThumbnail(new System.Windows.Interop.WindowInteropHelper(this).Handle, windows[i].Handle, out thumb); if (thumb != IntPtr.Zero) { PSIZE size; DwmQueryThumbnailSourceSize(thumb, out size); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY; props.fVisible = true; props.opacity = 255; System.Windows.Point point = btns[i].TranslatePoint(new System.Windows.Point(0, 0), this); int w = 230; int h = 130; int margin = 5; props.rcDestination = new Rect((int)point.X + margin, (int)point.Y + margin, (int)point.X + margin + w, (int)point.Y + margin + h); // 超出可视范围处理 int topLimit = 87; int bottomLimit = (int)this.Height - 175 + 45; if (point.Y < topLimit) { props.rcDestination.Top = topLimit; props.rcSource.Top = 500; props.rcSource.Bottom = size.y; props.rcSource.Left = 0; props.rcSource.Right = size.x; } else if (point.Y + h > bottomLimit) { props.rcDestination.Bottom = bottomLimit; } // 按照设置属性更新缩略图 DwmUpdateThumbnailProperties(thumb, ref props); // 记录此缩略图,方便下次解除注册 thumbs.Add(thumb); } } }
private void RegisterThumb() { var window = FindWindow(_processId); if (window == IntPtr.Zero) { UnregisterThumb(); return; } IntPtr newThumb; DwmRegisterThumbnail(MainWindow.Handle, window, out newThumb); if (newThumb == IntPtr.Zero) { UnregisterThumb(); return; } if (_thumb != newThumb) { UnregisterThumb(); _thumb = newThumb; } Point locationOnForm = MainWindow.PointToClient(Parent.PointToScreen(Location)); PSIZE size; DwmQueryThumbnailSourceSize(_thumb, out size); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES { dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY, fVisible = true, opacity = 255, rcDestination = new Rect( locationOnForm.X, locationOnForm.Y, locationOnForm.X + Width, locationOnForm.Y + Height) }; if (size.x < Width) { props.rcDestination.Right = props.rcDestination.Left + size.x; } if (size.y < Height) { props.rcDestination.Bottom = props.rcDestination.Top + size.y; } DwmUpdateThumbnailProperties(_thumb, ref props); }
public void RegisterThumbnailPreview(Form form, IntPtr PreviewHandle) { _form = form; _oldSize = _form.ClientSize; _ThumbnailHandle = DwmRegisterThumbnail(form.Handle, PreviewHandle); _Properties = new DWM_THUMBNAIL_PROPERTIES(); _Properties.dwFlags = DWM_THUMBNAIL_PROPERTIES.DWM_TNP_VISIBLE + DWM_THUMBNAIL_PROPERTIES.DWM_TNP_OPACITY + DWM_THUMBNAIL_PROPERTIES.DWM_TNP_RECTDESTINATION + DWM_THUMBNAIL_PROPERTIES.DWM_TNP_SOURCECLIENTAREAONLY; _Properties.opacity = 255; _Properties.fVisible = true; _Properties.rcSource = _Properties.rcDestination = new RECT(0, 0, _form.ClientRectangle.Right, _form.ClientRectangle.Bottom); _Properties.fSourceClientAreaOnly = true; DwmUpdateThumbnailProperties(_ThumbnailHandle, _Properties); Size sz; DwmQueryThumbnailSourceSize(_ThumbnailHandle, out sz); _WidthRatio = sz.Width; _HeightRatio = sz.Height; _form.Width = sz.Width / 2; if (_form.Width < 200) { _form.Width = 200; } _form.Disposed += new EventHandler(frm_Disposed); _form.Resize += new EventHandler(frm_Resize); frm_Resize(null, null); _timer = new Timer(); _timer.Interval = 1000; _timer.Tick += new EventHandler(t_Tick); _timer.Enabled = true; }
public void SetupMonitor(IntPtr target) { IntPtr self = new WindowInteropHelper(this).Handle; DWM_THUMBNAIL_PROPERTIES prop = new DWM_THUMBNAIL_PROPERTIES(); prop = new DWM_THUMBNAIL_PROPERTIES(); prop.rcSource = new Rect(cropArea.Left, cropArea.Top, cropArea.Right, cropArea.Bottom); prop.rcDestination = new Rect(3, 3, (int)(cropArea.Right - cropArea.Left + 3), (int)(cropArea.Bottom - cropArea.Top + 3)); prop.dwFlags = DWM_TNP_RECTDESTINATION | DWM_TNP_RECTSOURCE; DwmRegisterThumbnail(self, target, out thumb); DwmUpdateThumbnailProperties(thumb, ref prop); }
private void RegisterThumbnail() { this._thumbnailHandle = WindowManagerNativeMethods.DwmRegisterThumbnail(this.Handle, this.Id); this._thumbnail = new DWM_THUMBNAIL_PROPERTIES(); this._thumbnail.dwFlags = DWM_TNP_CONSTANTS.DWM_TNP_VISIBLE + DWM_TNP_CONSTANTS.DWM_TNP_OPACITY + DWM_TNP_CONSTANTS.DWM_TNP_RECTDESTINATION + DWM_TNP_CONSTANTS.DWM_TNP_SOURCECLIENTAREAONLY; this._thumbnail.opacity = 255; this._thumbnail.fVisible = true; this._thumbnail.fSourceClientAreaOnly = true; this._isThumbnailSetUp = true; }
private static void SetLocation(IntPtr thumbHandle, Rect rectangle) { DwmQueryThumbnailSourceSize(thumbHandle, out PSIZE size); CenterAndScaleRectangle(ref rectangle, size); var props = new DWM_THUMBNAIL_PROPERTIES { dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_SOURCECLIENTAREAONLY, fVisible = true, rcDestination = rectangle, fSourceClientAreaOnly = true }; DwmUpdateThumbnailProperties(thumbHandle, ref props); }
private void UpdateThumbnailProperties() { var dpi = GetDpiScaleFactor(); var props = new DWM_THUMBNAIL_PROPERTIES { fVisible = true, dwFlags = (int)(DWM_TNP.DWM_TNP_VISIBLE | DWM_TNP.DWM_TNP_OPACITY | DWM_TNP.DWM_TNP_RECTDESTINATION | DWM_TNP.DWM_TNP_SOURCECLIENTAREAONLY), opacity = 255, rcDestination = new RECT { left = 0, top = 0, bottom = (int)(Grid.ActualHeight * dpi.Y), right = (int)(Grid.ActualWidth * dpi.X) }, fSourceClientAreaOnly = true }; NativeMethods.DwmUpdateThumbnailProperties(_hThumbnail, ref props); }
private async void MainForm_Load(object sender, System.EventArgs e) { _currentHandle = IntPtr.Zero; _spyWindowHandle = IntPtr.Zero; _dwm_THUMBNAIL_PROPERTIES = new DWM_THUMBNAIL_PROPERTIES { Opacity = byte.MaxValue, Visible = true, SourceClientAreaOnly = false, Destination = DWMSize.ClientSize(groupBoxPreview) }; await Task.Delay(500); RefreshActiveProcess(); }
//在目标窗口(HWND = hwndDestination)内显示源窗口(HWND = hwndSource)的实时缩略图 public static ThumbnailSafeHandle Register(IntPtr hwndDestination, IntPtr hwndSource) { ThumbnailSafeHandle handle; DwmRegisterThumbnail(hwndDestination, hwndSource, out handle); Size size = handle.Size; DWM_THUMBNAIL_PROPERTIES m_ThumbnailProperties = new DWM_THUMBNAIL_PROPERTIES(); m_ThumbnailProperties.dwFlags = 29; m_ThumbnailProperties.opacity = 128; //透明度 m_ThumbnailProperties.fVisible = true; m_ThumbnailProperties.fSourceClientAreaOnly = true; //只显示客户区 m_ThumbnailProperties.rcDestination = new RECT(0, 0, (int)size.Width, (int)size.Height); //显示在目标窗口的哪个位置 DwmUpdateThumbnailProperties(handle, ref m_ThumbnailProperties); return(handle); }
public bool ScaleMonitor(double scaleFactor) { if (thumb != IntPtr.Zero) { DWM_THUMBNAIL_PROPERTIES prop = new DWM_THUMBNAIL_PROPERTIES(); prop = new DWM_THUMBNAIL_PROPERTIES(); prop.rcSource = new Rect(cropArea.Left, cropArea.Top, cropArea.Right, cropArea.Bottom); prop.rcDestination = new Rect(3, 3, (int)(scaleFactor * (cropArea.Right - cropArea.Left)) + 3, (int)(scaleFactor * (cropArea.Bottom - cropArea.Top)) + 3); prop.dwFlags = DWM_TNP_RECTDESTINATION | DWM_TNP_RECTSOURCE; DwmUpdateThumbnailProperties(thumb, ref prop); Width = scaleFactor * (cropArea.Right - cropArea.Left) + 6; Height = scaleFactor * (cropArea.Bottom - cropArea.Top) + 6; return(true); } return(false); }
/// <summary> /// Show the thumbnail of the supplied window above (or under) the parent Control /// </summary> /// <param name="window">WindowDetails</param> /// <param name="parentControl">Control</param> public void ShowThumbnail(WindowDetails window, Control parentControl) { UnregisterThumbnail(); DWM.DwmRegisterThumbnail(Handle, window.Handle, out _thumbnailHandle); if (_thumbnailHandle != IntPtr.Zero) { SIZE sourceSize; DWM.DwmQueryThumbnailSourceSize(_thumbnailHandle, out sourceSize); int thumbnailHeight = 200; int thumbnailWidth = (int)(thumbnailHeight * (sourceSize.Width / (float)sourceSize.Height)); if (parentControl != null && thumbnailWidth > parentControl.Width) { thumbnailWidth = parentControl.Width; thumbnailHeight = (int)(thumbnailWidth * (sourceSize.Height / (float)sourceSize.Width)); } Width = thumbnailWidth; Height = thumbnailHeight; // Prepare the displaying of the Thumbnail DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES { Opacity = 255, Visible = true, SourceClientAreaOnly = false, Destination = new RECT(0, 0, thumbnailWidth, thumbnailHeight) }; DWM.DwmUpdateThumbnailProperties(_thumbnailHandle, ref props); if (parentControl != null) { AlignToControl(parentControl); } if (!Visible) { Show(); } // Make sure it's on "top"! if (parentControl != null) { User32.SetWindowPos(Handle, parentControl.Handle, 0, 0, 0, 0, WindowPos.SWP_NOMOVE | WindowPos.SWP_NOSIZE | WindowPos.SWP_NOACTIVATE); } } }
private void SpyFormHandler_Load(object sender, EventArgs e) { this.TransparencyKey = Color.FromArgb(255, 2, 3, 4); this.BackColor = Color.FromArgb(255, 2, 3, 4); this.TopMost = Settings.Default.DefaultTop; this.topToolStripMenuItem.Checked = Settings.Default.DefaultTop; this.landscapeToolStripMenuItem.Checked = Settings.Default.DefaultLandscape; this.drawMarginToolStripMenuItem.Checked = Settings.Default.DefaultDrawMargin; _dwm_THUMBNAIL_PROPERTIES = new DWM_THUMBNAIL_PROPERTIES { Opacity = byte.MaxValue, Visible = true, SourceClientAreaOnly = false, Destination = DWMSize.ClientSize(this, this.landscapeToolStripMenuItem.Checked, this.FormBorderStyle == FormBorderStyle.None) }; ShowThumbnail(_spyHandler); }
private Point UpdateThumb(Rect area, int width, int height) { if (thumb != IntPtr.Zero) { PSIZE size; DwmQueryThumbnailSourceSize(thumb, out size); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY | DWM_TNP_SOURCECLIENTAREAONLY; props.fVisible = true; props.fSourceClientAreaOnly = true; props.opacity = 255; props.rcDestination.Bottom = height; props.rcDestination = area; var scaleFactor = height / (double)size.y; int scaledX = (int)(size.x * scaleFactor); if (scaledX < width) { props.rcDestination.Left += (width / 2) - (scaledX / 2); props.rcDestination.Right += (width / 2) - (scaledX / 2); } scaleFactor = width / (double)size.x; int scaledY = (int)(size.y * scaleFactor); if (scaledY < height) { props.rcDestination.Top += (height / 2) - (scaledY / 2); props.rcDestination.Bottom += (height / 2) - (scaledY / 2); } DwmUpdateThumbnailProperties(thumb, ref props); return(new Point(scaledX, scaledY)); } return(new Point(0, 0)); }
private void UpdateThumb() { if (thumb != IntPtr.Zero) { PSIZE size; DwmQueryThumbnailSourceSize(thumb, out size); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.fVisible = true; props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY; props.opacity = (byte)255;//opacity.Value; props.rcDestination = new Rect(image.Left, image.Top, image.Right, image.Bottom); if (size.x < image.Width) props.rcDestination.Right = props.rcDestination.Left + size.x; if (size.y < image.Height) props.rcDestination.Bottom = props.rcDestination.Top + size.y; DwmUpdateThumbnailProperties(thumb, ref props); } }
private void DrawThumbnail(WindowInfo client, Rect rect) { DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_RECTSOURCE | DWM_TNP_SOURCECLIENTAREAONLY; props.fVisible = true; props.fSourceClientAreaOnly = true; props.rcDestination = new DWMRect((int)rect.Left, (int)rect.Top, (int)rect.Right, (int)rect.Bottom); props.rcSource = new DWMRect((int)CLIPPING_BOX.Left, (int)CLIPPING_BOX.Top, (int)CLIPPING_BOX.Right, (int)CLIPPING_BOX.Bottom); if (client.Thumbnail != IntPtr.Zero) { DwmUpdateThumbnailProperties(client.Thumbnail, ref props); return; } int hResult = DwmRegisterThumbnail(parentWindowHandle, client.Handle, out client.Thumbnail); if (hResult == 0) { DwmUpdateThumbnailProperties(client.Thumbnail, ref props); } }
public static extern void DwmUpdateThumbnailProperties( IntPtr hThumbnail, DWM_THUMBNAIL_PROPERTIES props);
static extern int DwmUpdateThumbnailProperties(ThumbnailSafeHandle hThumbmailId, ref DWM_THUMBNAIL_PROPERTIES ptnProperties);
private void RegisterThumb() { var window = FindWindow(_processId); if (window == IntPtr.Zero) { UnregisterThumb(); return; } IntPtr newThumb; DwmRegisterThumbnail(MainWindow.Handle, window, out newThumb); if (newThumb == IntPtr.Zero) { UnregisterThumb(); return; } if (_thumb != newThumb) { UnregisterThumb(); _thumb = newThumb; } Point locationOnForm = MainWindow.PointToClient(Parent.PointToScreen(Location)); PSIZE size; DwmQueryThumbnailSourceSize(_thumb, out size); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES { dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_OPACITY, fVisible = true, opacity = 255, rcDestination = new Rect( locationOnForm.X, locationOnForm.Y, locationOnForm.X + Width, locationOnForm.Y + Height) }; if (size.x < Width) props.rcDestination.Right = props.rcDestination.Left + size.x; if (size.y < Height) props.rcDestination.Bottom = props.rcDestination.Top + size.y; DwmUpdateThumbnailProperties(_thumb, ref props); }
public static extern int DwmUpdateThumbnailProperties(IntPtr hThumbnailId, [In] ref DWM_THUMBNAIL_PROPERTIES ptnProperties);
private PSIZE UpdateThumb(IntPtr thumb, RECT dest) { if (thumb != IntPtr.Zero) { PSIZE size = GetSourceSize(thumb); DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.dwFlags = Constants.DWM_TNP_VISIBLE | Constants.DWM_TNP_RECTDESTINATION; props.fVisible = true; props.rcDestination = new RECT(dest.Left, dest.Top, dest.Right, dest.Bottom); if (size.x < dest.Right - dest.Left) props.rcDestination.Right = props.rcDestination.Left + size.x; if (size.y < dest.Bottom - dest.Top) props.rcDestination.Bottom = props.rcDestination.Top + size.y; DwmApi.DwmUpdateThumbnailProperties(thumb, ref props); return size; } else { return new PSIZE(); } }
public void RegisterThumbnailPreview(Form form, IntPtr PreviewHandle) { _form = form; _oldSize = _form.ClientSize; _ThumbnailHandle = DwmRegisterThumbnail(form.Handle, PreviewHandle); _Properties = new DWM_THUMBNAIL_PROPERTIES(); _Properties.dwFlags = DWM_THUMBNAIL_PROPERTIES.DWM_TNP_VISIBLE + DWM_THUMBNAIL_PROPERTIES.DWM_TNP_OPACITY + DWM_THUMBNAIL_PROPERTIES.DWM_TNP_RECTDESTINATION + DWM_THUMBNAIL_PROPERTIES.DWM_TNP_SOURCECLIENTAREAONLY; _Properties.opacity = 255; _Properties.fVisible = true; _Properties.rcSource = _Properties.rcDestination = new RECT(0, 0, _form.ClientRectangle.Right, _form.ClientRectangle.Bottom); _Properties.fSourceClientAreaOnly = true; DwmUpdateThumbnailProperties(_ThumbnailHandle, _Properties); Size sz; DwmQueryThumbnailSourceSize(_ThumbnailHandle, out sz); _WidthRatio = sz.Width; _HeightRatio = sz.Height; _form.Width = sz.Width / 2; if (_form.Width < 200) _form.Width = 200; _form.Disposed += new EventHandler(frm_Disposed); _form.Resize += new EventHandler(frm_Resize); frm_Resize(null, null); _timer = new Timer(); _timer.Interval = 1000; _timer.Tick += new EventHandler(t_Tick); _timer.Enabled = true; }
static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DWM_THUMBNAIL_PROPERTIES props);
public static extern int DwmUpdateThumbnailProperties(IntPtr hThumbnailId, ref DWM_THUMBNAIL_PROPERTIES ptnProperties);
private void RegisterThumbnail() { this._thumbnailHandle = DwmApiNativeMethods.DwmRegisterThumbnail(this.Handle, this.Id); this._thumbnail = new DWM_THUMBNAIL_PROPERTIES(); this._thumbnail.dwFlags = DWM_TNP_CONSTANTS.DWM_TNP_VISIBLE + DWM_TNP_CONSTANTS.DWM_TNP_OPACITY + DWM_TNP_CONSTANTS.DWM_TNP_RECTDESTINATION + DWM_TNP_CONSTANTS.DWM_TNP_SOURCECLIENTAREAONLY; this._thumbnail.opacity = 255; this._thumbnail.fVisible = true; this._thumbnail.fSourceClientAreaOnly = true; this._isThumbnailSetUp = true; }
/// <summary> /// Make a full-size thumbnail of the captured window on a new topmost form, and capture /// this new form with a black and then white background. Then compute the transparency by /// difference between the black and white versions. /// This method has these advantages: /// - the full form is captured even if it is obscured on the Windows desktop /// - there is no problem with unpredictable Z-order anymore (the background and /// the window to capture are drawn on the same window) /// </summary> /// <param name="handle">handle of the window to capture</param> /// <param name="windowRect">the bounds of the window</param> /// <param name="redBGImage">the window captured with a red background</param> /// <param name="captureRedBGImage">whether to do the capture of the window with a red background</param> /// <returns>the captured window image</returns> private static Image CaptureWindowWithDWM(IntPtr handle, Rectangle windowRect, out Bitmap redBGImage, Color backColor) { Image windowImage = null; redBGImage = null; if (backColor != Color.White) { backColor = Color.FromArgb(255, backColor.R, backColor.G, backColor.B); } using (Form form = new Form()) { form.StartPosition = FormStartPosition.Manual; form.FormBorderStyle = FormBorderStyle.None; form.ShowInTaskbar = false; form.BackColor = backColor; form.TopMost = true; form.Bounds = CaptureHelpers.GetWindowRectangle(handle, false); IntPtr thumb; NativeMethods.DwmRegisterThumbnail(form.Handle, handle, out thumb); SIZE size; NativeMethods.DwmQueryThumbnailSourceSize(thumb, out size); #if DEBUG DebugHelper.WriteLine("Rectangle Size: " + windowRect.ToString()); DebugHelper.WriteLine("Window Size: " + size.ToString()); #endif if (size.Width <= 0 || size.Height <= 0) { return(null); } DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.dwFlags = NativeMethods.DWM_TNP_VISIBLE | NativeMethods.DWM_TNP_RECTDESTINATION | NativeMethods.DWM_TNP_OPACITY; props.fVisible = true; props.opacity = (byte)255; props.rcDestination = new RECT(0, 0, size.Width, size.Height); NativeMethods.DwmUpdateThumbnailProperties(thumb, ref props); form.Show(); System.Threading.Thread.Sleep(250); if (form.BackColor != Color.White) { // no need for transparency; user has requested custom background color NativeMethods.ActivateWindowRepeat(form.Handle, 250); windowImage = Screenshot.CaptureRectangleNative(windowRect) as Bitmap; } else if (form.BackColor == Color.White) { // transparent capture NativeMethods.ActivateWindowRepeat(handle, 250); Bitmap whiteBGImage = Screenshot.CaptureRectangleNative(windowRect) as Bitmap; form.BackColor = Color.Black; form.Refresh(); NativeMethods.ActivateWindowRepeat(handle, 250); Bitmap blackBGImage = Screenshot.CaptureRectangleNative(windowRect) as Bitmap; // Capture rounded corners with except for Windows 8 if (ZAppHelper.IsWindows8()) { form.BackColor = Color.Red; form.Refresh(); NativeMethods.ActivateWindowRepeat(handle, 250); redBGImage = Screenshot.CaptureRectangleNative(windowRect) as Bitmap; } form.BackColor = Color.White; form.Refresh(); NativeMethods.ActivateWindowRepeat(handle, 250); Bitmap whiteBGImage2 = Screenshot.CaptureRectangleNative(windowRect) as Bitmap; // Don't do transparency calculation if an animated picture is detected if (whiteBGImage.AreBitmapsEqual(whiteBGImage2)) { windowImage = HelpersLib.GraphicsHelper.Core.ComputeOriginal(whiteBGImage, blackBGImage); } else { DebugHelper.WriteLine("Detected animated image => cannot compute transparency"); form.Close(); Application.DoEvents(); Image result = new Bitmap(whiteBGImage.Width, whiteBGImage.Height, PixelFormat.Format32bppArgb); using (Graphics g = Graphics.FromImage(result)) { // Redraw the image on a black background to avoid transparent pixels artifacts g.Clear(Color.Black); g.DrawImage(whiteBGImage, 0, 0); } windowImage = result; } blackBGImage.Dispose(); whiteBGImage.Dispose(); whiteBGImage2.Dispose(); } NativeMethods.DwmUnregisterThumbnail(thumb); } return(windowImage); }
internal static extern int DwmUpdateThumbnailProperties(IntPtr hThumb, ref DWM_THUMBNAIL_PROPERTIES props);
private void ShowThumbnailOnEnter(object sender, EventArgs e) { ToolStripMenuItem captureWindowItem = sender as ToolStripMenuItem; WindowDetails window = captureWindowItem.Tag as WindowDetails; parentMenuBounds = captureWindowItem.GetCurrentParent().TopLevelControl.Bounds; if (thumbnailForm == null) { thumbnailForm = new FormWithoutActivation(); thumbnailForm.ShowInTaskbar = false; thumbnailForm.FormBorderStyle = FormBorderStyle.None; thumbnailForm.TopMost = false; thumbnailForm.Enabled = false; if (conf.WindowCaptureMode == WindowCaptureMode.Auto || conf.WindowCaptureMode == WindowCaptureMode.Aero) { thumbnailForm.BackColor = Color.FromArgb(255, conf.DWMBackgroundColor.R, conf.DWMBackgroundColor.G, conf.DWMBackgroundColor.B); } else { thumbnailForm.BackColor = Color.White; } } if (thumbnailHandle != IntPtr.Zero) { DWM.DwmUnregisterThumbnail(thumbnailHandle); thumbnailHandle = IntPtr.Zero; } DWM.DwmRegisterThumbnail(thumbnailForm.Handle, window.Handle, out thumbnailHandle); if (thumbnailHandle != IntPtr.Zero) { Rectangle windowRectangle = window.WindowRectangle; int thumbnailHeight = 200; int thumbnailWidth = (int)(thumbnailHeight * ((float)windowRectangle.Width / (float)windowRectangle.Height)); if (thumbnailWidth > parentMenuBounds.Width) { thumbnailWidth = parentMenuBounds.Width; thumbnailHeight = (int)(thumbnailWidth * ((float)windowRectangle.Height / (float)windowRectangle.Width)); } thumbnailForm.Width = thumbnailWidth; thumbnailForm.Height = thumbnailHeight; // Prepare the displaying of the Thumbnail DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES(); props.Opacity = (byte)255; props.Visible = true; props.SourceClientAreaOnly = false; props.Destination = new RECT(0, 0, thumbnailWidth, thumbnailHeight); DWM.DwmUpdateThumbnailProperties(thumbnailHandle, ref props); if (!thumbnailForm.Visible) { thumbnailForm.Show(); } // Make sure it's on "top"! User32.SetWindowPos(thumbnailForm.Handle, captureWindowItem.GetCurrentParent().TopLevelControl.Handle, 0, 0, 0, 0, WindowPos.SWP_NOMOVE | WindowPos.SWP_NOSIZE | WindowPos.SWP_NOACTIVATE); // Align to menu Rectangle screenBounds = WindowCapture.GetScreenBounds(); if (screenBounds.Contains(parentMenuBounds.Left, parentMenuBounds.Top - thumbnailHeight)) { thumbnailForm.Location = new Point(parentMenuBounds.Left + (parentMenuBounds.Width / 2) - (thumbnailWidth / 2), parentMenuBounds.Top - thumbnailHeight); } else { thumbnailForm.Location = new Point(parentMenuBounds.Left + (parentMenuBounds.Width / 2) - (thumbnailWidth / 2), parentMenuBounds.Bottom); } } }