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