Custom initial size of Form

  • Thread starter Thread starter Sergey Hrushev
  • Start date Start date
S

Sergey Hrushev

Hello All!

I found quite strange problem with a Form from System.Windows.Forms:

What I need is the borderless Form which must have initial dimensions and
position
specified by application (i.e. not default ones).
So for my form I've specified FormBorderStyle = None and StartPosition =
Manual.
After that I've called SetBounds(...) for it and made it visible with
Visible = true.

But the dimensions of the form on screen were different from ones I
specified in SetBounds.
I tried to look for something like StartPosition but for size (StartSize or
something),
but it seems nothing exists for this.

From other side when I've tried to set Visible = true at first and then
SetBounds(...) all was working correctly.
But this causes so terrible flickering that this method seems to be
unusable.

Does anybody know how this problem can be solved?

Best regards,
Sergey Hrushev.
 
Sergey said:
But the dimensions of the form on screen were different from ones I
specified in SetBounds.
I tried to look for something like StartPosition but for size (StartSize or
something),
but it seems nothing exists for this.
Does anybody know how this problem can be solved?

If the form is bigger than specified, this may be a problem with
autoscaling. Do you have a high dpi setting? Does the problem occur if
you change the display settings to normal (96dpi)?
If so, than you should read this article:
http://msdn.microsoft.com/en-us/library/ms229605.aspx

I don't have a simple "click that option and it works" solution, because
I depends how you would like to handle those situations (high dpi
settings, large font settings, ...). Do you resize your form and your
content? Only your content, but not your form? Do you ignore it
completely and override the user preferences? Not a simple problem.

Hope to help,
Bernd Rausch
 
Hello Bernd Rausch,

thank you for your answer!
I don't have a simple "click that option and it works" solution, because
I depends how you would like to handle those situations (high dpi
settings, large font settings, ...). Do you resize your form and your
content? Only your content, but not your form? Do you ignore it
completely and override the user preferences? Not a simple problem.

This form doesn't need any scaling, also it doesn't contain any controls.
And only custom painting is used for it.
So no any DPI & co. problems ever exist.

The situation is very simple in theory, and in WinAPI there's no such a
problem at all
but what to do in WinForms if I just need for example dimensions 100*100?

What I can't understand is why only first time my SetBounds(...) dimensions
are became overridden?
But when I change dimensions of already displayed form they're remains
correct and not scaled/changed.

Best regards,
Sergey Hrushev.
 
Sergey said:
This form doesn't need any scaling, also it doesn't contain any controls.
And only custom painting is used for it.
So no any DPI & co. problems ever exist.

When does this problem occur? Always? Or just in high dpi settings? Only
on some computers? Did you try to change the AutoScaleMode of your form?
The situation is very simple in theory, and in WinAPI there's no such a
problem at all
but what to do in WinForms if I just need for example dimensions 100*100?

When you set the Bounds to 100x100, what are the real bounds of the
form? Is the form getting bigger or smaller?

I just created a Demo Project. Just a form without controls, size set to
200x200 and the following method:

protected override void OnPaint( PaintEventArgs e ) {
e.Graphics.DrawString(
this.ClientRectangle.ToString( ),
this.Font,
Brushes.Black,
5f, 5f );
base.OnPaddingChanged( e );
}

If I start the programm with normal dpi settings, the text is
{X=0,Y=0,Width=192,Height=166}. If I change to high dpi settings
(Display properties -> Settings -> Extended -> DPI Settings [sorry, I'm
using a German version, so the naming may not be correct]), reboot
Windows and start the program again, the form is bigger and the text is
{X=0,Y=0,Width=256,Height=204}.
If I change the AutoScaleMode property of my form to None, then the size
is the same, regarless of the dpi settings.

Hope to help,
Bernd Rausch
 
Hello Bernd Rausch,

thank you for your answer!
When does this problem occur? Always? Or just in high dpi settings? Only
on some computers?

The problem occurs only first time the form being displayed.
It's independent of current DPI setting and occurs always when form is
displayed "first time".
I tried on different computers and the result was always 100% the same.
Did you try to change the AutoScaleMode of your form?

Yes, I tried to switch off the AutoScaleMode unfortunatelly without any
effect.
When you set the Bounds to 100x100, what are the real bounds of the
form? Is the form getting bigger or smaller?

I need quite small forms (for some special purposes) and the size of form on
the
screen is always larger than the one I specified.
I tried many times with different settings (AutoScaleMode and etc.) but the
form was
always bigger than specified.

Best regards,
Sergey Hrushev.
 
Hello Bernd Rausch,

thank you for your answer!
When does this problem occur? Always? Or just in high dpi settings? Only
on some computers?

The problem occurs only first time the form being displayed.
It's independent of current DPI setting and occurs always when form is
displayed "first time".
I tried on different computers and the result was always 100% the same.
Did you try to change the AutoScaleMode of your form?

Yes, I tried to switch off the AutoScaleMode unfortunatelly without any
effect.
When you set the Bounds to 100x100, what are the real bounds of the
form? Is the form getting bigger or smaller?

I need quite small forms (for some special purposes) and the size of form on
the
screen is always larger than the one I specified.
I tried many times with different settings (AutoScaleMode and etc.) but the
form was
always bigger than specified.

Best regards,
Sergey Hrushev.
 
Back
Top