How to make a form resizable by fixed steps

K

kurotsuke

I'd need to resize a form using fixed steps. What I mean is that the
window show have width and height mutiple of let's say 50 pixels.
My form has a border.
Thanks for any suggestions.
 
G

Guest

Just register with the OnResize event for the form and do the calculation
checks and set the width and height manually in increments of 50.
 
B

Bruce Wood

I would create my own form that derives from Form, and override
OnResize(). Change OnResize() to check the window width and height and
change them if they are not a multiple of 50 pixels.

I tried it myself... the code looks like this:

protected override void OnResize(EventArgs e)
{
if (this.Width % 50 != 0)
{
this.Width = (int)(Math.Ceiling(this.Width / 50.0) * 50);
}
if (this.Height % 50 != 0)
{
this.Height = (int)(Math.Ceiling(this.Width / 50.0) * 50);
}
base.OnResize (e);
}

I'll warn you that the border redrawing while you're resizing at run
time looks really weird: you get a dual border flickering look, one
border where the mouse is actually dragging and another at the size the
window will really be when you release the mouse. However, it does
produce a window that has a height and width of a multiple of 50
pixels, and it allows you to resize it appropriately.

(Of course, if you take the border into account, etc, you will have to
revise the mathematics, but be careful: if you don't do it carefully
you may get an infinite loop as the OnResize method causes a resizing
that ends up calling it back.)
 
K

kurotsuke

I would create my own form that derives from Form, and override
OnResize(). Change OnResize() to check the window width and height and
change them if they are not a multiple of 50 pixels.

I tried it myself... the code looks like this:

protected override void OnResize(EventArgs e)
{
if (this.Width % 50 != 0)
{
this.Width = (int)(Math.Ceiling(this.Width / 50.0) * 50);
}
if (this.Height % 50 != 0)
{
this.Height = (int)(Math.Ceiling(this.Width / 50.0) * 50);
}
base.OnResize (e);
}

I'll warn you that the border redrawing while you're resizing at run
time looks really weird: you get a dual border flickering look, one
border where the mouse is actually dragging and another at the size the
window will really be when you release the mouse. However, it does
produce a window that has a height and width of a multiple of 50
pixels, and it allows you to resize it appropriately.

(Of course, if you take the border into account, etc, you will have to
revise the mathematics, but be careful: if you don't do it carefully
you may get an infinite loop as the OnResize method causes a resizing
that ends up calling it back.)


Thanks. I've tried it but unfortunately it's not what I intended. I
think that probably one should work with the WindowProcedure and the
WM_RESIZE message.

Any idea on how to do that?
Thanks.
 

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