Why is Visual Studio 2017's new AutoScale support resizing my Forms app at runtime? - winforms

We have a Windows Forms application used for testing our products that has had pretty much the same forms and dialogs for nearly 10 years now. AutoScaleMode in the designer is set to Font for both forms, and that hasn't changed since the original design. AutoSize is set to false and just for good measure (I guess) AutoSizeMode is set to GrowOnly. The following source lines are in Program.cs:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Remember, this has been working flawlessly for years...
One of the recent updates to VS2017 included AutoScale support for monitors with different DPI. I have a relatively high DPI monitor, so when I made some changes to a Settings dialog (adding controls, etc) I started getting bright yellow banners cross the top of the designer surface telling me that AutoScaling was set to 125%, and would I like to change that? I tried going back and forth, and when in 100% mode (Autoscaling off) I was warned that XAML forms might not display correctly. Fine, I am working with Forms so I went back to "normal" scaling and the form looked fine.
Until I tried running the program. Now when I start the program the main form looks like this (details deleted to protect the guilty):
...but when I open the settings dialog it looks like this:
Yikes! It looks worse in practice, the relative images here don't do justice to the difference in size and scale.
I have no idea what got changed or how / why this is happening, but there's no way I can put this into production. I've tried changing the AutoScale settings, to no avail. Can somebody point me in the right direction here?
Thanks in advance!
EDIT: It seems the rescaling only happens on my machine when I run the app in Debug. Whatever is messing up the display of the forms on my machine doesn't do that on my associates' machines and isn't replicated to the executable produced by the build server.
On another note, I tried every DPI-related setting I could find, added those that weren't there due to the program's age, all to no avail. Nothing I have tried has had any effect on the program's display weirdness on my machine. Ugh.

I tried all the tricks I could find, short of disabling AutoScale completely, and nothing worked. I finally merged our develop branch in to my feature branch and inspected all 270 edits, choosing the oldest settings I could find for all size parameters, all controls. Ugh.
Along the way I stumbled across the following line in the Designer.cs file for the form:
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
This setting is NOT accessible from Visual Studio's designer, as far as I can tell, nor can I find anything about how or why it gets set. What I did find was that in the earlier version that did work as expected, the values were (8F, 17F) -- so I manually edited the line to match the older, working version. Success!
I also checked my Windows display settings, and the Custom Scaling value was at 100%, so I used the registry hack mentioned in one of the articles I found following the first link from the comment above (thanks, #Jimi) to disable auto-scaling in Visual Studio, then turned off the notification. Now I'm (finally) back in business.
Registry hack is here:
https://learn.microsoft.com/en-us/dotnet/framework/winforms/disable-dpi-awareness-visual-studio

If one uses the registry hack mentioned in the answer from DaveN59, and there is already entry for your ...\devenv.exe (mine was previously set to run as admin) then add the DPIUNAWARE string to the end of the "Data". Make sure to use a space as a separator.
Example: ^ RUNASADMIN DPIUNAWARE
I'm not sure what the ^ is for, but it was there and I just added DPIUNAWARE string. Also, if you just add this string, it will come up unscaled, but you will still see the yellow banner. To turn that off you'll need to select that option from with in Visual Studio. The instructions are in the link for the registry hack, also provided above. For conveinece here's the menu navigation path.
To disable notifications, choose Tools > Options to open the Options
dialog. Then, choose Windows Forms Designer > General, and set DPI
Scaling Notifications to False.

Related

Why does Inspect.exe hang frequently and inconsistently display AutomationId?

I'm trying to use MS UI Automation to test a WPF application, and am using the Inspect Object tool (inspect.exe) included with the Windows SDK to look up the AutomationId property on certain elements.
Inspect is behaving very strangely for me:
If I close all applications and start the WPF application and Inspect, inspect is able to see the AutomationId property for various UI elements. Elements which do not have an AutomationId simply show two quotation marks denoting an empty string ("").
After I perform a few actions in the WPF application, inspect.exe hangs and I have to kill it and restart it. Even though the machine's CPU and RAM utilization are around 50% or less, I've tried waiting several minutes--possibly close to 20 or 30 mins on a couple occasions--to no avail.
After restarting, inspect.exe can no longer find an AutomationId for any UI element, even those which did have them previously. What's more, the property is completely missing when hovering the mouse over the WPF application--it is no longer listed at all, not even with an empty string value.
If I move the mouse to another screen (specifically, to another computer, using Mouse Without Borders), the AutomationId property reappears with a value of "FormDot"
If I restart only inspect additional times while the WPF application is still running, inspect still behaves the same as after the first restart.
If I restart only the WPF app while inspect is still running, inspect still behaves the same as after its first restart.
If I close both inspect and the WPF app, then start inspect, then start the WPF app, everything works correctly for a while and inspect finds the AutomationId on a few elements in the WPF app...up until the point at which inspect hangs again.
I've tried running inspect both normally and as an administrator as suggested in https://stackoverflow.com/a/7833728/44737, and it behaves the same either way.
What, if anything, am I doing wrong? Am I just too impatient and do I need to wait a really long time instead of assuming inspect is hung? And why does inspect's behavior regarding AutomationId vary?
There are more than one version of Inspect.exe. The latest to my knowledge is the one dated from 2012 that says version 7.2.0.0 in the help/about dialog box.
The old one doesn't have a tree view on the left with all detected automation elements displayed in a tree, so it's easy to check you're using the right one.
The latest one works quite correctly, however, IMHO, the best tool so far to work with UI Automation is Visual UI Automation Verify. It's a .NET program, and he source is available here:
UI Automation Verify (UIA Verify) Test Automation Framework.
Note that although it's a .NET program, it doesn't use the standard .NET automation dlls (more on that here: What's the difference of UISpy.exe and Inspect.exe? (From Microsoft Windows SDK)).
About the AutomationId property, to clarify my initial comment to the question, I meant its usefulness depends on the program that you're trying to automate.
If you own it as a developer, it's clearly interesting. For example, if you're working with WPF, you can use the x:Uid property, it's clearly meant for UI automation. In the Winforms space, it's also quite useful because UI Automation will use the control's AccessibleName by default and revert to the Name as a fallback, for the AutomationId value.
But there are many apps that don't rely on .NET (browsers, native apps, etc.) Usually, for these apps, it's easier to use other properties.
I have been using inspect.exe for a while on a Microsoft Surface Studio PC (running Windows 10), and my experience is that inspect.exe will hang much more frequently (sometimes always) when Windows Updates are pending. When the updates are out of the way, inspect.exe is still somewhat slow, but much more stable.

WPF XAML slow start up for a small application

I'm using VS2010 WPF / XMAL to create a very detailed order form. It has about 50 data items on it all data-bound in xaml. All is fine in development on my win7 PC. When I deploy the app, via one click or an MSI, the application take seconds to download but up to 5 mins to prepare before the login screen is shown on a windows 7 pc. But on my XP machine it's done in seconds, for exactly the same app!. I've trouble shot the order form by commenting out some of the xaml I found that there is breaking point to the amount of items it can show before I get a start up problem. For example I have 30 items without issue but once you add one more then they very slow startup times occur. It doesn't matter which area of the xaml I comment out as soon as it goes to one extra I get the slow start up time?
I'm only using grids, stack panels and textboxes with single items of data. No lists
Very strange as XP doesn't have this problem. Any ideas?
What graphics cards do you have in each machine? It may be to do with there DirectX compatibility? Changing to software rendering might give you an insight (or at least some consistency.
Try the advice from this page:
http://blogs.msdn.com/b/jgoldb/archive/2010/06/22/software-rendering-usage-in-wpf.aspx
This is the profiling tool you should use for WPF:
http://msdn.microsoft.com/en-us/library/aa969767(v=vs.110).aspx
On my windows 7 pc I changed these setting in symantec antivirus; In Auto-protect I disable the Enable File System Auto-Protect option. Also in the global settings I disabled "Insight for" and " Enable bloodhound heuristic virus detection.
After this my app loaded in a second. When I enabled the virus settings back to what it was and did a reboot my app continued load up in seconds.
I'm not sure why Symantec was inconsistent with this issue... By just adding one extra line of xaml, be it a textbox or a label I get a massive difference in behaviour. My assemblies are sign on my build server with domain certificate so I would assume they are trusted.

Changing the default file path for icons in Visual Studio

On a WPF project, if I have an image and click on 'Source' and then 'Add' I get the choice to import images into the project. Is there a way of changing this file location?
VS2010 currently defaults to the "Libraries / Pictures" setting. I'd rather not change where this library points to as I don't store icons and pictures together.
After a bit of research I set up a new Windows library (useful article here) for icons but I can't work out how to set VS to default to the icons library I created, does anyone know how to do this?
Using Process Monitor on my machine when doing this, I see the following:
Calls to RegOpenKey, RegQueryKey aimed at:
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32
This seems to be a list of the last projects I've added/opened in Visual Studio. To test this, I added a project from a different location to my solution, then tried to change the source of the image.
Sure enough, the dialog defaulted to that location.
Your mileage my vary, but certainly, on my machine, the behaviour seems tied to what project I opened last. You can use Process Monitor to monitor devenv.exe while you choose an image on your machine to see what happens.
Given this, though, it would seem that you can't change this behaviour, although you can influence it.

In WPF pop up messages, drop down list appear behind main window

I am currently working on WPF touch-screen application. I am developing it on Windows XP machine. I have tested it on this machine and it works perfectly fine. But when I deploy it to Windows Embedded machine I start to get strange behaviour: all pop up messages, drop down list, context menus appear behind the main window.
I am also setting the focus on my main window, when application loads, to enable context menu on the main screen.
Also my main window's AllowTransparency is set to true, (I have seen people had similar issues when having set to AllowTransparency). And also this didn't happen in the previous release.
Edit: The issues has gone after several compilations, I was unable to reproduce it, but I am still trying.
I also think it has to be something related to graphic driver, as it happens on one windows XP machine, but not on another (hardware is different, one run XP embedded 2nd XP professional).
Any idea why this is happening?
Have you tried forcing the ZIndex of the elements to that they are higher than the main window?
This MSDN blog post describes it's use - but the important part might be:
The first set of Rectangle objects uses the default z-ordering rendering of objects, which is based on the position of the child object in the Canvas collection
So if the order the objects are created has changed (for whatever reason) you might see this behaviour.
Explicitly setting the ZIndex will confirm or deny this.

Visual Studio 2010 Beta 2 + ClearType

I was wondering: are you satisfied with the text-rendering in the Visual Studio 2010 editor (Beta 2)? On my primary monitor it looks very blurred, even when using font size 12!
When using font size 10 or 11 it's horrible. Can the WPF text rendering mode be set explicitly for VS code window?
EDIT: I am using Windows 7 x64 and my primary monitor is a Benq G2412HD. What's kind of strange is that the font seems to be nicer on the secondary monitor, which is a 4 year old 19' TFT.
EDIT: I tried several Cleartype settings but none really looks as nice as when using Visual Studio 2008.
The first image is from the primary monitor, the second from the secondary. Both are using Consolas 10pt (my preferred font).
Primary screen http://img4.imageshack.us/img4/6789/vs2010.png
Secondary screen http://img4.imageshack.us/img4/7986/vs20102.png
I use Consolas size 10 and I have no issues.
Try following the instructions on Microsoft's website for tuning clear type. You can find it here.
If you use IE when accessing that website, you can make changes to clear type settings right from the browser.
TextSharp is the answer right now. I really hope they continue to fix this issue because I've had terrible results on my primary and secondary monitors with the standard rendering. Using beta 2.
With 8 or 9pt Lucida Console the text is fine with text mode set to 'Aliased'.
Here is how VS2008 and VS2010 beta2 editor text rendering looks for me, side by side. The font is Consolas 13pt.
VS2008:
http://int19h.org/so/cleartype_vs9.png
VS2010:
http://int19h.org/so/cleartype_vs10.png
I don't see any observable differences.
[EDIT] Okay, I've reproduced it with the color scheme posted. It seems that the key part here is to use bright text on dark background. With dark-on-bright, the output seems to be the same.
Here's some guesswork. Apparently, Direct2D (which WPF uses for antialiased text output) always "gives precedence" to foreground (i.e. text) color over background color when doing subpixel antialiasing. On the other hand, traditional GDI ClearType seems to always give precedence to dark colors over bright ones. Thus, with bright letters and dark backgrounds, ClearType text becomes thinner, but Direct2D text remains of the same size, pixel-wise. Furthermore, as bright pixels are more intensive, the same amount of them "stands out" more with same foreground/background contrast, so bright-on-dark D2D text looks noticeably "bolder".
Well I experience the same oddity (not only in VS2010, but in all WPF applications). Sadly there seems no way of setting a "backwards style" text rendering in WPF in general.
I just found this addon "Text Sharp" for VS2010 on the VS gallery which lets you select different font aliasing options for the VS2010 text editor, but at least for me, this didn't help with the issue.
Here is the link, if you might want to try it: Text Sharp VS2010 extension
Please have a look at the screenshots in the following update (to come in final release of VS2010), and see if the improvements solve your issues with VS font rendering:
http://blogs.msdn.com/text/archive/2010/03/05/additional-wpf-text-clarity-improvements.aspx
http://blogs.msdn.com/visualstudio/archive/2010/03/11/wpf-text-clarity-improvements.aspx
Have you ensured that ClearType is enabled on your OS? I've seen similar issues with 2010 when ClearType was disabled on my machine. Re-enabling ClearType made the Text snappy looking again.
For some reason the ClearType setting on my OS kept getting undone when I TS'd around a bit. I had to reset it a couple of times but it seems to have calmed down recently (I believe I was using a Pre-RTM build of Win7 at the time).
Try using Courier New font.
Tools -> Options -> Environment -> Fonts and Colors
ollifant I agree with you, they are different.
Others may not see any differences from screen shots, but on the actual machine I can see differences. Loading the same project with the same settings side by side looks different. I think it is the difference in how WPF renders fonts or something.
The VS 2010 pane looks shifted slightly left, like kerning in the font is off by a little or something. Again - same font in both VS 2008 and 2010.
I have tried now on Windows 7 and Windows Vista. Maybe older XP machines render differently, can't say (and no I won't load XP to find out).
I have noticed a rendering problem with Visual Studio 2010b2 as well.
I've tried adjusting the clear type settings to no avail.
I use consolas 9 pt on win7x64 with a average 19 inch TFT.
This is what it looks like on my system.
A side by side screen shot
OK here's what you do. Finally figured it out!
You need to reset cleartype to the default values. Don't try to tune it based on what you think looks good - because what you think looks good won't in VS2010.
I noticed on a brand new install of Windows 7 my VS2010 text suddenly became a lot nicer. After running cleartype to try to get nice text on a wall mounted Sony TV I found it had totally screwed up text for my normal external monitor.
I haven't yet found a way to reset cleartype explicitly, but apparently the below explains what the defaults are :
When you open ClearType Text Tuner,
select "Turn on ClearType" check box
and click on Next. Then, again click
on the Next after setting Native
Resolution. Then, select the options
as given below:
1st Screen – 1st Option out of 2
2nd Screen – 2nd Option out of 6
3rd Screen – 1st Option out of 3
4th Screen – 2nd Option out of 6
Finally, click on Finish.
This is for VS2010 RC.

Resources