Keep Window Size after display set to 120 dpi
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
you could use the DisableDpiAwarenessAttribute on the assembly
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
All Replies
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
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 SnippetMatrix m =
PresentationSource.FromVisual(Application.Current.MainWindow).CompositionTarget.TransformToDevice;
double dpiX = m.M11*96;
double dpiY = m.M22*96;
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?
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?
you could use the DisableDpiAwarenessAttribute on the assembly
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
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 !
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
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 helpsThanks, Marco.
Best Regards,
Pen


