Question

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.

Was it helpful?

Solution

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;

OTHER TIPS

You could do all of that, or you could just put a me.refresh in the MDI's resize event.

Call PositionContainersToParentMiddle method in Resize event of your MDI form. I have not tested it, but it should work. You might have to put conditions in Resize event to stop Image location change at every resize.

   private void YourMDI_Resize(object sender, EventArgs e)
    {
        PositionContainersToParentMiddle();
    }

    private void PositionContainersToParentMiddle()
    {
        int iInitX = (ParentOfImage.Size.Width - YourImage.Size.Width) / 2;
        int iInitY = ( ParentOfImage.Location.Y + YourImage.Size.Height ) / 2;
        YourImage.Location = new Point( iInitX, iInitY ) ;

    }
Private Sub YourMDIFormName_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Resize
    Me.BackgroundImage = My.Resources.YourBackgroundImageName
    Me.Refresh()
 End Sub
    private void Change_BackgroundImage(string _path)
  {
    string imagepath = _path;
    System.IO.FileStream fs;
 // MDI Form image background layout change her`enter code here`e     
 //(Remember control imagebakground layout take default form background layount )
          this.BackgroundImageLayout = ImageLayout.Center;
            // Checking File exists if yes go --->
            if (System.IO.File.Exists(imagepath))
            {
              // Read Image file
              fs = System.IO.File.OpenRead(imagepath);
                fs.Position = 0;
                // Change MDI From back ground picture
                foreach (Control ctl in this.Controls)
                {
                    if (ctl is MdiClient)
                    {
                      //ctl.BackColor = Color.AntiqueWhite;
                       ctl.BackColor = Color.FromArgb(31, 26, 23);
                       ctl.BackgroundImage = System.Drawing.Image.FromStream(fs);
                      break;
                       }
                  }
             }
          }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top