Windows窗体中BackgroundImage的位置形式

Windows窗体中BackgroundImage的位置形式

问题描述:

我将Windows窗体的BackgroundImage设置为200 x 200图像。该表单为500 x 500.我希望图像可以锚定在表单的右下角。不过,唯一可用的选项是BackgroundImageLayout属性 - 将其设置为“无”会导致图像锚定在左上角。我该如何改变这一点?Windows窗体中BackgroundImage的位置形式

注:我使用.NET 2.0

+1

WinForms中没有该选项。 MSDN上的[ImageLayout Enumeration](http://msdn.microsoft.com/zh-cn/library/system.windows.forms.imagelayout.aspx)。 – ChrisF 2010-09-14 09:26:14

只需在OnPaintBackground()方法中自己绘制它即可。将图像添加到资源(我称之为BkgImage),并使表单代码如下所示:

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

你不能这样做,与BackgroundImageLayout。
但是,你可以做的是添加一个PictureBox,将其锚定到右下角并将其设置为最低的z值。这将导致几乎所需的效果。

+0

会导致背景图像?即像按钮之类的其他控件是否出现在图片框的“顶部”? – 2010-09-14 09:50:04

+0

是的。 [15个字符] – 2010-09-14 17:04:45