2010-09-14 17 views
5

Sto impostando BackgroundImage di un Windows Form su un'immagine 200 x 200. Il modulo è 500 x 500. Voglio che l'immagine sia ancorata nell'angolo in basso a destra del modulo. Tuttavia, l'unica opzione disponibile per me è la proprietà BackgroundImageLayout - impostandola su "Nessuno", l'immagine verrà ancorata in alto a sinistra. Come posso cambiare questo?Posizione di BackgroundImage in Windows Form

Nota: io sto usando .NET 2.0

+1

Non hai questa opzione in WinForms. [Enumerazione ImageLayout] (http://msdn.microsoft.com/en-us/library/system.windows.forms.imagelayout.aspx) su MSDN. – ChrisF

risposta

7

Basta disegnarlo autonomamente nel metodo OnPaintBackground(). Aggiungi l'immagine alle risorse (l'ho chiamata BkgImage) e rendi il codice del modulo simile al seguente:

public Form1() { 
     InitializeComponent(); 
     backgroundImage = Properties.Resources.BkgImage; 
     this.DoubleBuffered = true; 
     this.SetStyle(ControlStyles.ResizeRedraw, true); 
    } 
    private Image backgroundImage; 

    protected override void OnPaintBackground(PaintEventArgs e) { 
     base.OnPaintBackground(e); 
     var rc = new Rectangle(this.ClientSize.Width - backgroundImage.Width, 
      this.ClientSize.Height - backgroundImage.Height, 
      backgroundImage.Width, backgroundImage.Height); 
     e.Graphics.DrawImage(backgroundImage, rc); 
    } 
2

Non si può fare che con il BackgroundImageLayout.
Tuttavia, ciò che si potrebbe fare è aggiungere un PictureBox, ancorarlo in basso a destra e impostarlo sul valore z più basso. Ciò comporterebbe praticamente l'effetto richiesto.

+0

Ciò comporterà l'immagine di sfondo? Ad esempio, altri controlli come i pulsanti appariranno "in cima" alla finestra? –

+0

Sì. [15 caratteri] –