Continue to study the display animation effect of WinForm of C# today.
Last time we realized the display animation effect of the borderless form (see blog post: The perfect solution for the animation effect of the borderless form based on C# - open source research series articles ), this time we introduce the form that is not in the taskbar tray The implementation code of the display and hide animation effect.
1. Project directory;
Below is the project directory, which consists of basic form and action classes.
2. Code introduction;
The code is relatively simple, just add the minimization and maximization effects of the form directly.
1 namespace Lzhdim.Helper
2 {
3 using System.Runtime.InteropServices;
4 using System;
5 using System.Windows.Forms;
6
7
8 /// <summary>
9 /// 窗体状态
10 /// </summary>
11 internal enum ShowWindowState
12 {
13 /// <summary>
14 /// 显示窗体
15 /// </summary>
16 Show,
17 /// <summary>
18 /// 隐藏窗体
19 /// </summary>
20 Hide,
21 /// <summary>
22 /// 最小化窗体
23 /// </summary>
24 Min,
25 /// <summary>
26 /// 最大化窗体
27 /// </summary>
28 Max,
29 /// <summary>
30 /// 直接调用窗体的显示
31 /// </summary>
32 DirectShow,
33 /// <summary>
34 /// 直接调用窗体的隐藏
35 /// </summary>
36 DirectHide,
37 /// <summary>
38 /// 窗体有运行实例时的显示
39 /// </summary>
40 RunningShow
41 }
42
43 /// <summary>
44 /// 显示隐藏窗体状态操作类
45 ///
46 /// 窗体的状态都通过此API进行处理
47 /// </summary>
48 internal static class ShowWindowHelper
49 {
50 [DllImport("User32.dll")]
51 private static extern bool SetForegroundWindow(IntPtr hWnd);
52 [DllImport("User32.dll")]
53 private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
54
55 //API 常數定義
56
57 private const int SW_HIDE = 0;
58 private const int SW_NORMAL = 1;
59 private const int SW_MAXIMIZE = 3;
60 private const int SW_SHOWNOACTIVATE = 4;
61 private const int SW_SHOW = 5;
62 private const int SW_MINIMIZE = 6;
63 private const int SW_RESTORE = 9;
64 private const int SW_SHOWDEFAULT = 10;
65
66 /// <summary>
67 /// 设置窗体状态
68 /// </summary>
69 /// <param name="form">要设置的窗体</param>
70 /// <param name="windowState">窗体状态</param>
71 internal static void ShowWindow(Form form, ShowWindowState windowState)
72 {
73 switch (windowState)
74 {
75 case ShowWindowState.Show:
76 form.Visible = true;
77 form.WindowState = System.Windows.Forms.FormWindowState.Normal;
78 form.ShowInTaskbar = true;
79 break;
80 case ShowWindowState.Hide:
81 //这里两个Visible是为了关闭时的动画效果
82 form.Visible = false;
83 form.WindowState = System.Windows.Forms.FormWindowState.Minimized;
84 form.ShowInTaskbar = false;
85 form.Visible = false;
86 break;
87 case ShowWindowState.Min:
88 form.Visible = true;
89 form.WindowState = System.Windows.Forms.FormWindowState.Minimized;
90 break;
91 case ShowWindowState.Max:
92 form.Visible = true;
93 form.WindowState = System.Windows.Forms.FormWindowState.Maximized;
94 break;
95 case ShowWindowState.DirectShow:
96 form.Show();
97 break;
98 case ShowWindowState.DirectHide:
99 form.Hide();
100 break;
101 case ShowWindowState.RunningShow:
102 //保存窗体现在的状态
103 FormWindowState formWindowState = form.WindowState;
104 bool visible = form.Visible;
105
106 //下面显示窗体
107 form.Visible = true;
108 form.ShowInTaskbar = true;
109
110 if (visible)
111 {
112 //如果在状态栏显示状态,则直接还原
113 switch (formWindowState)
114 {
115 case FormWindowState.Minimized:
116 //如果是最小化状态,则还原原来的状态,比如正常或者最大化
117 ShowWindowAsync(form.Handle, SW_RESTORE);
118 break;
119 }
120 }
121 else
122 {
123 //如果是隐藏状态
124 switch(formWindowState)
125 {
126 case FormWindowState.Maximized:
127 //原来是最大化的就最大化显示
128 ShowWindowAsync(form.Handle, SW_MAXIMIZE);
129 break;
130 case FormWindowState.Minimized:
131 case FormWindowState.Normal:
132 //如果是正常或者最小化则正常状态显示
133 ShowWindowAsync(form.Handle, SW_NORMAL);
134 break;
135 }
136 }
137 //最后将窗体置于最前显示
138 SetForegroundWindow(form.Handle);
139
140 break;
141 }
142 }
143 }
144 }
Because no GIF is recorded, no interface is provided.
4. How to use;
Put the operation class directly into the project, and then call the static class method.
1 /// <summary>
2 /// 隐藏窗体按钮事件
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 private void btnHide_Click(object sender, EventArgs e)
7 {
8 ShowWindowHelper.ShowWindow(this, ShowWindowState.Hide);
9 }
10
11 /// <summary>
12 /// 图标单击事件
13 /// </summary>
14 /// <param name="sender"></param>
15 /// <param name="e"></param>
16 private void NIShowWindow_MouseClick(object sender, MouseEventArgs e)
17 {
18 if(e.Button == MouseButtons.Left)
19 {
20 ShowWindowHelper.ShowWindow(this, ShowWindowState.Show);
21 }
22 }
23 /// <summary>
24 /// 图标双击事件
25 /// </summary>
26 /// <param name="sender"></param>
27 /// <param name="e"></param>
28 private void NIShowWindow_MouseDoubleClick(object sender, MouseEventArgs e)
29 {
30 if (e.Button == MouseButtons.Left)
31 {
32 //这里调用Hide程序会退出,所以直接调默认的Hide函数
33 ShowWindowHelper.ShowWindow(this, ShowWindowState.DirectHide);
34 }
35 }
5. Source code download;
The source code example download is provided here:
https://download.csdn.net/download/lzhdim/88171194
The above is the animation effect of the C# form hidden in the tray icon compiled by the author. With the effect of the borderless form last time, it can better display the effect of the form. The source code download and examples are provided, and the code can be reused directly. I hope readers who have the same problem can solve this problem.