<video id="nbppf"></video>
<video id="nbppf"><dl id="nbppf"><delect id="nbppf"></delect></dl></video>
<dl id="nbppf"></dl>
<dl id="nbppf"><delect id="nbppf"></delect></dl><dl id="nbppf"><delect id="nbppf"><meter id="nbppf"></meter></delect></dl>
<video id="nbppf"></video>
<dl id="nbppf"></dl><dl id="nbppf"></dl><address id="nbppf"><video id="nbppf"></video></address>
<dl id="nbppf"></dl>
<output id="nbppf"></output>
<output id="nbppf"><delect id="nbppf"></delect></output><dl id="nbppf"><delect id="nbppf"><meter id="nbppf"></meter></delect></dl>
<dl id="nbppf"></dl><dl id="nbppf"></dl>
<dl id="nbppf"></dl>
<video id="nbppf"></video>
<dl id="nbppf"></dl><dl id="nbppf"><delect id="nbppf"><meter id="nbppf"></meter></delect></dl>
<video id="nbppf"><output id="nbppf"><font id="nbppf"></font></output></video>
<output id="nbppf"><font id="nbppf"></font></output>

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

發布時間:2021-03-30 20:52 作者:獨孤劍 閱讀:2325

添加一個類文件"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();
        }
    }
調試運行,窗體只能在屏幕內部來回拖動,無法拖拽到屏幕外。


微信打賞, 微信掃一掃

支付寶打賞, 支付寶掃一掃

如果文章對您有幫助,歡迎給作者打賞

作者最新文章
開發過程中解決360兼容模式瀏覽器的方法
云南象群向西南方向遷移,云南離群獨象距離象群約12公里
吉林做網站最低價格,吉林企業網站建設價格低至500元起
守象人直擊云南象群最新動向
網站影響百度蜘蛛抓取量的因素有哪些?為什么我的網站Baidu蜘蛛來的次數少?
企業名片
在線客服
天天干天天在线观看_国产精品无码天堂2021_日韩特黄美女自慰大全_欧美老妇乱色老头与老妇
<video id="nbppf"></video>
<video id="nbppf"><dl id="nbppf"><delect id="nbppf"></delect></dl></video>
<dl id="nbppf"></dl>
<dl id="nbppf"><delect id="nbppf"></delect></dl><dl id="nbppf"><delect id="nbppf"><meter id="nbppf"></meter></delect></dl>
<video id="nbppf"></video>
<dl id="nbppf"></dl><dl id="nbppf"></dl><address id="nbppf"><video id="nbppf"></video></address>
<dl id="nbppf"></dl>
<output id="nbppf"></output>
<output id="nbppf"><delect id="nbppf"></delect></output><dl id="nbppf"><delect id="nbppf"><meter id="nbppf"></meter></delect></dl>
<dl id="nbppf"></dl><dl id="nbppf"></dl>
<dl id="nbppf"></dl>
<video id="nbppf"></video>
<dl id="nbppf"></dl><dl id="nbppf"><delect id="nbppf"><meter id="nbppf"></meter></delect></dl>
<video id="nbppf"><output id="nbppf"><font id="nbppf"></font></output></video>
<output id="nbppf"><font id="nbppf"></font></output>