zoukankan      html  css  js  c++  java
  • C# Fade Form Effect With the AnimateWindow API Function

    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 | Copy Code
    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 | Copy Code
    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 previous article, and modified to hide the actual implementation.

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

    Collapse | Copy Code
    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 | Copy Code
    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 | Copy Code
    public partial class TestForm : Form { }

    Changes to…

    Collapse | Copy Code
    public partial class TestForm : FadeForm { }

    Or

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

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  • 相关阅读:
    hive与hbase整合
    待重写
    hive DML
    【知识强化】第六章 总线 6.1 总线概述
    【知识强化】第五章 中央处理器 5.1 CPU的功能和基本结构
    【知识强化】第四章 指令系统 4.3 CISC和RISC的基本概念
    【知识强化】第四章 指令系统 4.2 指令寻址方式
    【知识强化】第四章 指令系统 4.1 指令格式
    【知识强化】第三章 存储系统 3.6 高速缓冲存储器
    【知识强化】第三章 存储系统 3.5 双口RAM和多模块存储器
  • 原文地址:https://www.cnblogs.com/luoyaoquan/p/2301774.html
Copyright © 2011-2022 走看看