Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
590 views
in Technique[技术] by (71.8m points)

winforms - Position of BackgroundImage in Windows Form

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 the top left. How can I change this?

Note: I am using .NET 2.0

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...