Report Viewer - scaling issue with system DPI change

后端 未结 2 1230
暖寄归人
暖寄归人 2021-01-12 15:04

I have a Winforms application (in Visual Studio 2010) that contains a Report Viewer control that previews and prints an A4 size report.

One user has his Windows font

相关标签:
2条回答
  • 2021-01-12 15:53

    I ran into the same problem. WinForms ReportViewer is already DPI aware and will do its own scaling. You just have to tell the system that your application is DPI aware so that the system doesn't try to scale it after.

    Add a manifest to your application if you haven't already, then inside the tag, add the following:

      <asmv3:application  xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
          <dpiAware>true</dpiAware>
        </asmv3:windowsSettings>
      </asmv3:application>
    

    You can also use an API function SetProcessDPIAware, but it is recommended against: http://msdn.microsoft.com/en-us/library/ms633543.aspx

    0 讨论(0)
  • 2021-01-12 16:02

    As a complimentary answer to what @JoMan said (since I can't comment on his post) bear in mind that you can manually scale up the UI elements in your app relatively simply. So leave your application DPI aware (so that your system doesn't distort the printed results) as JOMan suggested. You could use something like this...

    Using g As System.Drawing.Graphics = WindowsForm.CreateGraphics
     Dim sngScaleFactor As Single = 1
     Dim sngFontFactor As Single = 1 
    
     If (graphics.Dpix >= 96) Then
          sngScaleFactor = (graphics.Dpix / 96) - 0.25
          sngFontFactor = (graphics.Dpix / 96) - 0.25
     End If
    
     If WindowsForm.AutoScaleDimensions = WindowsForm.CurrentAutoScaleDimensions Then
                WindowsForm.Scale(sngScaleFactor)
                For Each child As Control In WindowsForm.Controls
                    ScaleFontRecursively(child, sngFontFactor)
                Next
     End If
    End Using
    

    I'm sure many people will argue (rightly!) that you generally don't want to detect DPI and manually scale yourself, but the bug with the dpi autoscaling screwing up printed microsoft reports is still outstanding as of 2018, so this provides an easy work around.

    0 讨论(0)
提交回复
热议问题