c# winform 让一个无边框窗口可调整大小

窗体被控件完全覆盖,完全无法响应鼠标事件。 其中参数 container 传 this.container 即可,是为了资源能优雅释放做准备用的。 用如下代码:

public static class FormResizer
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern IntPtr SetCapture(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern IntPtr GetCapture();

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool ReleaseCapture();

        private static bool IsMouseInBottomRightCorner(Form frm)
        {
            // 获取窗体客户端坐标系下的右下角区域
            Rectangle bottomRightRect = new Rectangle(frm.ClientSize.Width - 10, frm.ClientSize.Height - 10, 10, 10);

            // 获取鼠标的屏幕坐标
            Point mousePosition = Cursor.Position;

            // 将鼠标坐标转换为窗体客户端坐标
            Point clientMousePosition = frm.PointToClient(mousePosition);

            // 检查鼠标坐标是否在右下角区域内
            return bottomRightRect.Contains(clientMousePosition);
        }

        /// <summary>
        /// 让ctrl的右下角可调整frm的大小
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="frm"></param>
        public static void EnableFormResize(Form frm, System.ComponentModel.IContainer container)
        {
            var enteredCorner = false;
            bool isFormDragging = false;
            Point formDragStartPosition = Cursor.Position;
            Size formDragStartSize = frm.Size;
            var timer = new System.Windows.Forms.Timer(container);
            timer.Interval = 100;
            timer.Enabled = true;
            timer.Tick += (s, e) => 
            {
                var inCorner = IsMouseInBottomRightCorner(frm);

                //模拟MouseEnter事件
                if (inCorner && !enteredCorner)
                {
                    enteredCorner = true;
                    SetCapture(frm.Handle);
                    frm.Cursor = Cursors.SizeNWSE;
                }
                
                //模拟MouseLeave事件
                if(!inCorner && enteredCorner)
                {
                    if (!isFormDragging)
                    {
                        if (GetCapture().ToInt64() == frm.Handle.ToInt64())
                            ReleaseCapture();
                        frm.Cursor = Cursors.Default;
                    }
                    enteredCorner = false;
                }
            };
            frm.MouseDown += (s, e) =>
            {
                SetCapture(frm.Handle);
                isFormDragging = true;
                formDragStartPosition = e.Location;
                formDragStartSize = frm.Size;
            };
            frm.MouseMove += (s, e) =>
            {
                if (isFormDragging)
                {
                    var newSize = new Size(formDragStartSize.Width + (e.X - formDragStartPosition.X), formDragStartSize.Height + (e.Y - formDragStartPosition.Y));
                    frm.Size = newSize;
                }
            };
            frm.MouseUp += (s, e) =>
            {
                if (GetCapture().ToInt64() == frm.Handle.ToInt64())
                    ReleaseCapture();
                isFormDragging = false;
                frm.Cursor = Cursors.Default;
            };
        }
    }

 

posted on 2024-04-09 07:19  空明流光  阅读(133)  评论(0编辑  收藏  举报

导航