0

I've designed a project containing of some forms. The problem is that it doesn't display correctly on wide monitors. I've searched for it and found out that it works correctly for dpi= 96. I wanna change dpi via registry in vb.net (not manually) Here is the code I use:

    Dim dpi As Graphics = Me.CreateGraphics
    If (dpi.DpiX <> 96 And dpi.DpiY <> 96) Then
        Dim DPISetting As RegistryKey = My.Computer.Registry.CurrentUser.OpenSubKey("HKEY_CURRENT_USER\Control Panel\Desktop\WindowsMetrics", True)
        DPISetting.SetValue("AppliedDPI", 96) '**
    End If

But I get a nullRefrence Exception at the line with **. Object reference not set to an instance of an object. I've really worked on it for a long time but couldn't find what the problem is. I'd be so thankful if you have any suggestion or solution for it.

butterfly
  • 93
  • 5
  • 13

1 Answers1

2

This design is fundamentally broken.

Your application should not change a global system setting just to work around a bug in the code. That's a good way to guarantee that your users will immediately uninstall your application and never use it again. If you're lucky, they won't tell their friends. You're usually not so lucky.

Instead of trying to make your hack work, why not just fix the actual problem? If your form doesn't display correctly at high (or low) DPI settings, you need to make it work in those scenarios. That's part of developing desktop apps—making sure that your app works in heterogeneous environments.

To that end, you might find the advice in these answers useful:

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Well, I felt before it's a bit strange to change registry setting. I had also thought about using AutoScaleMode.dpi. Now that you've suggested I'm sure. Thanks for the useful advice. – butterfly May 26 '12 at 09:27