找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2583|回复: 13

Winform 限制窗体只能在屏幕内, 不能拖拽到屏幕外面

 火.. [复制链接]
  • 打卡等级:即来则安
  • 打卡总天数:29
  • 打卡月天数:1
  • 打卡总奖励:7791
  • 最近打卡:2025-12-13 17:25:16

2540

主题

1353

回帖

2万

积分

管理员

积分
21301
发表于 2021-8-19 19:27:44 | 显示全部楼层 |阅读模式
添加一个类文件"BaseForm.cs"


代码如下:




using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace WindowsFormsApp3
{
    public class BaseForm : Form
    {
        private Point _mouseDownPos;
        private bool _move;

        protected override void WndProc(ref Message m)
        {
            RECT nativeRect;
            switch (m.Msg)
            {
                case 0x20:
                    int lp = m.LParam.ToInt32();
                    if ((lp & 0xFFFF) == 2 &&
                    ((lp >> 0x10) & 0xFFFF) == 0x201)
                    {
                        _mouseDownPos = Control.MousePosition;
                        _move = true;
                    }
                    break;
                case 0x231:
                    if (_move)
                    {
                        Rectangle rect = Screen.GetWorkingArea(this);
                        nativeRect = new RECT(
                         _mouseDownPos.X - Location.X,
                         _mouseDownPos.Y - Location.Y,
                         rect.Right - (Bounds.Right - _mouseDownPos.X),
                         rect.Bottom - (Bounds.Bottom - _mouseDownPos.Y));
                        ClipCursor(ref nativeRect);
                    }
                    break;
                case 0x0232:
                    if (_move)
                    {
                        nativeRect = new RECT(Screen.GetWorkingArea(this));
                        ClipCursor(ref nativeRect);
                        _move = false;
                    }
                    break;
            }
            base.WndProc(ref m);
        }

        [DllImport("user32.dll")]
        public static extern bool ClipCursor(ref RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;

            public RECT(int left, int top, int right, int bottom)
            {
                Left = left;
                Top = top;
                Right = right;
                Bottom = bottom;
            }

            public RECT(Rectangle rect)
            {
                Left = rect.Left;
                Top = rect.Top;
                Right = rect.Right;
                Bottom = rect.Bottom;
            }

            public Rectangle Rect
            {
                get
                {
                    return new Rectangle(
                    Left,
                    Top,
                    Right - Left,
                    Bottom - Top);
                }
            }

            public Size Size
            {
                get
                {
                    return new Size(Right - Left, Bottom - Top);
                }
            }

            public static RECT FromXYWH(int x, int y, int width, int height)
            {
                return new RECT(x,
                  y,
                  x + width,
                  y + height);
            }

            public static RECT FromRectangle(Rectangle rect)
            {
                return new RECT(rect.Left,
                   rect.Top,
                   rect.Right,
                   rect.Bottom);
            }
        }
    }
}


新建应用窗体"Form1.cs",继承"BaseForm"


public partial class Form1 : BaseForm
{
    public Form1()
    {
        InitializeComponent();
    }
}
调试运行,窗体只能在屏幕内部来回拖动,无法拖拽到屏幕外。


​来源:C#社区


工控课堂 www.gkket.com

15

主题

405

回帖

2318

积分

高级会员

积分
2318
发表于 2021-8-19 19:27:44 | 显示全部楼层
绝对干货,楼主给力,支持了!!!
工控课堂 www.gkket.com

0

主题

96

回帖

415

积分

注册会员

积分
415
发表于 2021-8-19 19:53:42 | 显示全部楼层
真是被感动的痛哭流涕……
工控课堂 www.gkket.com
  • 打卡等级:无名新人
  • 打卡总天数:1
  • 打卡月天数:0
  • 打卡总奖励:6
  • 最近打卡:2025-01-22 14:08:01

0

主题

32

回帖

2478

积分

金牌会员

积分
2478
QQ
发表于 2021-8-20 09:46:23 | 显示全部楼层
强烈支持楼主ing……
书山有路勤为径

0

主题

112

回帖

442

积分

注册会员

积分
442
发表于 2025-11-18 18:30:05 | 显示全部楼层
原来还有这种操作,长见识了!
工控课堂 www.gkket.com

0

主题

89

回帖

265

积分

注册会员

积分
265
发表于 2025-11-18 18:34:45 | 显示全部楼层
蹲一波同款,有没有姐妹 / 兄弟推荐?
工控课堂 www.gkket.com

0

主题

89

回帖

130

积分

新手上路

积分
130
发表于 2025-11-18 18:36:04 | 显示全部楼层
水个经验,支持楼主,加油呀
工控课堂 www.gkket.com

0

主题

105

回帖

150

积分

新手上路

积分
150
发表于 2025-11-18 18:36:21 | 显示全部楼层
评论区人才辈出,笑到停不下来😂
工控课堂 www.gkket.com

0

主题

122

回帖

189

积分

新手上路

积分
189
发表于 2025-11-18 18:37:37 | 显示全部楼层
赞同 + 10086,没毛病,完全没毛病
工控课堂 www.gkket.com

0

主题

97

回帖

161

积分

新手上路

积分
161
发表于 2025-11-18 18:39:14 | 显示全部楼层
谁懂啊!真的被戳中笑点 / 泪点了
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|手机版|免责声明|本站介绍|工控课堂 ( 沪ICP备20008691号-1 )

GMT+8, 2025-12-22 17:55 , Processed in 0.098311 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表