tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
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
#region Imports
 
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region ParrotFormHandle
 
    public class ParrotFormHandle : Component
    {
        private Control _HandleControl;
 
        public const int WM_NCLBUTTONDOWN = 161;
        public const int HT_CAPTION = 2;
 
        [Category("Parrot")]
        [Browsable(true)]
        [Description("The HandleControl")]
        public Control HandleControl
        {
            get => _HandleControl;
            set
            {
                _HandleControl = value;
                _HandleControl.MouseDown += DragForm_MouseDown;
            }
        }
 
        [Category("Parrot")]
        [Browsable(true)]
        [Description("Maximize when dragged to top")]
        public bool DockAtTop { get; set; } = true;
 
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
 
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
 
        private void DragForm_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    ReleaseCapture();
                    SendMessage(HandleControl.FindForm().Handle, 161, 2, 0);
 
                    if (DockAtTop && HandleControl.FindForm().FormBorderStyle == FormBorderStyle.None)
                    {
                        if (HandleControl.FindForm().WindowState != FormWindowState.Maximized && Cursor.Position.Y <= 3)
                        {
                            HandleControl.FindForm().WindowState = FormWindowState.Maximized;
                            return;
                        }
 
                        HandleControl.FindForm().WindowState = FormWindowState.Normal;
                    }
                }
            }
            catch { }
        }
    }
 
    #endregion
}