要在Winform中移动窗口背景图片位置,可以使用以下步骤:

  1. 在窗体的属性中找到BackgroundImageLayout属性,将其设置为'None'。

  2. 在窗体的构造函数中添加以下代码:

this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  1. 在窗体的Paint事件中添加以下代码:
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.DrawImage(Properties.Resources.background, new Rectangle(0, 0, this.Width, this.Height));
}

其中,Properties.Resources.background是窗体背景图片的资源文件。

  1. 在窗体的MouseMove事件中添加以下代码:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        this.Left += e.X - mouse_offset.X;
        this.Top += e.Y - mouse_offset.Y;
    }
}

其中,mouse_offset是一个Point类型的变量,用于记录鼠标在窗体上的偏移量。

  1. 在窗体的MouseDown事件中添加以下代码:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        mouse_offset = new Point(e.X, e.Y);
    }
}

这样,当鼠标左键按下时,就记录下当前鼠标在窗体上的位置。

  1. 最后,在窗体的Load事件中添加以下代码:
private void Form1_Load(object sender, EventArgs e)
{
    this.FormBorderStyle = FormBorderStyle.None;
}

这样就可以去掉窗体的边框,使得窗体能够被移动。

Winform 移动窗口背景图片位置教程

原文地址: https://www.cveoy.top/t/topic/ornC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录