简体   繁体   中英

DWM API: Incorrect destination position on some computers

I'm using DWM API for displaying thumbnail of other window in my WPF app. On most computers it works fine, but on some computers my thumbnail in app is mispositioned and smaller (it's moved a few pixels left+up and it is about 30% smaller).

For creating a thumbnail relationship I'm using this code(and dwmapi.dll):

if (DwmRegisterThumbnail(IntPtr dest, IntPtr src, out IntPtr thumb) != 0) return;

PSIZE size;
DwmQueryThumbnailSourceSize(m_hThumbnail, out size);

DWM_THUMBNAIL_PROPERTIES props = new DWM_THUMBNAIL_PROPERTIES
{
   fVisible = true,
   dwFlags = DwmApiConstants.DWM_TNP_VISIBLE | DwmApiConstants.DWM_TNP_RECTDESTINATION | DwmApiConstants.DWM_TNP_OPACITY,
   opacity = 0xFF,
   rcDestination = destinationRect
};

DwmUpdateThumbnailProperties(m_hThumbnail, ref props);

For positioning in my app I'm using a canvas whose position I'm obtaining using:

var generalTransform = PreviewCanvas.TransformToAncestor(App.Current.MainWindow);
var leftTopPoint = generalTransform.Transform(new Point(0, 0));
return new System.Drawing.Rectangle((int)leftTopPoint.X, (int)leftTopPoint.Y, (int)PreviewCanvas.ActualWidth, (int)PreviewCanvas.ActualHeight);

Thanks to Hans, it was problem with dip -> px conversion (I thought that WPF dimensions are represented by pixels).

So, I changed

return new System.Drawing.Rectangle(
  (int)leftTopPoint.X, 
  (int)leftTopPoint.Y, 
  (int)PreviewCanvas.ActualWidth, 
  (int)PreviewCanvas.ActualHeight
);

to:

using (var graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
{
    return new System.Drawing.Rectangle(
      (int)(leftTopPoint.X * graphics.DpiX / 96.0),
      (int)(leftTopPoint.Y * graphics.DpiY / 96.0), 
      (int)(PreviewCanvas.ActualWidth * graphics.DpiX / 96.0),
      (int)(PreviewCanvas.ActualHeight * graphics.DpiY / 96.0)
     );
}

and now positions and sizes of thumbnails are correct on all devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM