Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DWM API:某些计算机上的目标位置不正确_C#_Wpf_Dwm - Fatal编程技术网

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

C# DWM API:某些计算机上的目标位置不正确,c#,wpf,dwm,C#,Wpf,Dwm,我正在使用DWMAPI在我的WPF应用程序中显示其他窗口的缩略图。在大多数计算机上,它工作正常,但在一些计算机上,我的应用程序中的缩略图位置错误,而且更小。它向左+向上移动了几个像素,大约小了30% 为了创建缩略图关系,我使用以下代码和dwmapi.dll: if (DwmRegisterThumbnail(IntPtr dest, IntPtr src, out IntPtr thumb) != 0) return; PSIZE size; DwmQueryThumbnailSourceSi

我正在使用DWMAPI在我的WPF应用程序中显示其他窗口的缩略图。在大多数计算机上,它工作正常,但在一些计算机上,我的应用程序中的缩略图位置错误,而且更小。它向左+向上移动了几个像素,大约小了30%

为了创建缩略图关系,我使用以下代码和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);
对于在我的应用程序中的定位,我使用的是画布,其位置是通过以下方式获得的:

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);

多亏了Hans,dip->px转换出现了问题,我认为WPF维度是用像素表示的

所以,我改变了

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


现在,缩略图的位置和大小在所有设备上都是正确的。

您正在将英寸转换为像素。当视频适配器DPI未设置为96dpi 100%时,这将不起作用。谷歌wpf将英寸转换为像素,第一次点击看起来不错。
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)
     );
}