Question about handling form resizing events.

J

JDeats

I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.

I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.

The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.

Can anyone recommend a proper way to do this?
 
N

Nicholas Paldino [.NET/C# MVP]

JDeats,

I would extend the Form class and override the WndProc method. In it,
when you come across the WM_SIZE notification (it's value is 5, btw) you can
fire an event to indicate that the resizing operation has been completed.
 
J

JDeats

JDeats,

I would extend the Form class and override the WndProc method. In it,
when you come across the WM_SIZE notification (it's value is 5, btw) you can
fire an event to indicate that the resizing operation has been completed.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.
I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.
The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.
Can anyone recommend a proper way to do this?

Thanks that put me on the right track.... Since I needed to not only
detect when resizing ended but also when the maximize restore buttons
are pressed I had to resort to overriding WndProc anyway.

For anyone else looking to call a method whenever the user resizes the
form by mouse drag or by way of maximize restore buttons here, this
does work. Spent bout five hours on this issue.... There are days I
absolutely hate the .NET Framework team for not putting in enough
event handlers. It's seems like there might be many instances where
someone would want an event to trap on when the Form size is changed
or when the form has been maximized/restored.

Here's the code.


private bool inSizing = false;

private void OnFormResizeEnd()
{
// code to execute after sizing is done goes here.
}

protected override void WndProc(ref
System.Windows.Forms.Message m)
{

const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
const int SC_RESTORE = 0xF120;
const int WM_SIZING = 0x214;
const int WM_EXITSIZEMOVE = 0x232;

if (m.Msg == WM_SIZING)
{
inSizing = true;
}

if (m.Msg == WM_EXITSIZEMOVE && inSizing) //
WM_EXITSIZEMOVE
{
OnFormResizeEnd();
inSizing = false;
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_MAXIMIZE)
{
OnFormResizeEnd();
inSizing = false;
}
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_RESTORE)
{
OnFormResizeEnd();
inSizing = false;
}
}

base.WndProc(ref m);

}
 
P

Peter Duniho

[...]
For anyone else looking to call a method whenever the user resizes the
form by mouse drag or by way of maximize restore buttons here, this
does work.

Thanks for posting the code for reference to others. One suggestion
though: IMHO, it would be better to use a switch() than to use several
independent if() statements. At the very least, use "else if()" for the
subsequent if() statements. :)
 
M

Mudhead

Or you can use the Form's SizeChanged event.

JDeats said:
JDeats,

I would extend the Form class and override the WndProc method. In
it,
when you come across the WM_SIZE notification (it's value is 5, btw) you
can
fire an event to indicate that the resizing operation has been completed.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have a WinForms based application written for the .NET Framework 2.0
and in this application I need to be able to be able to take some
action in code when the user finishes resizing the form.
I can easily create an event handler for the SizeChanged form level
event, the problem is if the user is using a mouse drag to resize the
form this event is firing every few milliseconds or what have you
until the user stops the drag process.
The approach I've taken is to create a Timer object and inside that
timer Tick event handler check the status of a flag that I'm setting
inside the form SizeChanged event handler. As I suspected, this a
sloppy approach that feels like a hack and it doesn't work without
bugs.
Can anyone recommend a proper way to do this?

Thanks that put me on the right track.... Since I needed to not only
detect when resizing ended but also when the maximize restore buttons
are pressed I had to resort to overriding WndProc anyway.

For anyone else looking to call a method whenever the user resizes the
form by mouse drag or by way of maximize restore buttons here, this
does work. Spent bout five hours on this issue.... There are days I
absolutely hate the .NET Framework team for not putting in enough
event handlers. It's seems like there might be many instances where
someone would want an event to trap on when the Form size is changed
or when the form has been maximized/restored.

Here's the code.


private bool inSizing = false;

private void OnFormResizeEnd()
{
// code to execute after sizing is done goes here.
}

protected override void WndProc(ref
System.Windows.Forms.Message m)
{

const int WM_SYSCOMMAND = 0x0112;
const int SC_MAXIMIZE = 0xF030;
const int SC_RESTORE = 0xF120;
const int WM_SIZING = 0x214;
const int WM_EXITSIZEMOVE = 0x232;

if (m.Msg == WM_SIZING)
{
inSizing = true;
}

if (m.Msg == WM_EXITSIZEMOVE && inSizing) //
WM_EXITSIZEMOVE
{
OnFormResizeEnd();
inSizing = false;
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_MAXIMIZE)
{
OnFormResizeEnd();
inSizing = false;
}
}

if (m.Msg == WM_SYSCOMMAND)
{
if ( (int)m.WParam == SC_RESTORE)
{
OnFormResizeEnd();
inSizing = false;
}
}

base.WndProc(ref m);

}
 

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