I have an MDI form with a centered background image.
Each time the user changes the size or state of the form, the image isn't updated at all. It remains in the old place (not centered any more) and is even lost when the form is made too small.
How can this situation correctly be handled?
Do I really have to call "this.Refresh()" in all event handlers related to form size and state?
Application is realized in .net 3.5SP1 C# with Windows.Forms.
Unfortunately there doesn't seem to be a super-quick way to do this, but the following is my solution and at least doesn't seem to rely on coincidences.
In the mdi constructor, handle resizing:
this.ResizeEnd += delegate { this.Refresh(); };
And then this override to handle maximize/restore events
protected override void WndProc(ref Message m)
{
if (m.Msg == Win32.WM_SYSCOMMAND)
{
int test = m.WParam.ToInt32() & 0xFFF0;
switch (test)
{
case Win32.SC_MAXIMIZE:
case Win32.SC_RESTORE:
this.Invalidate(); // used to keep background image centered
break;
}
}
base.WndProc(ref m);
}
Constant values are defined as:
public const int WM_SYSCOMMAND = 0x0112;
//wparam for WM_SYSCOMMAND should be one of these after masking with 0xFFF0:
public const int SC_RESTORE = 0xF120;
public const int SC_MINIMIZE = 0xF020;
public const int SC_MAXIMIZE = 0xF030;