Windows 窗体中背景图像的位置 [英] Position of BackgroundImage in Windows Form
问题描述
我正在将 Windows 窗体的 BackgroundImage
设置为 200 x 200 图像.表单为 500 x 500.我希望图像锚定在表单的右下角.然而,我唯一可用的选项是 BackgroundImageLayout
属性 - 将其设置为无"会导致图像锚定在左上角.我该如何更改?
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?
注意:我使用的是 .NET 2.0
Note: I am using .NET 2.0
推荐答案
只需在 OnPaintBackground() 方法中自己绘制即可.将图片添加到资源中(我叫它BkgImage)并使表单代码如下所示:
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);
}
这篇关于Windows 窗体中背景图像的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!