开发者

Position of BackgroundImage in Windows Form

开发者 https://www.devze.com 2023-01-16 05:49 出处:网络
I am setting the BackgroundImage of a Windows Form to a 200 x 200 image. The Form is 500 x 500. I want the image to be anchored in the bottom right corner of the form. However the only option availabl

I am setting the BackgroundImage of a Windows Form to a 200 x 200 image. The Form is 500 x 500. I want the image to be anchored in the bottom right corner of the form. However the only option available to me is the BackgroundImageLayout property - setting this to 'None' results in the image being anchored to 开发者_StackOverflowthe top left. How can I change this?

Note: I am using .NET 2.0


Just draw it yourself in the OnPaintBackground() method. Add the image to the resources (I called it BkgImage) and make the form code look like this:

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


You cannot do that with the BackgroundImageLayout.
However what you could do is add a PictureBox, anchor it to the bottom right and set it to the lowest z-value. This would result in pretty much the requested effect.

0

精彩评论

暂无评论...
验证码 换一张
取 消