yangyin
2025-03-27 b0de14c2670b9ff0079dacfb4b7457b438368f11
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/antdui/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620
 
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Vanara.PInvoke;
using static Vanara.PInvoke.DwmApi;
using static Vanara.PInvoke.User32;
 
namespace AntdUI
{
    public class Window : BaseForm, IMessageFilter
    {
        #region 属性
 
        bool resizable = true;
        [Description("调整窗口大小"), Category("行为"), DefaultValue(true)]
        public bool Resizable
        {
            get => resizable;
            set
            {
                if (resizable == value) return;
                resizable = value;
                HandMessage();
            }
        }
 
        WState winState = WState.Restore;
        WState WinState
        {
            set
            {
                if (winState == value) return;
                winState = value;
                if (IsHandleCreated) HandMessage();
                EventHub.Dispatch(EventType.WINDOW_STATE, winState == WState.Maximize);
            }
        }
 
        protected virtual bool UseMessageFilter => false;
        void HandMessage()
        {
            ReadMessage = winState == WState.Restore && resizable;
            if (UseMessageFilter) IsAddMessage = true;
            else IsAddMessage = ReadMessage;
        }
 
        bool ReadMessage = false;
        bool _isaddMessage = false;
        bool IsAddMessage
        {
            set
            {
                if (_isaddMessage == value) return;
                _isaddMessage = value;
                if (value) Application.AddMessageFilter(this);
                else Application.RemoveMessageFilter(this);
            }
        }
 
        #endregion
 
        internal HWND handle { get; private set; }
 
        protected override void OnHandleCreated(EventArgs e)
        {
            handle = new HWND(Handle);
            base.OnHandleCreated(e);
            if (FormBorderStyle == FormBorderStyle.None)
            {
                SetTheme();
                DisableProcessWindowsGhosting();
                HandMessage();
                DwmArea();
            }
            else
            {
                Size max = MaximumSize, min = MinimumSize;
                sizeInit = ClientSize;
                MaximumSize = MinimumSize = ClientSize = sizeInit.Value;
                SetTheme();
                DisableProcessWindowsGhosting();
                HandMessage();
                DwmArea();
                ClientSize = sizeInit.Value;
                MinimumSize = min;
                MaximumSize = max;
            }
        }
 
        protected override void OnLoad(EventArgs e)
        {
            SetWindowPos(handle, HWND.NULL, 0, 0, 0, 0, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_FRAMECHANGED);
            base.OnLoad(e);
        }
 
        private void InvalidateNonclient()
        {
            if (!IsHandleCreated || IsDisposed) return;
            RedrawWindow(handle, null, HWND.NULL, RedrawWindowFlags.RDW_FRAME | RedrawWindowFlags.RDW_UPDATENOW | RedrawWindowFlags.RDW_VALIDATE);
            UpdateWindow(handle);
            SetWindowPos(handle, HWND.NULL, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_NOCOPYBITS | SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOOWNERZORDER | SetWindowPosFlags.SWP_NOREPOSITION | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOZORDER);
        }
 
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            var msg = (WindowMessage)m.Msg;
            switch (msg)
            {
                case WindowMessage.WM_ACTIVATE:
                    DwmArea();
                    break;
                case WindowMessage.WM_NCCALCSIZE when m.WParam != IntPtr.Zero:
                    if (WmNCCalcSize(ref m)) return;
                    break;
                case WindowMessage.WM_NCACTIVATE:
                    if (WmNCActivate(ref m)) return;
                    break;
                case WindowMessage.WM_SIZE:
                    WmSize(ref m);
                    break;
            }
            if (WmGhostingHandler(m)) return;
            base.WndProc(ref m);
        }
 
        static IntPtr FALSE = new IntPtr(0);
        bool WmGhostingHandler(System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case 0x00AE:
                case 0x00AF:
                case 0xC1BC:
                    m.Result = FALSE;
                    InvalidateNonclient();
                    break;
            }
            return false;
        }
 
        bool iszoomed = false;
        bool ISZoomed()
        {
            bool value = IsZoomed(handle);
            if (iszoomed == value) return value;
            iszoomed = value;
            DwmArea();
            return value;
        }
 
        void DwmArea()
        {
            int margin;
            if (iszoomed) margin = 0;
            else margin = 1;
            DwmExtendFrameIntoClientArea(handle, new MARGINS(margin));
        }
 
        #region 区域
 
        /// <summary>
        /// 获取或设置窗体的位置
        /// </summary>
        public new Point Location
        {
            get
            {
                if (winState == WState.Restore) return base.Location;
                return ScreenRectangle.Location;
            }
            set
            {
                sizeNormal = null;
                base.Location = value;
            }
        }
 
        /// <summary>
        /// 控件的顶部坐标
        /// </summary>
        public new int Top
        {
            get => Location.Y;
            set
            {
                sizeNormal = null;
                base.Top = value;
            }
        }
 
        /// <summary>
        /// 控件的左侧坐标
        /// </summary>
        public new int Left
        {
            get => Location.X;
            set
            {
                sizeNormal = null;
                base.Left = value;
            }
        }
 
        /// <summary>
        /// 控件的右坐标
        /// </summary>
        public new int Right
        {
            get => ScreenRectangle.Right;
        }
 
        /// <summary>
        /// 控件的底部坐标
        /// </summary>
        public new int Bottom
        {
            get => ScreenRectangle.Bottom;
        }
 
        /// <summary>
        /// 获取或设置窗体的大小
        /// </summary>
        public new Size Size
        {
            get
            {
                if (winState == WState.Restore) return base.Size;
                return ScreenRectangle.Size;
            }
            set
            {
                sizeNormal = null;
                base.Size = value;
                sizeInit = ClientSize;
            }
        }
 
        /// <summary>
        /// 控件的宽度
        /// </summary>
        public new int Width
        {
            get => Size.Width;
            set
            {
                sizeNormal = null;
                base.Width = value;
                sizeInit = ClientSize;
            }
        }
 
        /// <summary>
        /// 控件的高度
        /// </summary>
        public new int Height
        {
            get => Size.Height;
            set
            {
                sizeNormal = null;
                base.Height = value;
                sizeInit = ClientSize;
            }
        }
 
        /// <summary>
        /// 获取或设置窗体屏幕区域
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Rectangle ScreenRectangle
        {
            get
            {
                if (winState == WState.Restore) return new Rectangle(base.Location, base.Size);
                var rect = ClientRectangle;
                var point = RectangleToScreen(Rectangle.Empty);
                return new Rectangle(point.Location, rect.Size);
            }
            set
            {
                sizeNormal = null;
                base.Location = value.Location;
                base.Size = value.Size;
                sizeInit = ClientSize;
            }
        }
 
        #endregion
 
        #region 交互
 
        #region 调整窗口大小
 
        /// <summary>
        /// 调整窗口大小(鼠标移动)
        /// </summary>
        /// <returns>可以调整</returns>
        public override bool ResizableMouseMove()
        {
            var retval = HitTest(PointToClient(MousePosition));
            if (retval != HitTestValues.HTNOWHERE)
            {
                var mode = retval;
                if (mode != HitTestValues.HTCLIENT && winState == WState.Restore)
                {
                    SetCursorHit(mode);
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 调整窗口大小(鼠标移动)
        /// </summary>
        /// <param name="point">客户端坐标</param>
        /// <returns>可以调整</returns>
        public override bool ResizableMouseMove(Point point)
        {
            var retval = HitTest(point);
            if (retval != HitTestValues.HTNOWHERE)
            {
                var mode = retval;
                if (mode != HitTestValues.HTCLIENT && winState == WState.Restore)
                {
                    SetCursorHit(mode);
                    return true;
                }
            }
            return false;
        }
 
        #endregion
 
        #region 鼠标
 
        public override bool IsMax
        {
            get => winState == WState.Maximize;
        }
 
        public static bool CanHandMessage = true;
        public bool PreFilterMessage(ref System.Windows.Forms.Message m)
        {
            if (is_resizable) return OnPreFilterMessage(m);
            if (CanHandMessage && ReadMessage)
            {
                switch (m.Msg)
                {
                    case 0xa0:
                    case 0x200:
                        if (isMe(m.HWnd))
                        {
                            if (ResizableMouseMove(PointToClient(MousePosition))) return true;
                        }
                        break;
                    case 0xa1:
                    case 0x201:
                        if (isMe(m.HWnd))
                        {
                            if (ResizableMouseDown()) return true;
                        }
                        break;
                }
            }
            return OnPreFilterMessage(m);
        }
 
        protected virtual bool OnPreFilterMessage(System.Windows.Forms.Message m) => false;
 
        bool isMe(IntPtr intPtr)
        {
            var frm = FromHandle(intPtr);
            if (frm == this || GetParent(frm) == this) return true;
            return false;
        }
 
        static Control? GetParent(Control? control)
        {
            try
            {
                if (control != null && control.IsHandleCreated && control.Parent != null)
                {
                    if (control is Form) return control;
                    return GetParent(control.Parent);
                }
            }
            catch { }
            return control;
        }
 
        #endregion
 
        #endregion
 
        #region WindowMessage Handlers
 
        const nint SIZE_RESTORED = 0;
        const nint SIZE_MINIMIZED = 1;
        const nint SIZE_MAXIMIZED = 2;
        void WmSize(ref System.Windows.Forms.Message m)
        {
            if (m.WParam == SIZE_MINIMIZED) WinState = WState.Minimize;
            else if (m.WParam == SIZE_MAXIMIZED) WinState = WState.Maximize;
            else if (m.WParam == SIZE_RESTORED)
            {
                sizeNormal = ClientSize;
                WinState = WState.Restore;
                InvalidateNonclient();
                Invalidate();
            }
        }
 
        bool WmNCCalcSize(ref System.Windows.Forms.Message m)
        {
            if (FormBorderStyle == FormBorderStyle.None) return false;
            if (ISZoomed())
            {
#if NET40
                var nccsp = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
#else
                var nccsp = Marshal.PtrToStructure<RECT>(m.LParam);
#endif
                var borders = GetNonClientMetrics();
 
                nccsp.top -= borders.Top;
                nccsp.top += borders.Bottom;
                Marshal.StructureToPtr(nccsp, m.LParam, false);
                return false;
            }
            else
            {
                m.Result = new IntPtr(1);
                return true;
            }
        }
 
        internal Size? sizeInit;
        Size? sizeNormal;
        bool WmNCActivate(ref System.Windows.Forms.Message m)
        {
            if (m.HWnd == IntPtr.Zero) return false;
            if (IsIconic(m.HWnd)) return false;
            m.Result = DefWindowProc(m.HWnd, (uint)m.Msg, m.WParam, new IntPtr(-1));
            return true;
        }
 
        #endregion
 
        #region Frameless Crack
 
        protected override void SetClientSizeCore(int x, int y)
        {
            if (DesignMode) Size = new Size(x, y);
            else base.SetClientSizeCore(x, y);
        }
 
        protected Padding GetNonClientMetrics()
        {
            var screenRect = ClientRectangle;
            screenRect.Offset(-Bounds.Left, -Bounds.Top);
            var rect = new RECT(screenRect);
            AdjustWindowRectEx(ref rect, (WindowStyles)CreateParams.Style, false, (WindowStylesEx)CreateParams.ExStyle);
            return new Padding
            {
                Top = screenRect.Top - rect.top,
                Left = screenRect.Left - rect.left,
                Bottom = rect.bottom - screenRect.Bottom,
                Right = rect.right - screenRect.Right
            };
        }
 
        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            if (DesignMode) base.SetBoundsCore(x, y, width, height, specified);
            else if (WindowState == FormWindowState.Normal && sizeNormal.HasValue) base.SetBoundsCore(x, y, sizeNormal.Value.Width, sizeNormal.Value.Height, specified);
            else base.SetBoundsCore(x, y, width, height, specified);
        }
 
        #endregion
    }
 
    public enum WState
    {
        Restore,
        Maximize,
        Minimize
    }
}