1

gifアニメーションがあります。CompactFrameworkで作業しているので、このMSDNの例に従いました。

基本的な考え方は、画像にフレームを追加し、次のフレームで再描画しrectangleて、アニメーションを作成することです。アニメーションが私の画像のフルサイズ(240,320)ではなく、正方形で表示されていることを除いて、問題なく機能します。

これは私のコードです:

クラスAnimateCtl.cs:

public class AnimateCtl : System.Windows.Forms.Control
{
    Timer fTimer;
    int frameWidth = 240;
    int frameHeight = 320;
    int loopCount = 0;
    int loopCounter = 0;
    int frameCount;
    int currentFrame = 0;
    Graphics graphics;

    private Bitmap bitmap;
    public Bitmap Bitmap
    {
        get
        {
            return bitmap;
        }
        set
        {
            bitmap = value;
        }
    }

    private void Draw(int iframe)
    {
        //Calculate the left location of the drawing frame
        int XLocation = iframe * frameWidth;

        Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);

        //Draw image
        graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
    }

    public AnimateCtl()
    {
        //Cache the Graphics object
        graphics = this.CreateGraphics();
        //Instantiate the Timer
        fTimer = new System.Windows.Forms.Timer();
        //Hook up to the Timer's Tick event
        fTimer.Tick += new System.EventHandler(this.timer1_Tick);
    }

    /// <summary>
    /// Start animation
    /// </summary>
    /// <param name="frWidth"></param>
    /// <param name="DelayInterval"></param>
    /// <param name="LoopCount"></param>
    public void StartAnimation(int frWidth, int DelayInterval, int LoopCount)
    {

        frameWidth = frWidth;
        //How many times to loop
        loopCount = LoopCount;
        //Reset loop counter
        loopCounter = 0;
        //Calculate the frameCount
        frameCount = bitmap.Width / frameWidth;
        frameHeight = bitmap.Height;
        //Resize the control
        //this.Size(frameWidth, frameHeight);
        //Assign delay interval to the timer
        fTimer.Interval = DelayInterval;
        //Start the timer
        fTimer.Enabled = true;
    }

    private void timer1_Tick(object sender, System.EventArgs e)
    {
        if (loopCount == -1) //loop continuously
        {
            this.DrawFrame();
        }
        else
        {
            if (loopCount == loopCounter) //stop the animation
                fTimer.Enabled = false;
            else
                this.DrawFrame();
        }
    }

    private void DrawFrame()
    {
        if (currentFrame < frameCount - 1)
        {
            //move to the next frame
            currentFrame++;
        }
        else
        {
            //increment the loopCounter
            loopCounter++;
            currentFrame = 0;
        }
        Draw(currentFrame);
    }

そして私のForm1:

public partial class Form1 : Form
{
    AnimateCtl animCtl;

    public Form1()
    {
        InitializeComponent();

        //Instantiate control
        animCtl = new AnimateCtl();
        //Assign the Bitmap from the image file
        animCtl.Bitmap = new Bitmap(@"\Program Files\Animacion\Animacion-Frames.jpg");
        //Set the location
        animCtl.Location = new Point(0, 0);
        //Add to control to the Form
        this.Controls.Add(animCtl);

        animCtl.StartAnimation(240, 100, 3);
    }        
}

長方形が正しい寸法で描画されないのはなぜですか?助けてくれてありがとう!

4

1 に答える 1

1

Drawメソッドを削除し、次のようにDrawFrameを更新します

    private void DrawFrame() {
        if (currentFrame < frameCount - 1) {
            currentFrame++;
        } else {
            loopCounter++;
            currentFrame = 0;
        }
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);

        int XLocation = currentFrame * frameWidth;
        Rectangle rect = new Rectangle(XLocation, 0, frameWidth, frameHeight);
        e.Graphics.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
    }

this.CreateGraphics()は使用しないでください。OnPaintメソッドを上書きするのが正しい方法です。this.Invalidate();を呼び出した後、OnPaintは常に起動します。

this.CreateGraphics()を使用すると、現在のグラフィックサイズしか取得できませんが、フォームが表示された後、コントロールサイズが変更された可能性があります。

于 2012-10-15T15:05:20.227 に答える