首页 > 其他分享 >WPF阻止窗体被系统缩放,使用显示器DPI

WPF阻止窗体被系统缩放,使用显示器DPI

时间:2024-06-11 15:45:51浏览次数:7  
标签:IntPtr WindowMessage 缩放 窗体 result WPF DPI

WPF默认是跟随系统DPI变化(缩放与布局)而缩放窗体的;

微软把它称为默认DPI感知,当DPI发生变化时WPF感知到后缩放窗体,介绍链接:设置进程的默认 DPI 感知 (Windows) - Win32 apps | Microsoft Learn

如果我们不希望窗体被缩放,而是让窗体使用显示器DPI该怎么办呢?

首先修改app.manifest,如下xml改到对应的xml

  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/pm</dpiAware>
      <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
    </windowsSettings>
  </application>

然后在创建窗体前添加如下代码就可以阻止窗体跟随系统缩放。

//TypeUtil -> https://www.cnblogs.com/RedSky/p/18215093
TypeUtil.InvokeMethod(null, 
    TypeUtil.GetType("PresentationCore", "System.LocalAppContext"), "DefineSwitchDefault",
    new Type[] { typeof(string), typeof(bool) },
    new object[] { "Switch.System.Windows.DoNotScaleForDpiChanges", true });
var v = TypeUtil.GetType("WindowsBase", "MS.Win32.NativeMethods+PROCESS_DPI_AWARENESS").GetEnumValues().GetValue(0);
TypeUtil.SetProperty(null, typeof(HwndTarget), "ProcessDpiAwareness", v);
TypeUtil.SetProperty(null, typeof(HwndTarget), "AppManifestProcessDpiAwareness", v);

 

但是之后Window的DpiChanged就不会起作用了,如果想要监听DPI变化可以重写窗体OnSourceInitialized,使用如下方法:

protected override void OnSourceInitialized(EventArgs e)
{
    var aa = (HwndSource)HwndSource.FromDependencyObject(this);
    aa.AddHook(new HwndSourceHook(HwndTargetFilterMessage));
    base.OnSourceInitialized(e);
}
private IntPtr HwndTargetFilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    IntPtr result = IntPtr.Zero;
    //WindowMessage: https://referencesource.microsoft.com/#WindowsBase/Base/MS/Internal/Interop/NativeValues.cs
    result = HandleMessage((WindowMessage)msg, wParam, lParam);
    if (result != IntPtr.Zero)
        handled = true;

    return result;
}
internal IntPtr HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
{
    IntPtr result = IntPtr.Zero;
    switch (msg)
    {
        case WindowMessage.WM_DPICHANGED:
            Debug.WriteLine("WindowMessage.WM_DPICHANGED");
            break;
        case WindowMessage.WM_DPICHANGED_AFTERPARENT:
            Debug.WriteLine("WindowMessage.WM_DPICHANGED_AFTERPARENT");
            break;
    }
    return result;
}

 

标签:IntPtr,WindowMessage,缩放,窗体,result,WPF,DPI
From: https://www.cnblogs.com/RedSky/p/18242167

相关文章

  • 发现XWPFDocument写入Word文档时的小BUG:两天的探索与解决之旅
    引言最近在使用XWPFDocument生成Word文档时,遇到一个错误:“未将对象引用设置到对象的实例”。这个平常很容易找到原因的问题却困扰了我两天,最终发现问题出在设置段落时赋值了空值。本文将详细记录这个问题的原因及解决方法,希望能对遇到相同问题的开发者有所帮助。第一天:问题的发......
  • ACCESS 窗体的"模式"属性只能同时存在一个
    这是我实际工作中遇到的问题.两个窗体同时设置了"模式"属性为"是",预想中的结果是左边最后弹出的数据表窗体为最上层窗体.结果左右两个窗体同级了,能点左,也能点右.当然主窗体不能点是正确的.解决思路:这明显就是"模式"冲突了.所以我们设置成只同时存在一个模式属性为"是"......
  • wpf LiveCharts 使用
    LiveCharts用于显示图表和仪表盘安装LinveChartsinstall-packageLiveCharts.Wpf使用示例Code<CartesiantChart><!--图标控件--><LineSeriesStroke="#7ADA95"Values="156,60,70,760,320,540,880,340,500"><!--序列:序列中......
  • C# WPF入门学习主线篇(十六)—— Grid布局容器
    C#WPF入门学习主线篇(十六)——Grid布局容器欢迎来到C#WPF入门学习系列的第十六篇。在前几篇文章中,我们已经探讨了Canvas、StackPanel、WrapPanel和DockPanel布局容器及其使用方法。本篇博客将介绍另一种功能强大且灵活的布局容器——Grid。通过本文,您将学习如何使用......
  • 按住panel 拖动窗体的方法
    有些时候,我们的窗体没有顶部栏,比如:窗体的borderstyle=bsNone;我们想在顶部放置一个panel,来拖动窗体,方法如下,直接上代码了:unitUnit1;interfaceusesWinapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls......
  • WPF GroupStyle GroupStyle.HeaderTemplate
    //xaml<Windowx:Class="WpfApp148.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF DataContext order and filter via CollectionViewSource.GetDefaultView(DataCon
    //xaml<Windowx:Class="WpfApp146.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF CollectionViewSource.GetDefaultView ICollectionViewLiveShaping IsLiveSorting
    <Windowx:Class="WpfApp147.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF Image zoomin zoomout move
    //xaml<Windowx:Class="WpfApp145.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF ListBox ListBox.ItemTemplate DataTemplate
    <Windowx:Class="WpfApp144.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......