Windows窗体中背景图像的位置

时间:2010-09-14 09:21:25

标签: winforms

我将Windows窗体的BackgroundImage设置为200 x 200图像。表格是500 x 500.我希望图像锚定在表单的右下角。但是,我唯一可用的选项是BackgroundImageLayout属性 - 将此设置为“无”会导致图像锚定在左上角。我怎么能改变这个?

注意:我使用的是.NET 2.0

2 个答案:

答案 0 :(得分:8)

只需在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);
    }

答案 1 :(得分:2)

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