Windows Forms: Control.Scale(SizeF) does not work?

W

WernerG

Hello there.

For some reason the Function Control.Scale
(http://msdn.microsoft.com/en-us/library/ms158413.aspx) does not work. I have
absolutely no idea how to approach this kind of problem. Could it be that I
am doing something wrong or might there be a bug in .NET Cf 3.5?

To give more details. I am developing the application for a Windows CE 6
platform that has .NET CF 3.5 on it. Currently I use the emulator that comes
with Platform builder 6 but the same behaviour is seen on the device itself.
The only time I can get the scaling to work properly is if I run the
executable manually on my Development machine (WinXP).

I debugged the program and Control.Size does not change after I do a
Control.Scale on it. No child controls are scaled either.

As far as I can see this is a bug in the framework. Please tell me I am wrong.

W
 
C

Christopher Fairbairn [MVP]

Hi,

WernerG said:
I debugged the program and Control.Size does not change after I do a
Control.Scale on it. No child controls are scaled either.

The automatic scaling feature is not as useful as it first looks. After
initial form layout (within InitializeComponent) the AutoScaleDimensions
property of your form will be reset to match the current screen resolution.

This means that calls to Control.Scale etc will return what ever value you
provide them since they think a 1 to 1 mapping is required.

For a description of the autoscaling process refer to the "Automatic Scaling
in Windows Forms" article available on MSDN at
http://msdn.microsoft.com/en-us/library/ms229605.aspx (in particular check
step number 3 within the "Automatic Scaling in Action" section).

Hope this helps,
Christopher Fairbairn
 
W

WernerG

Thanks for your reply.

I must be honest I don't quite follow the quoted part below.

I have read the link but I am a bit lost in all that, I fail to inderstand
what is causing the Scale to do nothing. Most of my UI I created
programatically without the IDE generating the code. Could it be that I
forgot a scale setting somewhere?

Basically what I have is a main form with scaling properties set. Then I
added a panel to the form, lets call this mainWindow. Now I add a UserControl
to the panel. The problem is that at design time I do not know the size of
that mainWindow Panel, so when I add a Control to it I execute the following
code...

public class MainWindow: Panel, IUI
{
public T AddComponent<T>() where T: Control, new()
{
T newControl;
newControl = new T();

newControl.Scale( new SizeF( (float)Width /
(float)newControl.Width, (float)Height/(float)newControl.Height ) );

Controls.Add( newControl );
//newControl.Init();
return newControl;
}
}

That newControl.Scale line does nothing. It does not alter newControl.size
at all.

Sorry that I dont understand you properly. My windows forms knowlege is
really really small.

Any thoughts?

W


:

This means that calls to Control.Scale etc will return what ever value you
provide them since they think a 1 to 1 mapping is required.
step number 3 within the "Automatic Scaling in Action" section).
 
S

Scott Gifford

[...]
I have read the link but I am a bit lost in all that, I fail to inderstand
what is causing the Scale to do nothing.
[...]

I had a similar problem understanding Scale. What helped me was
walking through with the debugger. What I found is that the scale is
used one time, the first time the UI is layed out. It is applied to
the dimensions of everything that one time, and their height and width
are set to the new, scaled dimensions instead of what you originally
set them to. Once it has been applied the scaled DPI is set to the
same as the current DPI, so no more scaling will happen.

This works well for UI components that are layed out in the editor,
since they will all be created before the first layout, and so they
will be scaled as you would expect. Components added after this will
not be scaled, though.

After scaling happens, the original components will have their height
and width already adjusted for the screen layout, so you can use those
already-scaled dimensions to calculate the dimensions or location for
a new component. Or, if you really need to use the scaling factor,
save the scaling information in the constructor and do the scaling
manually.

Hope this helps,

----Scott.
 
W

WernerG

Scott Gifford said:
I had a similar problem understanding Scale. What helped me was
walking through with the debugger.

Interesting idea. What part of the code would you step through? I have not
managed to debug any .NET framework code before; it would be awesome though
because that will show me exactly why things are going wrong.
Once it has been applied the scaled DPI is set to the
same as the current DPI, so no more scaling will happen.

What variable is this? I have looked everywhere and found nothing that
changes.

This works well for UI components that are layed out in the editor,
since they will all be created before the first layout, and so they
will be scaled as you would expect. Components added after this will
not be scaled, though.

I never even though of this, thanks. Granted I would have hit this problem
once my basic scaling worked.

After scaling happens, the original components will have their height
and width already adjusted for the screen layout, so you can use those
already-scaled dimensions to calculate the dimensions or location for
a new component. Or, if you really need to use the scaling factor,
save the scaling information in the constructor and do the scaling
manually.

Hope this helps,

Doing scaling manually is something I am trying to avoid. I still fail to
understand why it is such a big problem for the framework to do scaling for
me in my emulator or on my real device. The scaling works perfectly under
windows XP ( if I run the binary manually ) so its not a mathematical issue.

I have done some more tests. If I call scale on my Main Form it actually
works. But only once. Subsequent calls to it fail or do nothing. I have no
idea why and this indicates to me that I have no idea what is going on. I
have read the links suggested above but nothing seems to hit home with me.

Regards,

W
 

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