Domanda

Ho un modulo MDI con un'immagine di sfondo centrata.
Ogni volta che l'utente modifica le dimensioni o lo stato del modulo, l'immagine non viene affatto aggiornata. Rimane nel vecchio posto (non più centrato) e si perde persino quando il modulo viene reso troppo piccolo.

Come può essere gestita correttamente questa situazione?
Devo davvero chiamare " this.Refresh () " in tutti i gestori di eventi relativi alle dimensioni e allo stato del modulo?

L'applicazione è realizzata in .net 3.5SP1 C # con Windows.Forms.

È stato utile?

Soluzione

Sfortunatamente non sembra esserci un modo super rapido per farlo, ma la seguente è la mia soluzione e almeno non sembra fare affidamento su coincidenze.

Nel costruttore mdi, gestisci il ridimensionamento:

this.ResizeEnd += delegate { this.Refresh(); };

E quindi questa sostituzione per gestire gli eventi di massimizzazione / ripristino

    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);
    }

I valori costanti sono definiti come:

    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;

Altri suggerimenti

Potresti fare tutto questo, oppure puoi semplicemente inserire un me.refresh nell'evento ridimensionare dell'MDI.

Chiama il metodo PositionContainersToParentMiddle nell'evento Resize del modulo MDI. Non l'ho provato, ma dovrebbe funzionare. Potrebbe essere necessario inserire condizioni nell'evento Ridimensiona per interrompere la modifica della posizione dell'immagine ad ogni ridimensionamento.

   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;
                       }
                  }
             }
          }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top