120dpi setting for windows and dynamic added controls

R

Rolf Welskes

Hello,

I have the following:
..net 1.1 application (must be), on windows xp
one form (via designer).
one userControl with labels (via designer), BUT the usercontrol is NOT added
to the form via designer.

Now at runtime the userCtrl01 is added in the form:

private void btnNext_Click(object sender, System.EventArgs e)
{
MyUserCtrl userCtrl01 = new MyUserCtrl();
userCtrl01.Location = .............;
userCtrl01.Size = .............;
this.Controls.Add(userCtrl01);
this.ResumeLayout(true);


}

All this is done when windows xp has setting to 96dpi.
All works fine.

Now I change windows xp to 120dpi.
All other forms (made by designer) work fine with 120dpi.
But this form with dynamic added userCtrl does not.
First: The usercontrol is not set to the right size; the fonts are larger as
it's right for 120dpi, but the size of the userctrl is the same as at 96dpi.
If I now make a trick to get this, it works, but then the labels in the
usercontrol have not the right size.

What is to do, that all work with 96dpi AND 120dpi.
I cannot seet this at development time because it's a product which runs on
different systems.

Thank You for any help.
Rolf Welskes
 
L

Linda Liu [MSFT]

Hi Rolf,

I have performed a test based on your description and did reproduce the
problem. When the Windows is set to 96dpi, I add an instance of the
UserControl on the form at design time. Then I change the setting to 120dpi
and restart the machine. Under the 120dpi mode, the usercontrol added at
design time becomes larger. However, the usercontrol I add at run time is
as big as it was at 96dpi.

When the program is run at 120dpi mode, the form is auto scaled by Windows
operation system. Before the form is auto scaled, the usercontrol that is
added on the form at design time has already exists on the form (the
initialize code of this usercontrol is within the InitializeComponent
method and the InitializeComponent method is called in the form's
constructor). So this usercontrol will be auto scaled with the form as
well. On the contrary, the usercontrol that is added later at run time
won't be auto scaled.

As a workaround, I recommend you to set the AutoScale property of the form
to False. Thus, the form won't be auto scaled at 120dpi mode as well as the
usercontrol on the form.

Hope this helps.
If my suggestion doesn't apply to your scenario or you have anything
unclear, please feel free to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you.
I think this is not a good solution.
But maybe I must take it.

But what is in the future.
Is this problem also under dot 2.0 and when and how will it be solved.

Thank you and best regards
Rolf Welskes
 
R

Rolf Welskes

Hello,
I have tried it out.
It does not work.
Ok, the form does not change the size.
Now the added usercontrol is ok.
But the fonts are scaled to 120 dpi.
So it looks awfull.

So there must be a work around which realy works.

We also work with C++ native code.
We could for example write

c#

extern Scale(... handle...);

void Add()
{
this.Controls.Add(myUserCtrl);
Scale(myUserCtrl.GetHandle());
}

c++

void Scale(LONG handle)
{
code which uses windows native
}

So the problem would be how could this work ?

Each control in native windows is a window, so there must be a way to scale
the control with native windows.
But, how?

Thank you and best regards
Rolf Welskes
 
L

Linda Liu [MSFT]

Hi Rolf,

Thank you for your reply.

I agree with you that the workaround of preventing the form and controls on
it from autoscaling is not very good.

I spent more time researching on this problem. Version 1.0 and 1.1 of the
..NET Framework supported autoscaling in a straightforward manner that was
dependent on the Windows default font used for the UI, represented by the
Win32 SDK value DEFAULT_GUI_FONT. This font is typically only changed when
the display resolution changes. The following mechanism was used to
implement automatic scaling:

1. At design time, the AutoScaleBaseSize property (which is now deprecated)
was set to the height and width of the default system font on the
developer's machine.

2. At runtime, the default system font of the user's machine was used to
initialize the Font property of the Form class.

3. Before displaying the form, the ApplyAutoScaling method was called to
scale the form. This method calculated the relative scale sizes from
AutoScaleBaseSize and Font then called the Scale method to actually scale
the form and its children.

4. The value of AutoScaleBaseSize was updated so that subsequent calls to
ApplyAutoScaling did not progressively resize the form.

Automatic scaling was implemented in only the Form class, not in the
ContainerControl class. As a result, user controls would scale correctly
only when the user control was designed at the same resolution as the form,
and it was placed in the form at design time.

So it is a limitation in .NET 1.0 and 1.1. A good news is that in .NET 2.0,
base support for scaling has been moved to the ContainerControl class so
that forms, native composite controls and user controls all receive uniform
scaling support.

As for a better workaround for the limitation in .NET 1.0 and 1.1, we could
caculate the auto scaled factor and then apply it to the added user control
at run-time.

The following is a sample.

public class Form1 : System.Windows.Forms.Form
{
private Size baseSize = new Size(0,0);
public Form1()
{
this.baseSize = this.Size;
}

// click the button1 to add an instance of the UserControl1 on the
form at run time
private void button1_Click(object sender, System.EventArgs e)
{
UserControl1 uc = new UserControl();
this.Controls.Add(uc);
// caculate the scale factor
float dx = ((float)this.Size.Width) /
((float)this.baseSize.Width);
float dy = ((float)this.Size.Height) /
((float)this.baseSize.Height);
uc.Scale(dx,dy);
}
}

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
R

Rolf Welskes

Hello,
thank you for your answer.
The scaling example is fine.
I have used this method and it works fine.
Thank you again and best regards
Rolf Welskes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top