views:

25

answers:

2

I have an large application developed in .Net Compact Framework 1.0 that has been developed over the last 9 years with large amount of forms and custom controls. The application is designed for 240x320 screens. It scales good to a 480x640 screen when compiled with Compact .Net 1.0 and Visual Studio 2003.

I upgraded the application to .Net 2.0 using the default upgrade wizard of Visual Studio 2008. The application uses the full screen and all the controls are laid out as designed when using a device with resolution of 240x320. But the application uses only top left 25% of the screen when using a device with resolution of 480x640.

I tried using the code: AutoScaleDimensions = new SizeF(96,96); AutoScaleMode = AutoScaleMode.Dpi;

It works on the Form, but does not work on the dynamic controls (standard/custom) that are placed on the form.

Is there a way to force the application to use scaling similar to that done when complied using .Net 1.0, with out using the AutoScaleDimensions/AutoScaleMode properties.

Thanks.

+1  A: 

In sounds like your code to create custom controls contains hard coded values that assume a particular DPI, probably of the designer used, 96.

You could analyse the host resolution of the device your application is running on using something like:

const float designResolution = 96.0f; 
float scaleFactor;

System.Drawing.Graphics g = f.CreateGraphics();
float runningResolution = g.DpiX;
scaleFactor = runningResolution / designResolution;
g.Dispose();

You would then modify any hard coded co-ordinates in your code that is creating the dynamic controls to adjust the values with the calculated scaleFactor, for example:

const int controlY = 8;
int yPosition = (int)(controlY * scaleFactor);
simons19
+1  A: 

I found a kind of solution to this problem.

I get the scale-factor similar to the procedure explained by simons19 and then when adding any dynamically created control to the form, I call the method control.Scale(scale-factor). This solved my problem. Scale method scales both Location and Size of the control so, I set both location property and size property of the dynamically created control before calling the scale property.

Example:

Label lblTest = new Label();
lblTest.Text = "A Test Label";
lblTest.Location = new Point(10, 5);  //Designed in relation to a 96 dpi screen
lblTest.Size = new Size(30, 10);      //Size Designed in relation to 96 DPI screen
parentControl.Controls.Add(lblTest);
//Scale Factor is calculated as mentioned by simons19
lblTest.Scale(scaleFactor);           //Scale the control to the screen DPI.
Kishore A