Ask a questionAsk a question
 

AnswerKeep Window Size after display set to 120 dpi

  • Tuesday, March 25, 2008 8:04 AMPen Chu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello:

     

    I don't want my WPF program window to be enlarged when the display setting is changed from 96dpi to 120dpi, Can I achieve this?

     

    Thanks.

Answers

All Replies

  • Tuesday, March 25, 2008 11:46 AMAtul GuptaMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    you may be aware that this happens due to device independant units that WPF uses to draw. I am not sure if this can be turned off and not sure why you would want it that way.

     

    Check some more discussion on this here - http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2566603&SiteID=1

  • Tuesday, March 25, 2008 1:12 PMThomas Claudius Huber Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Hi Pen,

     

    additional to Atul's post, I also think you can't turn off this behaviour. But you can find a programmatic way to implement custom logic.

     

    Add an Eventhandler to the SystemEvents.DisplaySettingsChanged-Event. Read out the dpi-Settings in this Eventhandler by using the TransformToDevice-Property of CompositionTarget and maybe do something "strange" with your window. ;-)

     

    Code Snippet

    Matrix m =

    PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;

    double dpiX = m.M11*96;

    double dpiY = m.M22*96;

     

     

     

  • Wednesday, March 26, 2008 12:30 AMPen Chu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Atul, thanks for your reply.

    My condition is, if I have a fixed size window, say 1000 X 750.

    When Windows is in 96 dpi, my window can just fit into the 1024x768 screen.

    But after the user changes to 120 dpi or 150 dpi,

    the window will exceed the screen.

     

    That's why I need to find a way to prevent this condition, do you have more suggestion?

  • Wednesday, March 26, 2008 12:35 AMPen Chu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi, Thomas, thanks for your hints. I think it will work.

     

    But does it means if I have hundreds of controls in my program, I'll going to adjust them one by one?

     

  • Wednesday, March 26, 2008 5:51 AMLesterLobo - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
  • Wednesday, March 26, 2008 6:23 AMPen Chu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     LesterLobo - MSFT wrote:

    you could use the DisableDpiAwarenessAttribute on the assembly

     

    Thank you LesterLobo,

     

    However, I added

    [assembly: System.Windows.Media.DisableDpiAwareness]

    to my project's AssemblyInfo.cs

    It makes no difference.

    All the window and control sizes still get enlarged as before.

     

    Sincerely,

    Pen

  • Wednesday, March 26, 2008 6:26 AMAtul GuptaMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     Thomas Claudius Huber wrote:

    Add an Eventhandler to the SystemEvents.DisplaySettingsChanged-Event.

     

    Thomas, a slight issue with this... the application will need to be running in order to receive this event

     

    Interesting to note about the DisableDpiAwarenessAttribute...I wasn't aware of this !

  • Wednesday, March 26, 2008 6:49 AMPen Chu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     Atul Gupta wrote:

    Interesting to note about the DisableDpiAwarenessAttribute...I wasn't aware of this !

     

    Thanks for all your kindly.

     

    It seems I am not alone, DisableDpiAwarenessAttribute seems not work. Please see:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2634417&SiteID=1

     

    Pen

  • Thursday, March 27, 2008 3:59 AMMarco Zhou Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
     Pen Chu wrote:

    It seems I am not alone, DisableDpiAwarenessAttribute seems not work. Please see:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2634417&SiteID=1



    This property actually works, but if the DPI settings doesn't match up with the device native DPI settings, this problem will occur, basically Windows always presume that you are running under native resolution, and the DPI settings will match up with that native DPI settings, but the problem here is that DPI settings is always wrong.

    I don't think you can actually control how the end users would set the DPI value, what you actually can do is to write the UI in WPF and turn on WPF's DIU feature, and let the end users fumble with their display settings.

    Hope this helps
  • Tuesday, April 01, 2008 12:48 AMPen Chu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Thanks, Marco.

     

    Best Regards,

    Pen