BackgroundImage在Windows窗体中的位置

Pet*_*lly 7 winforms

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

注意:我使用的是.NET 2.0

Han*_*ant 9

只需在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);
    }
Run Code Online (Sandbox Code Playgroud)