C# 更改多个控件的背景图像

本文关键字:背景 图像 控件 | 更新日期: 2023-09-27 18:33:29

在 C# 表单中,我创建了一个函数,该函数一旦被调用,就会创建一个具有所需图像、大小和位置的图片框:

private void NewPic(string nName, int locX, int locY, int SizX, int SizY, Image Img)
{
    PictureBox Pic = new PictureBox();
    Pic.Name = nName; Pic.Image = Img;
    Pic.BackColor = Color.Transparent;
    Pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    Pic.Size = new System.Drawing.Size(SizX, SizY);
    Pic.Location = new System.Drawing.Point(locX, locY);
    Controls.Add(Pic);
    Pic.Click += new EventHandler(Pic_Click);
}

现在,当我需要图片时,我就这样做:

NewPic("FIRE", 32, 100, 120, 120, Properties.Resources.Image);
问题是,在单击事件中,当我单击图片框时,我希望

它更改其背景图像,但是,如果我单击其他图片框,我希望最后一个图片框自行重置它:

private void Pic_Click(object sender, System.EventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    switch (pb.Name)
    {
        case "1": 
            pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
            pb.BackgroundImageLayout = ImageLayout.Stretch;
                //INSERT CODE HERE: to remove from other if it has
            break;
        case "2":
            pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
            pb.BackgroundImageLayout = ImageLayout.Stretch;
                //INSERT CODE HERE: to remove from other if it has
            break;
        }
     }

我需要可以应用于多个图片框/对象的代码,而不仅仅是两个

C# 更改多个控件的背景图像

我认为您还需要存储最后一个图片框的图像

PictureBox _lastPictureBox = null;
Image _lastPictureBoxImage = null;
private void Pic_Click(object sender, System.EventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    if (_lastPictureBox != null) 
    {
      // update the previous one, eg:
       _lastPictureBox.BackgroundImage = _lastPictureBoxImage;
    }
    // now set it to the current one:
   _lastPictureBox = pb;
   _lastPictureBoxImage = pb.Image;
   switch (pb.Name)
   {
     case "1": 
       pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
       pb.BackgroundImageLayout = ImageLayout.Stretch;
    break;
    case "2":
      pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
      pb.BackgroundImageLayout = ImageLayout.Stretch;
    break;
  }

}

最简单的

方法是向表单添加一个成员,该成员将跟踪以前单击的 PictureBox:

PictureBox _lastPictureBox = null;

在处理程序中,检查_lastPictureBox是否有值,并根据需要更新它:

private void Pic_Click(object sender, System.EventArgs e)
{
    PictureBox pb = (PictureBox)sender;
    if (_lastPictureBox != null) 
    {
        // update the previous one, eg:
        _lastPictureBox.BackgroundImage = Properties.Resources.FirstImg;
    }
    // now set it to the current one:
    _lastPictureBox = pb;
    switch (pb.Name)
    {
    case "1": 
        pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
        pb.BackgroundImageLayout = ImageLayout.Stretch;
        break;
    case "2":
        pb.BackgroundImage = Properties.Resources.OtherImg; //creates the background
        pb.BackgroundImageLayout = ImageLayout.Stretch;
        break;
    }
 }

如果我理解正确,你需要一个全局变量

PictureBox lastbox;

然后你可以插入这个代码:

lastbox.Image = Properties.Resources.Image;
lasbox = pb;