適配各種Windows分辨率,為DPI添加感知,當在高DPI時,禁用WINFORM縮放等。


因為現在高分屏越來越多,很多windows設備必須設置高DPI,這樣很容易導致WINFORM整體錯位,因此我們需要自己適配。禁止縮放

 

在程序配置清單 mainfest中添加如下。

<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>True</dpiAware>
    </asmv3:windowsSettings>
  </asmv3:application>
</assembly>

其中dpiAware節點參數MSDN解釋如下,各取所需吧。

False -- Sets the application to not DPI-aware.
True -- Sets the application to system DPI–aware.
Per-monitor -- On Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to not DPI–aware.
True/PM -- On Windows 8.1, sets the application to per monitor-DPI aware. On Windows Vista through Windows 8, sets the application to system-DPI aware.

注意這個紅字部分,有的人自動生成mainfest之后不會添加這個節點,其實只要在第一行添加這個紅字部分就行了

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >

第一行改成這樣就行了

<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">

其次上面只是設置了感知,你要具體去改,在構造函數中,組建生成之前 添加如文字感知:

 Font = new Font(Font.Name, 8.25f * 96f / CreateGraphics().DpiX, Font.Style, Font.Unit, Font.GdiCharSet, Font.GdiVerticalFont);
            InitializeComponent();

這樣基本上你的窗體就不會被縮放了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM