winform窗体特效


1.

  private   void   Form1_Load(object   sender,   System.EventArgs   e)   
  for(double   d=0.01;   d<   1;   d+=0.02)   
  {   
  System.Threading.Thread.Sleep(1);   
  Application.DoEvents();   
  this.Opacity=d;   
  this.Refresh();   
  }

 

2.

  private   void   timer1_Tick(object   sender,   System.EventArgs   e)   
  {   
  this.Opacity   =   WinShow   ;   
  WinShow   +=   0.1   ;   
  if(WinShow   >=1   )   
  {   
  timer1.Dispose   ();   
  }   
  }

 

3.

用循环或计时器,   
  frmForm   myForm=new   frmForm()   
  frmForm.Opacity=0;   
  frmForm.show();   
  for(int   i=0;i<100;i++)   
  {   
  Application.DoEvents()   
  frmForm.Opacity=i/100;   
  }

 

4.

  #region     ********   窗体淡入效果函数       ********   
  private   double   WinShow   =   0;//用于窗口淡入效果的变量   
    
  private   void   FormShow(System.Windows.Forms.Form   Curfrm)   
  {   
  Curfrm.Opacity   =   WinShow   ;   
  WinShow   +=   0.01;   
  if(WinShow   ==   1)   
  {   
  Curfrm.timerShow.Stop   ();   
  }   
  }   
    
  #endregion   
    
  #region     ********   窗体淡入效果函数调用示例       ********   
  //实现窗口的淡入效果   
  private   void   timerShow_Tick(object   sender,   System.EventArgs   e)   
  {   
  //timerShow,这是一个timer控件名称;把timerShow.interval=100就可以了。   
  FormShow(this);   
  }   
  #endregion

 

 

 

*********************************************************************************************************

c# WInform 窗口启动特效,使用win32 api
一段使用api的窗口特效代码.自己可以试下,是不是在窗口显示的时候会显示各种特殊效果.有点惊喜的感觉
[System.Runtime.InteropServices.DllImport("user32")]
        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 void Form1_Load(object sender, System.EventArgs e)
        {
     
            int animatetype = 10;
            Random a = new Random();
            int dwFlags = (int)a.Next(animatetype);
            switch (dwFlags)
            {
                case 0://普通显示
                    AnimateWindow(Handle, 1000, AW_ACTIVATE);
                    break;
                case 1://从左向右显示
                    AnimateWindow(Handle, 1000, AW_HOR_POSITIVE);
                    break;
                case 2://从右向左显示
                    AnimateWindow(Handle, 1000, AW_HOR_NEGATIVE);
                    break;
                case 3://从上到下显示
                    AnimateWindow(Handle, 1000, AW_VER_POSITIVE);
                    break;
                case 4://从下到上显示
                    AnimateWindow(Handle, 1000, AW_VER_NEGATIVE);
                    break;
                case 5://透明渐变显示
                    AnimateWindow(Handle, 1000, AW_BLEND);
                    break;
                case 6://从中间向四周
                    AnimateWindow(Handle, 1000, AW_CENTER);
                    break;
                case 7://左上角伸展
                    AnimateWindow(Handle, 1000, AW_SLIDE | AW_HOR_POSITIVE | AW_VER_POSITIVE);
                    break;
                case 8://左下角伸展
                    AnimateWindow(Handle, 1000, AW_SLIDE | AW_HOR_POSITIVE | AW_VER_NEGATIVE);
                   break;
                case 9://右上角伸展
                    AnimateWindow(Handle, 1000, AW_SLIDE | AW_HOR_NEGATIVE | AW_VER_POSITIVE);
                    break;
                case 10://右下角伸展
                    AnimateWindow(Handle, 1000, AW_SLIDE | AW_HOR_NEGATIVE | AW_VER_NEGATIVE);
                    break;
            }
        }
 
        private void Form1_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            AnimateWindow(this.Handle, 1000, AW_SLIDE | AW_HIDE | AW_VER_NEGATIVE);
        }

 

 

******************************************************************************************************

本文基于.Net开发,使用C#作为开发语言,分别包含以下效果:
移动无边框窗口、窗口移动限制(限制在屏幕内)、桌面贴边自动隐藏(仿QQ隐藏窗口)

1、移动无边框窗口
采用了消息的方式,可以实现通过窗口内的任何控件来移动窗口

  1. private const int WM_NCLBUTTONDOWN = 0x00A1;
  2. private const int HT_CAPTION = 0x002;
  3. private void Form_MouseDown(object sender, MouseEventArgs e)
  4. {
  5.    if (e.Button == MouseButtons.Left)
  6.    {
  7.    ((Control)sender).Capture = false;
  8.    Message msg = Message.Create(Handle, WM_NCLBUTTONDOWN, (IntPtr)HT_CAPTION, IntPtr.Zero);
  9.    base.WndProc(ref msg);
  10.    }
  11. }

使用方法:
将以上代码复制到Form的class内,将Form或Form中需要控制Form移动的控件的MouseDown事件关联到这个方法上,如:
对于Form:this.MouseDown+=new System.Windows.Forms.MouseEventHandler(Form_MouseDown);
对于其它控件:Control.MouseDown+=new System.Windows.Forms.MouseEventHandler(Form_MouseDown);

2、窗口移动限制

  1. private const int WM_MOVING = 0x0216;
  2. protected override void WndProc(ref Message m)
  3. {
  4.    switch (m.Msg)
  5.    {
  6.    case WM_MOVING: // 窗体移动的消息,控制窗体不会移出屏幕外
  7.    int left = Marshal.ReadInt32(m.LParam, 0);
  8.    int top = Marshal.ReadInt32(m.LParam, 4);
  9.    int right = Marshal.ReadInt32(m.LParam, 8);
  10.    int bottom = Marshal.ReadInt32(m.LParam, 12);
  11.    left = Math.Min(Math.Max(0, left), Screen.PrimaryScreen.Bounds.Width - Width);
  12.    top = Math.Min(Math.Max(0, top), Screen.PrimaryScreen.Bounds.Height - Height);
  13.    right = Math.Min(Math.Max(Width, right), Screen.PrimaryScreen.Bounds.Width);
  14.    bottom = Math.Min(Math.Max(Height, bottom), Screen.PrimaryScreen.Bounds.Height);
  15.    Marshal.WriteInt32(m.LParam, 0, left);
  16.    Marshal.WriteInt32(m.LParam, 4, top);
  17.    Marshal.WriteInt32(m.LParam, 8, right);
  18.    Marshal.WriteInt32(m.LParam, 12, bottom);
  19.    break;
  20.    }
  21.    base.WndProc(ref m);
  22. }

使用方法:
添加using System.Runtime.InteropServices;
在Form类定义中加入以上代码即可
3、桌面贴边自动隐藏

下载: FormAutoDock.cs
  1. public class FormAutoDock
  2. {
  3. public static void SideHideOrShow(Form DockableForm, ref int DockFormHeight, Timer _dockTimer)
  4. {
  5. if (DockableForm.WindowState != FormWindowState.Minimized)
  6. {
  7. _dockTimer.Interval = 1500;
  8. if (Cursor.Position.X > DockableForm.Left - 1 && Cursor.Position.X < DockableForm.Right && Cursor.Position.Y > DockableForm.Top - 1 && Cursor.Position.Y < DockableForm.Bottom)
  9. {
  10. if (DockableForm.Top <= 0 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
  11. {
  12. DockableForm.Top = 0;
  13. }
  14. else if (DockableForm.Left <= 0)
  15. {
  16. DockableForm.Left = 0;
  17. }
  18. else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width)
  19. {
  20. DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width;
  21. }
  22. else
  23. {
  24. if (DockFormHeight > 0)
  25. {
  26. DockableForm.Height = DockFormHeight;
  27. DockFormHeight = 0;
  28. }
  29. }
  30. }
  31. else
  32. {
  33. if (DockFormHeight < 1)
  34. {
  35. DockFormHeight = DockableForm.Height;
  36. }
  37. if (DockableForm.Top <= 4 && DockableForm.Left > 5 && DockableForm.Left < Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width)
  38. {
  39. DockableForm.Top = 3 - DockableForm.Height;
  40. if (DockableForm.Left <= 4)
  41. {
  42. DockableForm.Left = -5;
  43. }
  44. else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
  45. {
  46. DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - DockableForm.Width + 5;
  47. }
  48. }
  49. else if (DockableForm.Left <= 4)
  50. {
  51. DockableForm.Left = 3 - DockableForm.Width;
  52. }
  53. else if (DockableForm.Left + DockableForm.Width >= Screen.PrimaryScreen.WorkingArea.Width - 4)
  54. {
  55. DockableForm.Left = Screen.PrimaryScreen.WorkingArea.Width - 3;
  56. }
  57. _dockTimer.Interval = 200;
  58. }
  59. }
  60. }
  61. }

使用方法:
将FormAutoDock.cs文件复制到与需要处理的Form相同目录下,并添加到项目中
在Form的类中增加以下内容:
private Timer _dockTimer = null;
private int DockFormHeight = 0;
private void DockTimerAutoHide_Tick(object sender, EventArgs e)
{
FormAutoDock.SideHideOrShow(this, ref DockFormHeight, _dockTimer);
Application.DoEvents();
}

在Form类构造方法中添加以下内容:
_dockTimer = new Timer();
_dockTimer.Enabled = false;
_dockTimer.Interval = 200;
_dockTimer.Tick += new EventHandler(DockTimerAutoHide_Tick);
_dockTimer.Start();

 

 

 

 

*********************************************************************************************************

打开QQ的时候,总是会先听到一声咳嗽声,然后屏幕的右下角就会慢慢升起一个小窗口,占用的地方不大,又可以起到提示的作用。如果要写代码控制窗体的位置和透明度来实现这个功能,挺麻烦的。下面就让我们来看看,怎样用系统API来轻松实现这个功能。老规矩,先来介绍一下要用到的API函数: bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags); 从字面的意思来看,这个函数名为"活动的窗口",事实上也如此,通过这个函数,可以使我们的窗体动作丰富起来,来说明一下参数。

hwnd: 指定产生动画的窗口的句柄 dwTime: 指定动画持续的时间 dwFags: 指定动画类型,可以是一个或多个以下标志的组合。 标志说明: AW_ACTIVE: 激活窗口,在使用了AW_HIDE标志后不要使用这个标志 AW_HIDE: 隐藏窗口 AW_BLEND: 使用淡入淡出效果 AW_SLIDE: 使用滑动类型动画效果,默认为滚动动画类型,当使用AW_CENTER标志时,这个标志就被忽略 AW_CENTER: 若使用了AW_HIDE标志,则使窗口向内重叠;否则向外扩展 AW_HOR_POSITIVE: 自左向右显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志 AW_HOR_NEGATIVE: 自右向左显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志 AW_VER_POSITIVE: 自顶向下显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志 AW_VER_NEGATIVE: 自下向上显示窗口,该标志可以在滚动动画和滑动动画中使用。使用AW_CENTER标志时忽略该标志 这些标志对应的值如下:

int AW_ACTIVE = 0x20000; int AW_HIDE = 0x10000; int AW_BLEND = 0x80000; int AW_SLIDE = 0x40000; int AW_CENTER = 0x0010; int AW_HOR_POSITIVE = 0x0001; int AW_HOR_NEGATIVE = 0x0002; int AW_VER_POSITIVE = 0x0004; int AW_VER_NEGATIVE = 0x0008;

好,了解了上面那些东西以后,剩下来的就超简单了,不必写很多代码去控制窗体位置,不必再写代码去控制透明度,只需一句代码。首先添加命名空间:

using System.Runtime.InteropServices;

然后,声明我们所要用到的函数及常量:

[DllImport("user32")]

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_ACTIVE = 0x20000;private const int AW_SLIDE = 0x40000;

private const int AW_BLEND = 0x80000;

基础打好了,现在来做剩下的工作吧。给窗体设定显示时效果。在窗体的Load事件中,添加以下代码:

//设定窗口的位置在右下角 int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width; int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height; this.Location = new Point(x, y); //设定窗口显示的动画效果为 自下向上滑动显示窗口 AnimateWindow(this.Handle, 1000, AW_SLIDE | AW_ACTIVE | AW_VER_NEGATIVE );

再来设定窗口关闭时候的效果。在窗口的FormClosing事件中添加以下代码:

//设定窗口关闭消失时的效果为淡出 AnimateWindow(this.Handle, 1000, AW_BLEND | AW_HIDE);

编译运行,这时我们会发现,窗口出现时是从右下角自下向上一点点的滑动出来的,关闭消失时,是从有到无逐渐消失的。我们的工作完成了,是不是很简单,呵呵。API有时候可以让我们的代码很简单。

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值