windows监测并且获取分辨率和文本放大的比例

监测屏幕分辨率变化和dpi值变化就一个事件:

  SystemEvents.DisplaySettingsChanged += SystemParameters_StaticPropertyChanged

获取分辨率:

  public static Size GetResolution()
  {
      Size size = new Size();
      IntPtr hdc = GetDC(IntPtr.Zero);
      size.Width = GetDeviceCaps(hdc, (int)DeviceCapsType.DESKTOPHORZRES);
      size.Height = GetDeviceCaps(hdc, (int)DeviceCapsType.DESKTOPVERTRES);
      ReleaseDC(IntPtr.Zero, hdc);
      return size;            
  }                                               

获取dpi:

 public double GetSystemScaling()
 {
     return GetResolution().Width / SystemParameters.PrimaryScreenWidth;
 }  

这是实时能获取到的,分辨率改变后触发事件再获取都是正确的

posted @ 2025-11-10 14:36  众爱卿平身2  阅读(0)  评论(0)    收藏  举报