Pixel Shifts Designer vs Running

Topics: CAB & Smart Client Software Factory
Dec 6, 2005 at 12:26 PM
originally posted by: mbpublic

It appears that when using CAB some controls are not drawn on the screen exactly as shown in the designer. This can create a real problem when doing layout in the designer.

To see the problem, run the EventBroker QuickStart, add a customer and click Show List. The border of the text box under the Local Customers label is partially obscured.

You can also see the problem by looking at the text inside any button in any application. The text is drawn a couple of pixels higher than in the designer.

In my experiments, all controls look normal when drawing a form without using CAB, but when displaying the exact same form using CAB (just change the code in Program) creates the problem.

My setup is the release version of Visual Studio and the November C# release of CAB.

If other people with this setup are not having this problem occur, I would very much like to know. Or, if anyone knows of a workaround, that would be greatly appreciated.
Dec 6, 2005 at 1:09 PM
originally posted by: BradWilsonMSFT

As far as I know, we don't make any adjustments to the positions of controls at runtime. Try this with plain Windows Forms and see if the problem is there.

Also, are you running in large fonts (120DPI) mode? There have always been known issues with Win32 apps having odd sizing issues as it tries to map your 96DPI layout to 120DPI.
Dec 6, 2005 at 1:42 PM
originally posted by: mbpublic

The problem does not appear with plain Windows forms projects and my DPI is the normal 96.

I don't believe the problem occured with beta versions of .Net and CAB. I upgraded to the latest versions of both VS and CAB at the same time, so I can't say which is the cause the issue.

Would you like me to send you a bare skeleton project with the issue? Although, as I mentioned, it exists in the CAB QuickStarts and is easily seen on my system in the Customer List window of the Event Broker where one of the textbox borders is obscured by the label above when running (the border is not obscured in the designer).
Dec 6, 2005 at 1:52 PM
originally posted by: BradWilsonMSFT

Yes, please. My e-mail address is bradwils at microsoft dot com. :)
Dec 7, 2005 at 9:24 AM
originally posted by: mbpublic

Mystery solved.

Applications using 2.0 should have the SetCompatibleTextRenderingDefault flag set to false.

A normal Windows project creates the following code in the Program.cs file:

STAThread
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}

To start a CAB application you use this code instead in the Program.cs file:

STAThread
static void Main()
{
new Program().Run();
}

which in turn calls this method in the CAB CompsiteUI.WinForms dll.

protected override void Start()
{
Application.Run(Shell);
}

I had previously thought that CAB took care of calling EnableVisualStyles and other normal start-up code. But it doesn't call Application.SetCompatibleTextRenderingDefault(false) and I don't believe it can, because when I added the line to the override Start() method in the CAB source, I get an error message saying that Application.SetCompatibleTextRenderingDefault(false) must be called before any object is created.

So the solution is to add Application.SetCompatibleTextRenderingDefault(false) above the call to new Program().Run in Program.cs as shown below.

STAThread
static void Main()
{
Application.SetCompatibleTextRenderingDefault(false);
new Program().Run();
}


You may also want to add Application.EnableVisualStyles() here as well, although that could also be added to the CAB source in the override void Start() method if its needed at all.

The QuickStarts should probably be updated to include Application.SetCompatibleTextRenderingDefault(false).

For more information on SetCompatibleTextRenderingDefault, see the following blog entry:

http://blogs.msdn.com/jfoscoding/archive/2005/10.aspx