protected override void OnSourceInitialized(EventArgs e) { var hwndSource = PresentationSource.FromVisual(this) as HwndSource; if (hwndSource != null) { var style = User32.GetWindowLongEx(hwndSource.Handle); style |= WindowExStyles.WS_EX_TOOLWINDOW | WindowExStyles.WS_EX_NOACTIVATE | WindowExStyles.WS_EX_TRANSPARENT; User32.SetWindowLongEx(hwndSource.Handle, style); } RECT rect; if (NativeMethods.GetWindowRect(this._target, out rect)) { var targetWidth = rect.Right - rect.Left; var targetHeight = rect.Bottom - rect.Top; var dpi = PerMonitorDpi.GetDpi(this._target); var width = this.ActualWidth * dpi.ScaleX; var height = this.ActualHeight * dpi.ScaleY; this.Left = (rect.Left + (targetWidth - width) / 2) / dpi.ScaleX; this.Top = (rect.Top + (targetHeight - height) / 2) / dpi.ScaleY; } base.OnSourceInitialized(e); }
private Rect GetExtendFrameBounds() { RECT frameBounds; Dwmapi.DwmGetWindowAttribute(this.Handle, DWMWINDOWATTRIBUTE.DWMWA_EXTENDED_FRAME_BOUNDS, out frameBounds, Marshal.SizeOf(typeof(RECT))); var dpi = PerMonitorDpi.GetDpi(this.Handle); var l = frameBounds.Left * (1 / dpi.ScaleX); var t = frameBounds.Top * (1 / dpi.ScaleY); var w = (frameBounds.Right - frameBounds.Left) * (1 / dpi.ScaleX); var h = (frameBounds.Bottom - frameBounds.Top) * (1 / dpi.ScaleY); return(new Rect(l, t, w, h)); }
public static Icon GetIconFromResource(Uri uri) { var streamResourceInfo = System.Windows.Application.GetResourceStream(uri); if (streamResourceInfo == null) { throw new ArgumentException("Resource not found.", nameof(uri)); } var dpi = PerMonitorDpi.GetDpi(IntPtr.Zero); // get desktop dpi using (var stream = streamResourceInfo.Stream) { return(new Icon(stream, new Size((int)(16 * dpi.ScaleX), (int)(16 * dpi.ScaleY)))); } }
protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); RECT rect; if (NativeMethods.GetWindowRect(this._target, out rect)) { var targetWidth = rect.Right - rect.Left; var targetHeight = rect.Bottom - rect.Top; var dpi = PerMonitorDpi.GetDpi(this._target); var width = this.ActualWidth * dpi.ScaleX; var height = this.ActualHeight * dpi.ScaleY; this.Left = (rect.Left + (targetWidth - width) / 2) / dpi.ScaleX; this.Top = (rect.Top + (targetHeight - height) / 2) / dpi.ScaleY; } }
private void UpdateCore() { var positionDpi = this._systemDpi; if (PerMonitorDpi.IsSupported) { var currentDpi = PerMonitorDpi.GetDpi(this.Owner.Handle); if (currentDpi != this.CurrentDpi) { this.DpiScaleTransform = currentDpi == this._systemDpi ? Transform.Identity : new ScaleTransform((double)currentDpi.X / this._systemDpi.X, (double)currentDpi.Y / this._systemDpi.Y); this.CurrentDpi = currentDpi; } } var left = this.GetLeft(positionDpi); var top = this.GetTop(positionDpi); var width = this.GetWidth(positionDpi); var height = this.GetHeight(positionDpi); User32.SetWindowPos(this._handle, this.Owner.Handle, left, top, width, height, SetWindowPosFlags.SWP_NOACTIVATE); }
/// <summary> /// キャプチャのコア処理。 /// </summary> /// <param name="target">キャプチャ対象</param> /// <returns>キャプチャ画像</returns> protected override Bitmap CaptureCore(Rectangle?target) { return(ScreenHelper.CaptureScreen(target.Value.ToPhysicalPixel(PerMonitorDpi.GetDpi(target.Value)))); }
private static Dpi GetMonitorDpi() { return PerMonitorDpi.GetDpi(IntPtr.Zero); }
/// <summary> /// InternetExplorerのページ全体をキャプチャします。 /// </summary> /// <param name="handle">ウィンドウハンドル</param> /// <returns>ビットマップ</returns> private Bitmap InternetExplorerCapture(IntPtr handle) { var isScrollWindowPageTop = Settings.General.IsWebPageCaptureStartWhenPageFirstMove.Value; var scrollDelayTime = Settings.General.ScrollDelayTime.Value; var fixHeaderHeight = Settings.General.FixHeaderHeight.Value; using (InternetExplorer ie = new InternetExplorer(handle, scrollDelayTime)) { // DPI取得 var dpi = PerMonitorDpi.GetDpi(handle); // クライアント領域、スクロール位置、スクロールサイズの取得 Rectangle client = ie.Client; Point scrollPoint = ie.ScrollPoint; Size scrollSize = ie.ScrollSize; // ページ先頭移動フラグが設定されている場合、スクロール位置をページ先頭に設定する if (isScrollWindowPageTop) { scrollPoint = ie.ScrollTo(0, 0); } // 出力先のビットマップ生成 // クライアント領域の幅 x (スクロール領域の高さ - スクロール位置Y) var bmpWidth = (int)((scrollSize.Width - scrollPoint.X) * dpi.ScaleX); var bmpHeight = (int)((scrollSize.Height - scrollPoint.Y) * dpi.ScaleY); Bitmap bmp = new Bitmap(bmpWidth, bmpHeight - (bmpHeight / client.Height - 1) * fixHeaderHeight); Debug.WriteLine($"BitmapHeight:{(int)((scrollSize.Height - scrollPoint.Y) * dpi.ScaleY)}"); Debug.WriteLine($"ClientHeight:{client.Height}"); Debug.WriteLine($"FixHeaderHeight:{fixHeaderHeight}"); using (Graphics g = Graphics.FromImage(bmp)) { // スクロール開始/終端座標を求める // ※終了座標はマージン前の高さで求める Point scrollStart = new Point(scrollPoint.X, scrollPoint.Y); Point scrollEnd = new Point(Math.Max(scrollSize.Width - client.Width, 0), Math.Max(scrollSize.Height - client.Height, 0)); // 右終端までスクロールしながらキャプチャする CaptureControl(handle, g, ref client, ref scrollPoint, ref scrollStart, dpi); while (scrollPoint.X < scrollEnd.X) { // スクロールしてキャプチャする scrollPoint = ie.ScrollTo(scrollPoint.X + client.Width, scrollPoint.Y); CaptureControl(handle, g, ref client, ref scrollPoint, ref scrollStart, dpi); } // 先頭行以降は固定ヘッダーを除外してキャプチャする client.Y += fixHeaderHeight; client.Height -= fixHeaderHeight; // 終端までスクロールしながらキャプチャする bool isFirst = true; int prevScrollY = -1; while (scrollPoint.Y < scrollEnd.Y && prevScrollY != scrollPoint.Y) { prevScrollY = scrollPoint.Y; // スクロールしてキャプチャする scrollPoint.X = scrollStart.X; scrollPoint = ie.ScrollTo(scrollPoint.X, scrollPoint.Y + client.Height); CaptureControl(handle, g, ref client, ref scrollPoint, ref scrollStart, dpi, (isFirst ? fixHeaderHeight : 0)); isFirst = false; // 右端までスクロールしながらキャプチャする while (scrollPoint.X < scrollEnd.X) { // スクロールしてウィンドウキャプチャする scrollPoint = ie.ScrollTo(scrollPoint.X + client.Width, scrollPoint.Y); CaptureControl(handle, g, ref client, ref scrollPoint, ref scrollStart, dpi); } } } return(bmp); } }