设置按钮背景/图像动画GIF

时间:2012-06-12 12:57:25

标签: c# .net button controls animated-gif

我有一个.gif文件,我希望将其用作按钮的背景图像。我想要这样做的主要原因(因为我知道你们中的一些人会想知道我为什么要这样做)是因为,一个按钮与一个类的实例有关,我想监视这些类。当一个类(因此按钮)进入“警报”状态时,我希望我使用这些动画gif。我试过了;

mainUI.Controls["btn" + device.deviceButtonNumber].BackgroundImage = (System.Drawing.Image)Properties.Resources.red_orange;

但据我所知,.BackgroundImage不支持GIF动画。我已经在stackoverflow中尝试了一些建议,并且这些建议似乎对我有用,例如将BackgroundImageLayout设置为居中等等。

我认为作为最后的手段,我可​​以让它手动循环图像,就好像它是一个gif,但这会创造更多的工作,我想保持简单。有什么建议吗?

编辑:对不起,忘了添加,这是在Winforms。

编辑2:

例如,

。当我动态创建我的按钮时,就像这样;

  Button btnAdd = new Button();
  btnAdd.Text = mDevices[i].deviceDescription;
  btnAdd.Location = new Point(mDevices[i].deviceXPos.Value, mDevices[i].deviceYPos.Value);

我仍然可以访问:

 btnAdd.Image  <- note i can access image.

然后,当我稍后从另一个线程修改它们时,我发现它们就是这样;

 mainUI.Controls["btn" + device.deviceButtonNumber].text = blah blah
 mainUI.Controls["btn" + device.deviceButtonNumber].Location = blah blah

但是我无法访问;

 mainUI.Controls["btn" + device.deviceButtonNumber].Image

2 个答案:

答案 0 :(得分:1)

/// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();

            //  this.button1.Image = new Bitmap(@"C:\images\dance.gif");

            // if you import the local resource 
            this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
            this.button1.ImageAlign = System.Drawing.ContentAlignment.TopRight;
            this.button1.Location = new System.Drawing.Point(109, 73);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(523, 437);
            this.button1.TabIndex = 1;
            this.button1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 17F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(807, 640);
            this.Controls.Add(this.button1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "Form1";
            this.Text = "Image In Button Control";
            this.ResumeLayout(false);

        }

答案 1 :(得分:0)

我建议使用PictureBox代替ButtonPictureBox支持.gif图片以及click事件