博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Fade Form Effect With the AnimateWindow API Function
阅读量:5342 次
发布时间:2019-06-15

本文共 2969 字,大约阅读时间需要 9 分钟。

http://www.codeproject.com/KB/dialog/FadePage.aspx?msg=2771956
The Motivation

I required a fade/slide effect to replace a previous thread based approach that was using deprecated methods.

When consulting the oracle (Google), I was able to find more than a few articles that use opacity or utility type implementation of the AnimateWindow function, but I was looking for a solution to hide the functionality and keep things as simple as possible, so I created FadeForm.

The Objective

Write a class that implements/wraps the AnimateWindow function which can easily be added to existing code.

The Code

The FadePage Object

Create a new class that derives from System.Windows.Forms.Form which will allow us to extend the behavior of a standard form with the AnimateWindow method.

Collapse |
public abstract class FadeForm : Form {}

Next, create a member variable that will determine if we fade or slide, and a few constructors to assign the variable, with the default being the slide effect.

Collapse |
public abstract class FadeForm : Form {     private bool _UseSlideAnimation;     public FadeForm() : this(false) { }     public FadeForm(bool useSlideAnimation)     {         _UseSlideAnimation = useSlideAnimation;     } }

The AnimateWindow Implementation

This is really the guts of the class that will control the effects. The logic is based on a , and modified to hide the actual implementation.

Declare the integers that define the effects and the functions internally to the fade page class.

Collapse |
const int AW_HIDE = 0X10000; const int AW_ACTIVATE = 0X20000; const int AW_HOR_POSITIVE = 0X1; const int AW_HOR_NEGATIVE = 0X2; const int AW_SLIDE = 0X40000; const int AW_BLEND = 0X80000; [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern int AnimateWindow (IntPtr hwand, int dwTime, int dwFlags);

Finally, we override the load and close methods of the Form class to perform the automation.

Collapse |
protected override void OnLoad(EventArgs e) {     base.OnLoad(e);     AnimateWindow(this.Handle, 1000, AW_ACTIVATE | (_UseSlideAnimation ?                    AW_HOR_POSITIVE | AW_SLIDE : AW_BLEND)); } protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {     base.OnClosing(e);     if (e.Cancel == false)     {         AnimateWindow(this.Handle, 1000, AW_HIDE | (_UseSlideAnimation ?                        AW_HOR_NEGATIVE | AW_SLIDE : AW_BLEND));     } }

Using the Class

Consuming the class is very simple, and requires a single line of code change to existing forms, by choosing to derive from FadeForm instead of the standard Form.

Collapse |
public partial class TestForm : Form { }

Changes to…

Collapse |
public partial class TestForm : FadeForm { }

Or

Collapse |
public partial class TestForm : FadeForm {     public TestForm() : base(true|false) {}  }

License

This article, along with any associated source code and files, is licensed under

转载于:https://www.cnblogs.com/luoyaoquan/archive/2011/12/26/2301774.html

你可能感兴趣的文章
Nginx 切片模块、断点续传
查看>>
MySQL 创建用户并分配用户权限
查看>>
typedef 函数指针
查看>>
UIScrollView的代理(delegate)
查看>>
【原创】.Net WebForm Calendar 日历控件常用方法
查看>>
对javascript中的匿名函数的理解
查看>>
noip模拟赛 道路分组
查看>>
【Python web 开发】django 从请求到响应经历了什么?
查看>>
移动端调试痛点?——送你五款前端开发利器
查看>>
贴心小棉袄
查看>>
关于this在不同使用情况表示的含义
查看>>
汇编实现: C库常见函数,串操作指令作用
查看>>
python中lambda表达式应用
查看>>
spring集成mongodb jar包版本问题
查看>>
ajax
查看>>
周赛Problem 1025: Hkhv love spent money(RMQ)
查看>>
[opencv] cv::Mat_类有时候可以替代cv::Mat类
查看>>
【BZOJ1257】【CQOI2007】余数之和sum
查看>>
(转)Thoughts on TypeScript——JavaScript大师Nicholas C. Zakas
查看>>
Spring Data JPA 实例查询
查看>>