在组合框上绘制动画

时间:2012-08-08 12:27:35

标签: c# winforms animation combobox picturebox

关于如何在组合框中绘制图像,有很多已经回答的问题和示例。但我没有找到任何如何在组合框内绘制动画的例子。

我使用的gif动画是(它是透明的):
enter image description here

我想要达到的结果是这样的想法:
enter image description here

我正在使用Windows Forms和.Net 3.5 我想到的所有实现这一目标的方法是:
1.在ComboBox的DrawItem处理程序中使用Graphics.DrawImage。但是图像是静态绘制的,没有动画 2.使用PictureBox显示动画,然后以某种方式调整大小并放置在ComboBox上。

对于第二个灵魂,我使用了以下代码:

pictureBox1 = new PictureBox();
pictureBox1.Image = Resource.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
//3 is used just for try to fit image into "white" area of ComboBox
pictureBox1.ClientSize = new Size(comboBox1.Size.Height-3, comboBox1.Size.Height-3);
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Dock = DockStyle.Left;
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;

但结果我得到了这个:
enter image description here
它是动画的,但是在ComboBox边缘绘制了图片框,它看起来很糟糕。

那么,有人可以给我一些建议或一些帮助吗?

谢谢。

修改
我的最终解决方案有效:

pictureBox1 = new PictureBox();
pictureBox1.Image = Resource1.myImage;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.ClientSize = new Size(comboBox1.Size.Height - SystemInformation.Border3DSize.Height,  comboBox1.Size.Height - (2 * SystemInformation.Border3DSize.Height));
pictureBox1.BackColor = System.Drawing.Color.Transparent;
pictureBox1.Location = new Point(SystemInformation.Border3DSize.Width, SystemInformation.Border3DSize.Height);
pictureBox1.Parent = this.comboBox1;
pictureBox1.Enabled = true;
pictureBox1.Visible = true;

谢谢大家!你帮了我很多忙!

3 个答案:

答案 0 :(得分:3)

试试这个:

            pictureBox1 = new PictureBox();
            pictureBox1.Image = Resource.myImage;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            //2 is used just for try to fit image into "white" area of ComboBox
            pictureBox1.ClientSize = new Size(comboBox1.Size.Height - 2, comboBox1.Size.Height - 2);
            pictureBox1.BackColor = System.Drawing.Color.Transparent;
            pictureBox1.Left = 1;
            pictureBox1.Top = 1;
            pictureBox1.Parent = this.comboBox1;
            pictureBox1.Enabled = true;
            pictureBox1.Visible = true;

答案 1 :(得分:1)

如果你将组合框的大小设置为pictureBox1.Size = new Size(comboBox1.ItemHeight, comboBox1.ItemHeight),那么它可能会很小太小

或者将高度和宽度设置为2*SystemInformation.3DBorderSize

答案 2 :(得分:1)

删除设置“Dock”属性的代码。设置此选项会导致布局管理器忽略大小/位置设置。

而是将Size属性和Location属性设置为特定值。

类似的东西:

pictureBox3.Size = new Size(comboBox1.Size.Height-3, comboBox1.Size.Height-3);
pictureBox3.Location = new Point(0, 3);

您可能需要调整这些以获得所需的接触位置。