C#调用Windows的API实现窗体动画
dawn 人气:0在VF、VB、PB的应用中,有些无法通过语言工具本身来完成的或者做得不理想的功能,我们会考虑通过Windows的API来完成。
在那个年代,熟悉API是一个编程者的基本功。那个时候网络信息也不发达,很多资料只能考购买书籍或者自己摸索来获取,每次掌握一个新的API应用内心里都有点小激动。
Powerbuilder数据功能强大,但是界面功能尤其是动画功能比较弱,通过调用Windows的API实现窗体动画比较容易。
声明API调用:
function boolean AnimateWindow ( long hwnd, long dwtime, long dwflags ) library "user32"
声明常量:
constant long AW_HOR_POSITIVE = 1 constant long AW_HOR_NEGATIVE = 2 constant long AW_VER_POSITIVE = 4 constant long AW_VER_NEGATIVE = 8 constant long AW_CENTER = 16 constant long AW_HIDE = 65526 constant long AW_ACTIVATE = 131072 conDAT* ? ?stant long AW_SLIDE = 262144 constant long AW_BLEND = 524288
在窗体的open事件中调用:
long ll_handle ll_handle = Handle ( This ) Randomize ( 0 ) Choose Case rand ( 6 ) Case 1 AnimateWindow ( ll_handle, 1000, AW_SLIDE + AW_VER_POSITIVE + AW_ACTIVATE ) Case 2 AnimateWindow ( ll_handle, 1000, AW_SLIDE + AW_VER_NEGATIVE+ AW_ACTIVATE ) Case 3 AnimateWindow ( ll_handle, 1000, AW_SLIDE + AW_HOR_POSITIVE + AW_ACTIVATE ) Case 4 AnimateWindow ( ll_handle, 1000, AW_SLIDE + AW_HOR_NEGATIVE + AW_ACTIVATE ) Case 6,5 AnimateWindow ( ll_handle, 1000, AW_SLIDE + AW_CENTER + AW_ACTIVATE ) End Choose
在C#中调用Windows的API来实现动画也一样容易。
引用:
using System.Runtime.InteropServices;
定义常量:
public const Int32 AW_HOR_POSITIVE = 0x00000001; public const Int32 AW_HOR_NEGATIVE = 0x00000002; public const Int32 AW_VER_POSITIVE = 0x00000004; public const Int32 AW_VER_NEGATIVE = 0x00000008; public const Int32 AW_CENTER = 0x00000010; public const Int32 AW_HIDE = 0x00010000; public const Int32 AW_ACTIVATE = 0x00020000; public const Int32 AW_SLIDE = 0x00040000; public const Int32 AW_BLEND = 0x00080000;
定义调用:
[DllImportAttribute("user32.dll")] private static extern bool AnimateWindow(IntPtr hwnd,int dwTime,int dwFlags);
调用:
private void Form1_Load(object sender, EventArgs e) { //this.FormBorderStyle =0 ; Random rand = new Random(); long LL = rand.Next(1,6); label1.Text = "动画演示:"+LL.ToString(); switch (LL) { case 1: AnimateWindow(this.Handle, 1000, AW_SLIDE + AW_VER_POSITIVE + AW_ACTIVATE); break; case 2: AnimateWindow(this.Handle, 1000, AW_SLIDE + AW_VER_NEGATIVE + AW_ACTIVATE); break; case 3: AnimateWindow(this.Handle, 1000, AW_SLIDE + AW_HOR_POSITIVE + AW_ACTIVATE); break; case 4: AnimateWindow(this.Handle, 1000, AW_SLIDE + AW_HOR_NEGATIVE + AW_ACTIVATE); break; case 5: AnimateWindow(this.Handle, 1000, AW_SLIDE + AW_CENTER + AW_ACTIVATE); break; case 6: AnimateWindow(this.Handle, 1000, AW_SLIDE + AW_CENTER + AW_ACTIVATE); break; } }
效果图
加载全部内容