本文介绍了对WPF应用程序禁用DPI感知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!



我已经在WPF应用程序上工作了一段时间了(作为一种学习经验,哦,这是一种学习经验),终于准备发布了。发行意味着将其安装在我的HTPC上,将用于浏览我的电影收藏。



我在运行1920 * 1080但正常DPI设置的PC上设计了它,而HTPC / TV在相同的分辨率下运行,但出于明显的原因,DPI设置较高。



问题是我的应用在HTPC上玩得很开心,搞砸了就视觉效果而言,几乎所有内容。我知道这是由于设计不当(mea culpa)造成的,但是由于它是只供我使用的应用程序,因此我想寻求一种快速解决方案,而不是完全重新设计。我读到有可能通过在AssemblyInfo.cs中添加以下内容来停止应用程序对DPI的了解:

  [assembly: System.Windows.Media.DisableDpiAwareness] 

但是,它似乎没有任何作用,该应用程序的行为保持不变。



有人可以指出我正确的方向吗?



谢谢,
Johan

解决方案

DPIAwareness



有些想法(未尝试):



您是否在XP上运行?该选项可能不适用于该平台。







您可以调用 IsProcessDPIAware 来检查您的应用进程是否具有该设置



找出DPI



以下是您查找当前DPI的方法使用:



通过您的 PresentationSource 访问 CompositionTarget Window 找出正在使用的DPI缩放比例.....然后,您可以使用这些值进行一些缩放比例调整,即缩小填充(其尺寸/长度) / etc是在设备独立单位中指定的),以便在由于较高的DPI而有效地将其扩展时,不会爆炸物理像素的使用情况(...可以通过多种方式完成,例如 ViewBox ,或元素的宽度等计算)。

  double dpiX,两倍dpiY; 
PresentationSourcepresentationsource = PresentationSource.FromVisual(mywindow);

if(presentationsource!= null)//确保已连接
{
dpiX = 96.0 * presentationsource.CompositionTarget.TransformToDevice.M11;
dpiY = 96.0 * presentationsource.CompositionTarget.TransformToDevice.M22;
}








进行缩放调整




  • 使用 ViewBox 技巧

    请参阅我之前做的这个回答,该问题允许 Canvas 使用被解释为本机的位置像素,无论DPI缩放大小如何。


  • CompositionTarget TransFormToDevice 缩放矩阵/ code>,然后从中计算出一个新的矩阵,该矩阵将取消缩放并在 LayoutTransform 或<$ c $中使用该矩阵c> RenderTransform 。


  • 使用修改DIP(与设备无关的位置)的方法(甚至可以将其放在Converter中)位置/长度在明确的基础上。...如果希望窗口大小匹配特定的像素大小,则可以这样做。



Good day!

I've been working on a WPF app for some time now (as a learning experience and oh boy it was a learning experience) and it's finally ready for release. Release means installing it on my HTPC where it will be used to browse my movie collection.

I designed it on my PC which runs 1920*1080 but at the normal DPI setting, while the HTPC/TV is running at the same resolution but a higher DPI setting for obvious reasons.

The problem is my app goes bonkers on the HTPC, messing up pretty much everything as far as visuals go. I know this is due to bad design (mea culpa), but since it's an application that will only be used by me I'm looking for an quick fix, not a full redesign. I read it would be possible to stop the app from being DPI aware by adding the following to AssemblyInfo.cs:

[assembly: System.Windows.Media.DisableDpiAwareness]

However, it doesn't seem to have any effect and the behavior of the app remains the same.

Could anyone point me in the right direction?

Thank you,Johan

解决方案

DPIAwareness

Just some ideas (not tried):

Are you running on XP? That option might not work on that platform.

What follows are probably just different ways to set the same DpiAwareness option:

  • look at the "compatibility mode" settings on your EXE...right click it's properties...and turn on "Disable display scaling"

  • create a manifest, and say you aren't aware

    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
     ...
      <asmv3:application>
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
          <dpiAware>false</dpiAware>
        </asmv3:windowsSettings>
      </asmv3:application>
     ...
    </assembly>
    

  • call SetProcessDPIAware (think you have to call this early i.e. before Window is created)

    http://msdn.microsoft.com/en-gb/library/windows/desktop/ms633543(v=vs.85).aspx

You can call IsProcessDPIAware to check if your apps process has had the setting applied to it or not.

Finding out the DPI

Here's how you find out what DPI you are currently using:

Access CompositionTarget via the PresentationSource of your Window to find out what DPI scaling it's using.....you can then use the values to do some scaling adjustments i.e. scale down your "stuff" (whose sizes/length/etc are specified in Device Independent Units), so that when its scaled up due to a higher DPI being in effect it doesn't explode the physical pixel usage (...this can be done in various ways e.g. ViewBox, or calculations on widths, etc on elements ).

double dpiX, double dpiY;
PresentationSource presentationsource = PresentationSource.FromVisual(mywindow);

if (presentationsource != null) // make sure it's connected
{
    dpiX = 96.0 * presentationsource.CompositionTarget.TransformToDevice.M11;
    dpiY = 96.0 * presentationsource.CompositionTarget.TransformToDevice.M22;
}

Do Scaling Adjustments

  • use the ViewBox trick

    See this answer I made before that allowed a Canvas to use positions that were interpreted as "native" pixel no matter what the DPI scaling.

    WPF for LCD screen Full HD

  • access the TransFormToDevice scaling matrix on the CompositionTarget, and from that calculate a new matrix that just undoes that scaling and use that in LayoutTransform or RenderTransform.

  • use a method (maybe even put it in a Converter) that modifies a DIP (Device Independent Position) position/length on an explicit basis....you might do that if you want your Window Size to match a particular pixel size.

这篇关于对WPF应用程序禁用DPI感知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 02:25