1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#region
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
#endregion
 
namespace CSFrameworkV5.Library.UIForm
{
    public enum FormSize
    {
        NORMAL = 0, //正常大小
        MAX = 1 //最大化
    }
 
    public partial class frmBaseUI : Form
    {
        private const int WM_NCHITTEST = 0x0084;
 
        private const int HTLEFT = 10; //左边界
        private const int HTRIGHT = 11; //右边界
        private const int HTTOP = 12; //上边界
        private const int HTTOPLEFT = 13; //左上角
        private const int HTTOPRIGHT = 14; //右上角
        private const int HTBOTTOM = 15; //下边界
        private const int HTBOTTOMLEFT = 0x10; //左下角
        private const int HTBOTTOMRIGHT = 17; //右下角
 
        /// <summary>
        ///     窗口默认大小
        ///     FormSize.NORMAL OR FormSize.MAX
        /// </summary>
        private FormSize defaultFormSize = FormSize.NORMAL;
 
        /// <summary>
        ///     是否允许最大化
        /// </summary>
        private bool maxVisible = true;
 
        private Point mPoint;
 
        /// <summary>
        ///     窗体标题
        /// </summary>
        private string titleText;
 
        /// <summary>
        ///     窗体标题是否显示
        /// </summary>
        private bool titleVisible = true;
 
        public frmBaseUI()
        {
            InitializeComponent();
        }
 
        [Description("窗口默认大小")]
        public FormSize DefaultFormSize
        {
            get => defaultFormSize;
            set
            {
                defaultFormSize = value;
                if (defaultFormSize == FormSize.MAX)
                {
                    //防止遮挡任务栏
                    MaximumSize = new Size(
                        Screen.PrimaryScreen.WorkingArea.Width,
                        Screen.PrimaryScreen.WorkingArea.Height);
                    WindowState = FormWindowState.Maximized;
                    //最大化图标切换
                }
            }
        }
 
        [Description("是否允许最大化")]
        public bool MaxVisible
        {
            get => maxVisible;
            set
            {
                maxVisible = value;
                if (!maxVisible)
                {
                    btnEXMin.Location = new Point(btnEXMax.Location.X, 12);
                    btnEXMax.Visible = false;
                }
                else
                {
                    btnEXMin.Location = new Point(btnEXMax.Location.X - 20, 12);
                    btnEXMax.Visible = true;
                }
            }
        }
 
        [Description("窗体标题")]
        public string TitleText
        {
            get => titleText;
            set
            {
                titleText = value;
                title.Text = titleText;
            }
        }
 
        [Description("窗体标题是否显示")]
        public bool TitleVisible
        {
            get => titleVisible;
            set
            {
                titleVisible = value;
                title.Visible = titleVisible;
            }
        }
 
        /// <summary>
        ///     关闭按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEXClose_ButtonClick(object sender, EventArgs e)
        {
            Close();
        }
 
        /// <summary>
        ///     最大化按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEXMax_ButtonClick(object sender, EventArgs e)
        {
            MaxNormalSwitch();
        }
 
        /// <summary>
        ///     最小化按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEXMin_ButtonClick(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }
 
        private void FormEX_Resize(object sender, EventArgs e)
        {
            Invalidate(); //使重绘
        }
 
        /// <summary>
        ///     最大化和正常状态切换
        /// </summary>
        private void MaxNormalSwitch()
        {
            if (WindowState ==
                FormWindowState.Maximized) //如果当前状态是最大化状态 则窗体需要恢复默认大小
            {
                WindowState = FormWindowState.Normal;
                //
            }
            else
            {
                //防止遮挡任务栏
                MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width,
                    Screen.PrimaryScreen.WorkingArea.Height);
                WindowState = FormWindowState.Maximized;
                //最大化图标切换
            }
 
            Invalidate(); //使重绘
        }
 
        private void titleBar_DoubleClick(object sender, EventArgs e)
        {
            MaxNormalSwitch();
        }
 
        /// <summary>
        ///     鼠标按下标题栏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void titleBar_MouseDown(object sender, MouseEventArgs e)
        {
            mPoint = new Point(e.X, e.Y);
        }
 
        /// <summary>
        ///     鼠标在移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void titleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                Location = new Point(Location.X + e.X - mPoint.X,
                    Location.Y + e.Y - mPoint.Y);
        }
 
        #region 无边框窗体移动、放大、缩小
 
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_NCHITTEST:
                    base.WndProc(ref m);
                    var vPoint = new Point((int)m.LParam & 0xFFFF,
                        ((int)m.LParam >> 16) & 0xFFFF);
                    vPoint = PointToClient(vPoint);
                    if (vPoint.X <= 5)
                    {
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOPLEFT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMLEFT;
                        else
                            m.Result = (IntPtr)HTLEFT;
                    }
                    else if (vPoint.X >= ClientSize.Width - 5)
                    {
                        if (vPoint.Y <= 5)
                            m.Result = (IntPtr)HTTOPRIGHT;
                        else if (vPoint.Y >= ClientSize.Height - 5)
                            m.Result = (IntPtr)HTBOTTOMRIGHT;
                        else
                            m.Result = (IntPtr)HTRIGHT;
                    }
                    else if (vPoint.Y <= 5)
                    {
                        m.Result = (IntPtr)HTTOP;
                    }
                    else if (vPoint.Y >= ClientSize.Height - 5)
                    {
                        m.Result = (IntPtr)HTBOTTOM;
                    }
 
                    break;
                case WM_NCPAINT: //四周阴影 box shadow
                    if (m_aeroEnabled)
                    {
                        var v = 2;
                        DwmSetWindowAttribute(Handle, 2, ref v, 4);
                        var margins = new MARGINS
                        {
                            bottomHeight = 1,
                            leftWidth = 1,
                            rightWidth = 1,
                            topHeight = 1
                        };
                        DwmExtendFrameIntoClientArea(Handle, ref margins);
                    }
 
                    break;
            }
 
            base.WndProc(ref m);
 
            if (m.Msg == WM_NCHITTEST &&
                (int)m.Result == HTCLIENT) // drag the form
                m.Result = (IntPtr)HTCAPTION;
        }
 
        #endregion
 
        #region 窗体四周阴影
 
        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd,
            ref MARGINS pMarInset);
 
        [DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr,
            ref int attrValue, int attrSize);
 
        [DllImport("dwmapi.dll")]
        public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
 
        private bool m_aeroEnabled; // variables for box shadow
        private const int CS_DROPSHADOW = 0x00020000;
        private const int WM_NCPAINT = 0x0085;
        private const int WM_ACTIVATEAPP = 0x001C;
 
        public struct MARGINS // struct for box shadow
        {
            public int leftWidth;
            public int rightWidth;
            public int topHeight;
            public int bottomHeight;
        }
 
        //private const int WM_NCHITTEST = 0x84;          // variables for dragging the form
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;
 
        protected override CreateParams CreateParams
        {
            get
            {
                m_aeroEnabled = CheckAeroEnabled();
 
                var cp = base.CreateParams;
                if (!m_aeroEnabled) cp.ClassStyle |= CS_DROPSHADOW;
 
                return cp;
            }
        }
 
        private bool CheckAeroEnabled()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                var enabled = 0;
                DwmIsCompositionEnabled(ref enabled);
                return enabled == 1 ? true : false;
            }
 
            return false;
        }
 
        #endregion
    }
}