In my case, No matter how user change win7 dpi setting,I can always get the correct screen resolution via code PrimaryScreen.Bounds,just as same as what set in the control-panel-display on system of win7.
But in my colleague's computer, I run the same code just after setting his os's dpi from 100% to 150%. I get a relative bounds result.
For example, his screen resolution is 1920 * 1080 now, I ran the code after I had set his os dpi from 100% to 150%. Finally I get the resolution is 1280 * 720 by the code PrimaryScreen.Bounds.
Why this?
I did this in the same way, but I got a same resolution.
This is by design. Windows has supported changing the video adapter's DPI (dots-per-inch) setting to 96 dpi (100%) and 120 dpi (125%) for a long time. At least since XP, possibly before that. So applications are expected to know how to deal with this.
Starting at Vista, Windows now supports arbitrary DPI settings. When you go past 120 dpi, Windows takes over the job of scaling the output of your program. A feature of Aero, it lies to the app about the DPI setting and lets the app render its window content into a memory buffer. It then rescales that buffer content before it draws it to the screen.
This is an important app-compat feature, it allows old programs to still be functional on a modern machine and avoids their window from turning into an unusable postage stamp. The result is not entirely flawless, you do see the quality of text in particular being less than stellar.
The Aero lie isn't just about the DPI setting, it also lies about the screen size. Necessary to prevent the app from creating a window that is too large and won't fit the screen anymore after it is rescaled. Which is why you got 1280 x 720.
To turn off the lie, you have to tell Aero that you do support higher DPI settings, even though your program is marked to still be compatible with old Windows versions. This answer shows you how to edit the manifest of your program to add the <dpiAware> element.
Related
I am writing this application and it has a size of (800 X 400). The problem is, it resizes when I run it on different monitor resolutions and all the objects get bigger. Is there a way to fix its size for all monitor resolution without maximizing?
In general, if your form is defined with a fixed size of (800 x 400) then it will generally appear larger on the same screen if you change the resolution to say (800 x 600).
The screen resolution defines how many pixels make up the view port. changing to a high resolution will make all apps, not just yours appear smaller. Changing to a low resolution can make all apps, again not just yours appear larger.
Changing the screen resolution is changing the DPI
Windows and WinForms do provide some mechanisms for scaling your app so the interface can be more proportional on different screens, but it can be a bit more hit and miss to take advantage of in WinForms compared to other technologies like WPF/UWP, especially if you have an older application or code base.
If the scaling is not what you expect, first check that the users have turned scaling in their display settings back to 100%, many laptops and tablets have the set to 125% OOTB. When you have not correctly defined your interfaces to take advantage of font scaling it can look a bit funky.
Setting windows font scaling back to 100% means your app will only have to contend with DPI scaling issues rather than both font and DPI scaling that windows might impose.
font scaling in the display settings, in windows 10 looks like this:
Have a read over:
Automatic scaling in Windows Forms
The key to getting it right is that all forms and controls in your app must be configured to use DPI based scaling OR Font based scaling. When the two concepts are confused the UI can look like a mess as some elements will resize and others may not.
This scaling behaviour is defined in the form as the AutoScaleMode:
- This defaults to Font in new apps.
Many 3rd part components however may be set to DPI mode, especially if they have any drawing canvases or their display logic is referenced by pixels (which is pretty common in WinForms custom painting.)
For this reason, setting your forms to use DPI scaling might solve your issue
Also check out some of these posts in SO that might help explain your observations:
How to write WinForms code that auto-scales to system font and dpi settings?
How to control the font DPI in .NET WinForms app
How to configure an app to run correctly on a machine with a high DPI setting (e.g. 150%)?
I use C# Winforms in Visual Studio 2010. Since I've upgraded my laptop from the old 1366 x 768 resolution laptop to a new laptop with a 1920x1080 resolution, I noticed a problem when continuing development on the new laptop. I isolated the issue down to the new 150% Windows OS-wide scaling (Windows on the old laptop used 100% scaling). When I run the program, the right side of the window is strangely chopped off.
Here's the working version on the old laptop (1000 pixels wide, or 1500 pixels in 150% scaled Windows mode):
And here's the new version on the new laptop with the chopped window (1286 pixels wide, when it should be 1500 due to the 150% scaling).
Upon immediately loading the project into the new 150% scaled Windows setup and running the program, it appears to work fine, but as soon as you move one of the buttons so that VS updates the designer code (button 5 was moved as you can see), and then run, the window is chopped.
Even when manually going into the Form1.Designer.cs code, and adjusting the ClientSize to a stupid high horizontal value like ClientSize = new System.Drawing.Size(3000, 709); results in a window width that is limited to 1286 pixels at most. (Not so) coincidentally, 1286 is around 1920 / 1.5.
Basically, what the heck is going on? This appears like a bug with VS's scaling management. To be clear, the problem occurs when moving development from a 100% Windows scaled screen to a 150% Windows scaled screen, on any system. You don't need two laptops to reproduce the problem, it can all be reproduced on a single laptop (but make sure you log out and into Windows after switching from/to 100% or 150%).
Finally, to be clear, I know about setting the DPI-aware setting to true using an app.manifest file. Although this allows clearer text and solves the chopped window problem, I do NOT want to go this route due to inconsistent changes in my program's GUI such as overlapping controls and misplaced items on some setups.
I've found two half-solutions so far:
First is upgrading to Visual Studio 2017 (which allows you to force 100% scaling in VS Forms designer even in 150% Windows scaling)
Second is keeping with an earlier version of Visual Studio (including VS 2010), but making sure Windows is set to 100% scaling. If you've only just changed the scaling setting, make sure to log out and in of Windows to ensure the scaling is applied appropriately.
A relatively simple workaround is to create a Panel Control that is the size you desire for the window, send it to the first layer (right click the control in the designer> send to back). Then set the form to AutoResize to fit that control. (also set AutoSizeMode to GrowAndShrink in case the form appears larger than it should)
This works great if you need Fixed forms but for Resizable forms you might want some extra code that resizes this panel when the form's resize event happens.
I have build a UWP project in 12" tablet size (which I chose from options provided when I design the UI in designer)
However when I run the application, it is shown too big on my screen, my design could not shown completely on the screen.
For example, I have 2 buttons in my design form top to down, only the top one is shown but the below one is missing due to my screen is not big enough to show them. But my laptop screen is 15.6", and previously the program was run in another desktop with a 24" monitor and it shows correctly.
Is there anything I can do to change the resolution/scale of my application on the laptop screen? My laptop resolution is 1920x1080.
Thank you.
I think the resolution is 2160*1440 for 12" 150% scale, my laptop resolution is only 1920*1080, how do I change the resolution?
Windows automatically selects a scale factor for each display based on its DPI (dots-per-inch) and the viewing distance of the device. So that the scale factor is defined by the windows. Details please reference Effective pixels and scale factor.
Users can override the default value and by going to Settings >System> Display > Scale and layout settings page. You could change the scale to 100% for your laptop.
I have a Winforms application that is working correctly on various versions of windows and various resolutions. When the application runs on a Motion CL900 tablet, however, the layouts are completely messed up; mainly caused by panel controls that are perfectly sized on other computers now being too small and causing scrollbars. The Motion devices are running Windows 7 Professional.
I have read on a few posts on stackoverflow that Windows machines have a standard DPI of 96, but this device clearly isn't 96 DPI. Is the fact that this device is "non-standard" a result of it's screen or is it caused by a setting in it's OEM version of Windows 7? I have read that I must set AutoScalingDimensions for my form to reflect the DPI of the PC I am developing on. But how do I even know what DPI my machine/screen is that I am developing on?
I have the following in my designer file for the form that has the layout issues:
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
I would appreciate anyone who has had experience with working with Winforms at various DPI resolutions shedding some light on this question, and maybe give some guidelines for .Net developers how to successfully handle non-standard DPIs.
There's indeed two different uses of the term here:
The display device's native resolution. This is actually how many pixels are within 2.54 cm. Larger values mean obviously a clearer, but smaller picture overall.
The OS' scaling of fonts and widgets. 96 dpi is the "native" size for Windows for example. That is, if you have a display that also has 96 dpi, then the controls are the size they should be when you look at them. Also point sizes for fonts are exactly the right size, then. If you have a display with, say, 120 dpi and set the OS to that measure too and then put both side by side you could measure with a ruler and find widgets and fonts in both cases to be the same size.
Nowadays the second measure mostly controls widget size if your eyes are not capable of working with too small controls. And plenty of applications have problems displaying correctly if you change it, although it's far better since Vista.
Dot per inch are related to the monitor/video card/resolution and just means how many points (or pixels) are shown in the length unit (inch).
Clearly is not related to the OS because you can have same values in all OS (Windows, Linux, Mac)...
DPI is dots per inch and it is a setting you can set in your OS. However, it is a characteristic of the monitor.
Since you are on Windows 7, to change DPI settings, you would want to do:
Control Panel
Appearance and Personalization
Display
Set Custom Text Size (DPI) - (on side bar)
Just change it till you get to 96 DPI (100% usually).
Windows 7 has an option in the Display properties called "Set custom text size (DPI)" if this is set to more than 100% it can impact how elements appear in an application, and can cause layout problems. I'd recommend keeping this at 100%, which is 96 DPI in Windows terms.
Note: the actual DPI resolution is determined by your monitor.
I have a WPF application rendering fonts to a larger size than I expect.
The catch is that only WPF applications I write seem to have this effect, and the problem happens only on one computer. The effect also happens with all WPF programs I write (not just one), and all windows exhit the effect.
I am using Visual Studio 2008 with .NET 3.5 SP1; all programs written in WPF.
The operating system is Windows XP with the latest service packs.
The screen resolution is 1680x1050.
Windows and buttons using Windows XP style.
Font size is Normal.
DPI setting is normal size (96 dpi).
I tested my applications on a two other computers with same OS and settings and it looks fine. My programs run on dozens of other computers and I do not see this problem.
Here is a window that looks good:
Here is a window that shows the problem (notice the button on the lower left, and the font size):
I know I can increase the width and add margin to compensate, but I need to understand why this happening.
I am close to a release and I want to make sure my users do not see this effect.
All ideas appreciated.
I'm not running XP right now, but I thought you could customize the size of the text that appears in window captions (and buttons) via the Display Properties (right-click desktop, select Properties, and it's the 3rd or 4th tab, I think). It lets you select colors for selected items, background colors of windows, etc.
That would be separate from setting the DPI settings.
This looks like an unholy XP feature. It had the option to change the size of the system font independently from the video adapter DPI setting. Very convenient to XP users living in Easy Asia where glyphs that make up the letters are very intricate and need all the pixels they can get to make their script legible.
It is covered by Windows Forms with the Form.AutoScaleMode property, which defaults to Font instead of Dpi. WPF is however strongly biased to Dpi, the normal way of scaling.
I don't think the twain shalt ever meet. Tell your user to reset the XP setting. It is buried somewhere in the Control Panel + Display setting, I don't have it anymore to tell you exactly where to look. Or the logical alternative, changing the video DPI setting to match the font size. That's was in the Adapter tab, IIRC. Changing it from 96 to 120 DPI would be a good guess.