简体   繁体   English

DWM API:在某些计算机上目标位置不正确

[英]DWM API: Incorrect destination position on some computers

I'm using DWM API for displaying thumbnail of other window in my WPF app. 我正在使用DWM API在WPF应用程序中显示其他窗口的缩略图。 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). 在大多数计算机上,它可以正常工作,但在某些计算机上,我在应用程序中的缩略图位置错误且较小(它向左+向上移动了几个像素,大约缩小了30%)。

For creating a thumbnail relationship I'm using this code(and dwmapi.dll): 为了创建缩略图关系,我使用以下代码(和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). 多亏了汉斯(Hans),Dip-> px转换才出现问题(我认为WPF尺寸用像素表示)。

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. 现在缩略图的位置和大小在所有设备上都是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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