窗体背景图片在 Windows 窗体中的位置

8
我正在将 Windows 窗体的 BackgroundImage 设置为 200 x 200 的图像。该窗体大小为 500 x 500。我希望图像锚定在窗体的右下角。然而,唯一可用的选项是 BackgroundImageLayout 属性 - 将其设置为“None”会导致图像被锚定在左上角。我该如何更改呢?
注意:我正在使用 .NET 2.0。

1
你在WinForms中没有那个选项。ImageLayout 枚举 详见MSDN。 - ChrisF
2个回答

10

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

3

使用BackgroundImageLayout无法做到这一点。
不过,您可以添加一个PictureBox,将其锚定到右下角并将其设置为最低的z值。这将基本上实现所需的效果。


这会导致背景图片吗?也就是说,其他控件(如按钮)会出现在图片框的“上方”吗? - Peter Kelly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接