SizeChanged vs. Resize

  • Thread starter Thread starter saurabh
  • Start date Start date
S

saurabh

Can anybody give me a practical example which will highlight the difference
between SizeChanged and Resize Events of a form.

TIA,

--Saurabh
 
saurabh said:
Can anybody give me a practical example which will highlight the
difference between SizeChanged and Resize Events of a form.

Well, I tried... I put together the following sample to find out the
difference,
but couldn't find any.

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
public Form1()
{
this.Click += new EventHandler(Form1_Click);
this.Resize += new System.EventHandler(this.Form1_Resize);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Resize(object sender, System.EventArgs e)
{
Console.WriteLine("Resize");
}

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
Console.WriteLine("SizeChanged");
}

protected override void WndProc(ref Message m)
{
const int WM_SIZE = 0x0005;
const int WM_SIZING = 0x0214;
base.WndProc (ref m);
if (m.Msg == WM_SIZING)
{
Console.WriteLine("WM_SIZING");
}
else if (m.Msg == WM_SIZE)
{
Console.WriteLine("WM_SIZE");
}
}

private void Form1_Click(object sender, EventArgs e)
{
Size = new Size(Size.Width / 2, Size.Height / 2);
}
}

To make things more interesting, I decided to also throw in the
WM_SIZE and WM_SIZING messages. This is the behavior I found:

Form opens: WM_SIZE
User clicks on the frame but doesn't actually resize the form: WM_SIZING
(twice)
Form is resized by user: WM_SIZING, WM_SIZE, Resize, SizeChanged (in that
order)
Form is resized by setting the Size property: WM_SIZE, Resize, SizeChanged
(in that order)
Form is maximized or minimized: Resize, SizeChanged, WM_SIZE (in that order)
Form is closed: none

So, WM_SIZE and WM_SIZING differs, but Resize and SizeChanged appears
to behave identically. I ran these tests with .NET Framework 1.1 and the 2.0
beta,
both on Windows XP Pro SP 2.

- Magnus
 
Joep said:
... and what about setting the size in code as opposed to using the
mouse...

That's what I did in the last method, though "technically" it's still done
by the mouse since I handle the Click event (but that shouldn't matter).

One thing I haven't testet is changing the size in code other than by
setting
the Size property, e.g. using interop and SetWindowPlacement. So let's
change the code to:

using System.Runtime.InteropServices;

private void Form1_Click(object sender, EventArgs e)
{
WINDOWPLACEMENT placement;
placement.length =
System.Runtime.InteropServices.Marshal.SizeOf(typeof(WINDOWPLACEMENT));
GetWindowPlacement(this.Handle, out placement);
placement.rcNormalPosition.right /= 2;
placement.rcNormalPosition.bottom /= 2;
SetWindowPlacement(this.Handle, ref placement);
}

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}

[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINT ptMinPosition;
public POINT ptMaxPosition;
public RECT rcNormalPosition;
}

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}

[DllImport("user32.dll")]
public static extern bool GetWindowPlacement(IntPtr hWnd, out
WINDOWPLACEMENT lpwndpl);

[DllImport("user32.dll")]
public static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref
WINDOWPLACEMENT lpwndpl);

It turns out this behaves the same way as setting the Size property.
Not worth the trouble...

- Magnus
 
Back
Top