C# API-動畫視窗

coolhe發表於2009-08-03

#region  -- 類方法說明
/* 說明:
*
* 1.名稱空間: Apc_AniWindow
*    類名    : AniWindow
*
*   2.宣告: AniWindow a=new AniWindow(this.Handle,6,1,this);
*
* 3.引數說明:AniWindow(視窗控制程式碼,動畫樣式,開啟或關閉標誌,例項表單);
*    視窗控制程式碼: this.Handle
*    動畫樣式: 0 -> 普通顯示
*     1 1 -> 從左向右顯示
*      2 -> 從右向左顯示
*      3 -> 從上到下顯示
*      4 -> 從下到上顯示
*      5 -> 透明漸變顯示
*      6 -> 從中間向四周
*      7 -> 左上角伸展
*      8 -> 左下角伸展
*      9 -> 右上角伸展
*      10 -> 右下角伸展
*    開關標誌: 0為關閉視窗 1為開啟視窗
*    例項表單: 為了去除Label的BUG, 取值 this
*/
#endregion

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PhyExam.Global
{
    public class AniWindow
    {
        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
        private const int AW_HOR_POSITIVE = 0x0001;
        private const int AW_HOR_NEGATIVE = 0x0002;
        private const int AW_VER_POSITIVE = 0x0004;
        private const int AW_VER_NEGATIVE = 0x0008;
        private const int AW_CENTER = 0x0010;
        private const int AW_HIDE = 0x10000;
        private const int AW_ACTIVATE = 0x20000;
        private const int AW_SLIDE = 0x40000;
        private const int AW_BLEND = 0x80000;
        private int CloseOpen = 0x20000;

        public AniWindow(IntPtr hwnd, int dwFlags, int CloseOrOpen, System.Windows.Forms.Form. myform)
        {
            try
            {
                if (CloseOrOpen == 1)
                {
                    foreach (System.Windows.Forms.Control mycontrol in myform.Controls)
                    {
                        string m = mycontrol.GetType().ToString();
                        m = m.Substring(m.Length - 5);
                        if (m == "Label")
                        {
                            mycontrol.Visible = false;     //這裡是在動畫效果之前,把表單上可視的LABEL設為不可視
                        }
                    }
                }
                //開啟or關閉 0是關閉 1是開啟
                if (CloseOrOpen == 0) { CloseOpen = 0x10000; }
                if (dwFlags == 100)
                {
                    int zz = 10;
                    Random a = new Random();
                    dwFlags = (int)a.Next(zz);
                }


                switch (dwFlags)
                {
                    case 0://普通顯示
                        AnimateWindow(hwnd, 200, AW_ACTIVATE);
                        break;
                    case 1://從左向右顯示
                        AnimateWindow(hwnd, 200, AW_HOR_POSITIVE | CloseOpen);
                        break;
                    case 2://從右向左顯示
                        AnimateWindow(hwnd, 200, AW_HOR_NEGATIVE | CloseOpen);
                        break;
                    case 3://從上到下顯示
                        AnimateWindow(hwnd, 200, AW_VER_POSITIVE | CloseOpen);
                        break;
                    case 4://從下到上顯示
                        AnimateWindow(hwnd, 200, AW_VER_NEGATIVE | CloseOpen);
                        break;
                    case 5://透明漸變顯示
                        AnimateWindow(hwnd, 1500, AW_BLEND | CloseOpen);
                        break;
                    case 6://從中間向四周
                        AnimateWindow(hwnd, 500, AW_CENTER | CloseOpen);
                        break;
                    case 7://左上角伸展
                        AnimateWindow(hwnd, 200, AW_SLIDE | AW_HOR_POSITIVE | AW_VER_POSITIVE | CloseOpen);
                        break;
                    case 8://左下角伸展
                        AnimateWindow(hwnd, 200, AW_SLIDE | AW_HOR_POSITIVE | AW_VER_NEGATIVE | CloseOpen);
                        break;
                    case 9://右上角伸展
                        AnimateWindow(hwnd, 200, AW_SLIDE | AW_HOR_NEGATIVE | AW_VER_POSITIVE | CloseOpen);
                        break;
                    case 10://右下角伸展
                        AnimateWindow(hwnd, 200, AW_SLIDE | AW_HOR_NEGATIVE | AW_VER_NEGATIVE | CloseOpen);
                        break;
                }
                if (CloseOrOpen == 1)
                {
                    foreach (System.Windows.Forms.Control mycontrol in myform.Controls)
                    {
                        string m = mycontrol.GetType().ToString();
                        m = m.Substring(m.Length - 5);
                        if (m == "Label")
                        {
                            mycontrol.Visible = true; //這裡恢復LABEL的可視.
                        }
                    }
                }
            }
            catch { }
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3090/viewspace-611191/,如需轉載,請註明出處,否則將追究法律責任。

相關文章