找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3413|回复: 12

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

 火.. [复制链接]
  • 打卡等级:常驻代表
  • 打卡总天数:34
  • 打卡月天数:6
  • 打卡总奖励:9027
  • 最近打卡:2025-12-17 23:15:51

2823

主题

541

回帖

2万

积分

管理员

积分
22569
发表于 2021-9-4 11:44:02 | 显示全部楼层 |阅读模式
添加一个类文件"BaseForm.cs"

代码如下:

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Text;

  4. using System.Windows.Forms;

  5. using System.Drawing;

  6. using System.Runtime.InteropServices;



  7. namespace WindowsFormsApp3

  8. {

  9.     public class BaseForm : Form

  10.     {

  11.         private Point _mouseDownPos;

  12.         private bool _move;



  13.         protected override void WndProc(ref Message m)

  14.         {

  15.             RECT nativeRect;

  16.             switch (m.Msg)

  17.             {

  18.                 case 0x20:

  19.                     int lp = m.LParam.ToInt32();

  20.                     if ((lp & 0xFFFF) == 2 &&

  21.                     ((lp >> 0x10) & 0xFFFF) == 0x201)

  22.                     {

  23.                         _mouseDownPos = Control.MousePosition;

  24.                         _move = true;

  25.                     }

  26.                     break;

  27.                 case 0x231:

  28.                     if (_move)

  29.                     {

  30.                         Rectangle rect = Screen.GetWorkingArea(this);

  31.                         nativeRect = new RECT(

  32.                          _mouseDownPos.X - Location.X,

  33.                          _mouseDownPos.Y - Location.Y,

  34.                          rect.Right - (Bounds.Right - _mouseDownPos.X),

  35.                          rect.Bottom - (Bounds.Bottom - _mouseDownPos.Y));

  36.                         ClipCursor(ref nativeRect);

  37.                     }

  38.                     break;

  39.                 case 0x0232:

  40.                     if (_move)

  41.                     {

  42.                         nativeRect = new RECT(Screen.GetWorkingArea(this));

  43.                         ClipCursor(ref nativeRect);

  44.                         _move = false;

  45.                     }

  46.                     break;

  47.             }

  48.             base.WndProc(ref m);

  49.         }



  50.         [DllImport("user32.dll")]

  51.         public static extern bool ClipCursor(ref RECT lpRect);



  52.         [StructLayout(LayoutKind.Sequential)]

  53.         public struct RECT

  54.         {

  55.             public int Left;

  56.             public int Top;

  57.             public int Right;

  58.             public int Bottom;



  59.             public RECT(int left, int top, int right, int bottom)

  60.             {

  61.                 Left = left;

  62.                 Top = top;

  63.                 Right = right;

  64.                 Bottom = bottom;

  65.             }



  66.             public RECT(Rectangle rect)

  67.             {

  68.                 Left = rect.Left;

  69.                 Top = rect.Top;

  70.                 Right = rect.Right;

  71.                 Bottom = rect.Bottom;

  72.             }



  73.             public Rectangle Rect

  74.             {

  75.                 get

  76.                 {

  77.                     return new Rectangle(

  78.                     Left,

  79.                     Top,

  80.                     Right - Left,

  81.                     Bottom - Top);

  82.                 }

  83.             }



  84.             public Size Size

  85.             {

  86.                 get

  87.                 {

  88.                     return new Size(Right - Left, Bottom - Top);

  89.                 }

  90.             }



  91.             public static RECT FromXYWH(int x, int y, int width, int height)

  92.             {

  93.                 return new RECT(x,

  94.                   y,

  95.                   x + width,

  96.                   y + height);

  97.             }



  98.             public static RECT FromRectangle(Rectangle rect)

  99.             {

  100.                 return new RECT(rect.Left,

  101.                    rect.Top,

  102.                    rect.Right,

  103.                    rect.Bottom);

  104.             }

  105.         }

  106.     }

  107. }
复制代码


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

  1. public partial class Form1 : BaseForm

  2. {

  3.     public Form1()

  4.     {

  5.         InitializeComponent();

  6.     }

  7. }
复制代码

调试运行,窗体只能在屏幕内部来回拖动,无法拖拽到屏幕外。


来源:C#社区
原文:https://www.hicsharp.com/a/0c2ee4aded234c8fa023c9f751a57061



工控课堂 www.gkket.com

0

主题

717

回帖

2166

积分

高级会员

积分
2166
发表于 2021-9-4 11:48:47 | 显示全部楼层
楼主您的技术水准,我最服你,其他都是浮云
工控课堂 www.gkket.com

0

主题

89

回帖

269

积分

注册会员

积分
269
发表于 2025-11-15 21:19:39 | 显示全部楼层
水个经验,支持楼主,加油呀
工控课堂 www.gkket.com

0

主题

55

回帖

60

积分

新手上路

积分
60
发表于 2025-11-15 21:27:24 | 显示全部楼层
学到干货了,感谢分享,已火速收藏
工控课堂 www.gkket.com

0

主题

101

回帖

363

积分

注册会员

积分
363
发表于 2025-11-15 21:29:04 | 显示全部楼层
被戳中笑点 / 泪点,太真实了!
工控课堂 www.gkket.com

0

主题

72

回帖

110

积分

新手上路

积分
110
发表于 2025-11-15 21:30:59 | 显示全部楼层
救命!这回复笑到我捶桌,必须置顶~
工控课堂 www.gkket.com

0

主题

100

回帖

153

积分

新手上路

积分
153
发表于 2025-11-15 21:31:07 | 显示全部楼层
浅评一下:内容优质,值得推荐~
工控课堂 www.gkket.com

0

主题

98

回帖

150

积分

新手上路

积分
150
发表于 2025-11-15 21:36:04 | 显示全部楼层
我先占个楼,等下再慢慢看~
工控课堂 www.gkket.com

0

主题

142

回帖

445

积分

注册会员

积分
445
发表于 2025-11-15 21:38:33 | 显示全部楼层
不请自来,就想夸一句:太顶了!
工控课堂 www.gkket.com

0

主题

98

回帖

148

积分

新手上路

积分
148
发表于 2025-11-15 22:22:41 | 显示全部楼层
蹲一波同款,有没有小伙伴推荐?
工控课堂 www.gkket.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

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

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

GMT+8, 2025-12-22 13:34 , Processed in 0.083535 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

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