C#實現動態窗體

1﹑使用動態窗體﹐其實也不難﹐拷貝以下代碼至你的工程中即可﹕

2﹑源碼如下﹕

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace frmAnimitation
{
 /// <summary>
 ///frmAnimitation 的摘要描述。
 /// </summary>
 public class frmAnimitation : System.Windows.Forms.Form
 {
        private System.ComponentModel.IContainer components;
        private System.Windows.Forms.ImageList imageList1;
        private System.Windows.Forms.Timer timer1;

        private int m_nIndex = 0;
        public Bitmap NewBitmap;
        public Bitmap OldBitmap;
        private Boolean m_bLooping = true;

        const int AW_BLEND = 0x80000;
  const int AW_HOR_POSITIVE = 0x0001;
  const int AW_HOR_NEGATIVE = 0x0002;
  const int AW_VER_POSITIVE = 0x0004;
  const int AW_VER_NEGATIVE = 0x0008;
  const int AW_CENTER = 0x0010;
  const int AW_HIDE = 0x10000;
  const int AW_ACTIVATE = 0x20000;
        private System.Windows.Forms.Timer timer2;
  const int AW_SLIDE = 0x40000;

  public frmAnimitation()
  {
   //
   // Windows Form 設計工具支援的必要項
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
   //
            this.timer1.Start();
  }

  /// <summary>
  /// 清除任何使用中的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form 設計工具產生的程式碼
  /// <summary>
  /// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
  /// 這個方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
            this.components = new System.ComponentModel.Container();
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmAnimitation));
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.timer2 = new System.Windows.Forms.Timer(this.components);
            //
            // imageList1
            //
            this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            //
            // timer1
            //
            this.timer1.Interval = 500;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // timer2
            //
            this.timer2.Enabled = true;
            this.timer2.Interval = 10;
            this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
            //
            // frmAnimitation
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(248, 125);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmAnimitation";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "動畫窗體";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.frmAnimitation_Closing);
            this.Load += new System.EventHandler(this.frmAnimitation_Load);

        }
  #endregion

  /// <summary>
  /// 應用程式的主進入點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new frmAnimitation());
  }

        [System.Runtime.InteropServices.DllImport("user32")]
  private static extern bool AnimateWindow(IntPtr hwnd,int dwTime, int dwFlags);

        private void frmAnimitation_Load(object sender, System.EventArgs e)
        {
   AnimateWindow(this.Handle,1000, AW_CENTER | AW_ACTIVATE);
            Point p = new Point(0,370);
            this.DesktopLocation = p ;
        }

        private void frmAnimitation_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
   AnimateWindow(this.Handle, 1000, AW_SLIDE | AW_HIDE | AW_VER_NEGATIVE);
        } 
 
        protected override void OnPaint ( System.Windows.Forms.PaintEventArgs pe )
        {
            Graphics g = pe.Graphics ;
            Rectangle rect = this.ClientRectangle;
            try
            {
                if (imageList1 == null)
                {
                    g.FillRectangle(Brushes.White, rect);    // if no imagelist,  just paint an empty white rectangle
                    return;
                }
                if (imageList1.Images.Count > 0)   // make sure there are images in the list
                {
                    //NewBitmap = imageList1.GetBitmap(m_nIndex);  // get the bitmap from the image list
                    NewBitmap = (Bitmap)imageList1.Images[m_nIndex];
                    g.DrawImage(NewBitmap, 0, 0, rect.Width, rect.Height); // Draw the bitmap to the screen to the size of the control
                    if (OldBitmap != null)  
                    {
                        OldBitmap.Dispose();
                    }
  
                    OldBitmap = NewBitmap;    // save latest bitmap for double buffering in the OnPaintBackground routine
                }
                else
                {
                    g.FillRectangle(Brushes.White, rect);    // if no images,  just paint an empty white rectangle
                }
            }
            catch (Exception exx)
            {
                System.Console.WriteLine(exx.Message.ToString());
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            Graphics g = pevent.Graphics; 
            Rectangle rect = this.ClientRectangle; 
            if (OldBitmap != null) 
            {
                g.DrawImage(OldBitmap, 0, 0, rect.Width, rect.Height);  // Draw the last image as background, instead of allowing
            }                                                                                                 // the base class OnPaintBackground to be called
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            if (imageList1.Images.Count == 0)
            {
                return; // precondition, make sure there are images in the list
            }
            this.Invalidate();  // force the control to repaint

            m_nIndex++;  // increment to the next image in the list

            if (m_nIndex >= imageList1.Images.Count)
            {
                if (m_bLooping == true)
                {
                    m_nIndex = 0;   // if we are looping, restart the index into the image list
                }
                else
                {
                    timer1.Stop();  // not looping,  just stop the timer
                }
            }
        }

        private void timer2_Tick(object sender, System.EventArgs e)
        {
            Point p = new Point(this.DesktopLocation.X + 1, this.DesktopLocation.Y);
            this.DesktopLocation = p;
            if (p.X == this.DesktopBounds.Width)
            {
                Point p2 = new Point(0,370);
                this.DesktopLocation = p2;
        }
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值