[C#] C#实现窗体淡入淡出效果的几种方法(转) →→→→→进入此内容的聊天室

来自 , 2020-11-07, 写在 C#, 查看 188 次.
URL http://www.code666.cn/view/ddf88ea6
  1. 1.
  2. private void Form1_Load(object sender, System.EventArgs e)
  3. for(double d=0.01; d< 1; d+=0.02)
  4. {
  5. System.Threading.Thread.Sleep(1);
  6. Application.DoEvents();
  7. this.Opacity=d;
  8. this.Refresh();
  9. }
  10.  
  11. 2.
  12. private void timer1_Tick(object sender, System.EventArgs e)
  13. {
  14. this.Opacity = WinShow ;
  15. WinShow += 0.1 ;
  16. if(WinShow >=1 )
  17. {
  18. timer1.Dispose ();
  19. }
  20. }
  21. 3.
  22. 用循环或计时器,
  23.  
  24. frmForm myForm=new frmForm()
  25. frmForm.Opacity=0;
  26. frmForm.show();
  27. for(int i=0;i<100;i++)
  28. {
  29. Application.DoEvents()
  30. frmForm.Opacity=i/100;
  31. }
  32.  
  33. 4.
  34.  
  35. #region ******** 窗体淡入效果函数 ********
  36. private double WinShow = 0;//用于窗口淡入效果的变量
  37. private void FormShow(System.Windows.Forms.Form Curfrm)
  38. {
  39. Curfrm.Opacity = WinShow ;
  40. WinShow += 0.01;
  41. if(WinShow == 1)
  42. {
  43. Curfrm.timerShow.Stop ();
  44. }
  45. }
  46.  
  47. #endregion
  48. #region ******** 窗体淡入效果函数调用示例 ********
  49. //实现窗口的淡入效果
  50. private void timerShow_Tick(object sender, System.EventArgs e)
  51. {
  52. //timerShow,这是一个timer控件名称;把timerShow.interval=100就可以了。
  53. FormShow(this);
  54. }
  55. #endregion
  56.  
  57. 5.
  58.  
  59. using System.Runtime.InteropServices;
  60.  
  61. public class Win32
  62. {
  63. public const Int32 AW_HOR_POSITIVE = 0x00000001; // 从左到右打开窗口
  64. public const Int32 AW_HOR_NEGATIVE = 0x00000002; // 从右到左打开窗口
  65. public const Int32 AW_VER_POSITIVE = 0x00000004; // 从上到下打开窗口
  66. public const Int32 AW_VER_NEGATIVE = 0x00000008; // 从下到上打开窗口
  67. public const Int32 AW_CENTER = 0x00000010;
  68. public const Int32 AW_HIDE = 0x00010000; // 在窗体卸载时若想使用本函数就得加上此常量
  69. public const Int32 AW_ACTIVATE = 0x00020000; //在窗体通过本函数打开后,默认情况下会失去焦点,除非加上本常量
  70. public const Int32 AW_SLIDE = 0x00040000;
  71. public const Int32 AW_BLEND = 0x00080000; // 淡入淡出效果
  72. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  73. public static extern bool AnimateWindow(
  74. IntPtr hwnd, // handle to window
  75. int dwTime, // duration of animation
  76. int dwFlags // animation type
  77. );
  78. }
  79.  
  80.  
  81. /*淡入窗体*/
  82.  
  83. private void Form_Load(object sender, EventArgs e)
  84. {
  85. Win32.AnimateWindow(this.Handle, 2000, Win32.AW_BLEND);
  86. }
  87.  
  88. /*淡出窗体*/
  89.  
  90.  
  91. private void Form_FormClosing(object sender, FormClosingEventArgs e)
  92. {
  93. Win32.AnimateWindow(this.Handle, 2000, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND);
  94. }
  95.  
  96. //csharp/1110

回复 "C#实现窗体淡入淡出效果的几种方法(转)"

这儿你可以回复上面这条便签

captcha