C#窗體越界時滑鼠還能回到初始坐標位置

来源:https://www.cnblogs.com/qiantao/archive/2018/08/24/9531532.html
-Advertisement-
Play Games

對窗體加越界限制後,滑鼠拖動窗體越界時,窗體不能動,滑鼠位置可動,但窗體不再越界時,滑鼠位置還能回到滑鼠按下時相對窗體的坐標:1、首先創建一個窗體Form1,然後在窗體上拖一個button1按鈕(主要通過這個按鈕來拖動窗體)2、然後對按鈕添加滑鼠按下事件、滑鼠移動事件和滑鼠抬起事件,事件裡面是對窗體 ...


對窗體加越界限制後,滑鼠拖動窗體越界時,窗體不能動,滑鼠位置可動,但窗體不再越界時,滑鼠位置還能回到滑鼠按下時相對窗體的坐標:
1、首先創建一個窗體Form1,然後在窗體上拖一個button1按鈕(主要通過這個按鈕來拖動窗體)

2、然後對按鈕添加滑鼠按下事件、滑鼠移動事件和滑鼠抬起事件,事件裡面是對窗體和滑鼠坐標的操作,整體代碼如下:
using System.Drawing;
using System.Windows.Forms;
namespace 窗體越界滑鼠還能回到初始坐標
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        #region 變數定義
        public Point mainFormPosition;//主視窗左上角位置
        int workingAreaHeight = SystemInformation.WorkingArea.Height;
        int workingAreaWidth = SystemInformation.WorkingArea.Width;
        private Point relativetMousePosition;//滑鼠相對窗體的相對坐標
        bool FirstLeft = false;//是否左邊第一次按下時坐標
        bool FirstRight = false;//是否右邊第一次按下時坐標
        bool FirstUp = false;//是否上邊第一次按下時坐標
        bool FirstDown = false;//是否下邊第一次按下時坐標
        bool isFirstDown = true;//是否第一次按下,滑鼠越界糾正時判斷是否是第一次按下,不是按下後又拖動了
        bool isMoveOut = false;//是否拖出
        //這兩個變數做差,差值為拖出的距離時軟體彈出側邊
        int oldMove_X = 0;//側邊停靠拖出時起始坐標
        //上一個時刻、滑鼠的位置
        private Point oldMousePosition;
        //當前時刻、滑鼠的位置
        private Point currentMousePosition;
        //滑鼠事件
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        //移動滑鼠
        const int MOUSEEVENTF_MOVE = 0x0001;
        //模擬滑鼠左鍵按下
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        //模擬滑鼠左鍵抬起
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        //模擬滑鼠右鍵按下
        const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        //模擬滑鼠右鍵抬起
        const int MOUSEEVENTF_RIGHTUP = 0x0010;
        //模擬滑鼠中鍵按下
        const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        //模擬滑鼠中鍵抬起
        const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        //標示是否採用絕對坐標
        const int MOUSEEVENTF_ABSOLUTE = 0x8000;
        #endregion

        #region 窗體初始化
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

        //-------------------------按鈕button1滑鼠事件---------------------------------------------------------------
        #region 滑鼠左鍵按下Button1按鈕事件
        private void Button1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentMousePosition = Control.MousePosition;
                oldMousePosition = currentMousePosition;   //滑鼠按下時、兩個時刻的坐標變成一樣,防止錯誤移動            
                mainFormPosition = Location;
                if (isFirstDown)//滑鼠按下時,記錄第一次滑鼠按下相對主窗體的坐標
                {
                    relativetMousePosition.X = currentMousePosition.X - Location.X;
                    relativetMousePosition.Y = currentMousePosition.Y - Location.Y;
                    isFirstDown = false;
                }
                if (isMoveOut)//滑鼠按下時,記錄拖出時的初始坐標
                {
                    oldMove_X = currentMousePosition.X;
                }
            }
        }
        #endregion

        #region 滑鼠左鍵按住Button1按鈕移動事件
        private void Button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                currentMousePosition = Control.MousePosition;
                if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y > relativetMousePosition.Y && currentMousePosition.Y < workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//左邊窗體越界坐標糾正
                {
                    mainFormPosition.X = 0;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstLeft = true;
                }
                else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y > relativetMousePosition.Y && currentMousePosition.Y < workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//右邊窗體越界坐標糾正
                {
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstRight = true;
                }
                else if (currentMousePosition.Y < relativetMousePosition.Y && currentMousePosition.X > relativetMousePosition.X && currentMousePosition.X < workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X)//上邊窗體越界坐標糾正
                {
                    mainFormPosition.Y = 0;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.X = currentMousePosition.X;
                    FirstUp = true;
                }
                else if (currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y && currentMousePosition.X > relativetMousePosition.X && currentMousePosition.X < workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X)//下邊窗體越界坐標糾正
                {
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.X = currentMousePosition.X;
                    FirstDown = true;
                }
                else if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y < relativetMousePosition.Y)//左上角窗體越界坐標糾正
                {
                    mainFormPosition.X = 0;
                    mainFormPosition.Y = 0;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstLeft = true;
                    FirstUp = true;
                }
                else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y < relativetMousePosition.Y)//右上角窗體越界坐標糾正
                {
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                    mainFormPosition.Y = 0;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstRight = true;
                    FirstUp = true;
                }
                else if (currentMousePosition.X < relativetMousePosition.X && currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//左下角窗體越界坐標糾正
                {
                    mainFormPosition.X = 0;
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstLeft = true;
                    FirstDown = true;
                }
                else if (currentMousePosition.X > workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X && currentMousePosition.Y > workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y)//右下角窗體越界坐標糾正
                {
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                    mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                    mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                    //保存上一個時刻的滑鼠位置
                    oldMousePosition.X = currentMousePosition.X;
                    oldMousePosition.Y = currentMousePosition.Y;
                    FirstRight = true;
                    FirstDown = true;
                }
                if (FirstLeft)
                {
                    currentMousePosition.X = relativetMousePosition.X;
                    FirstLeft = false;
                }
                if (FirstRight)
                {
                    currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                    FirstRight = false;
                }
                if (FirstUp)
                {
                    currentMousePosition.Y = relativetMousePosition.Y;
                    FirstUp = false;
                }
                if (FirstDown)
                {
                    currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                    FirstDown = false;
                }
                if (FirstLeft && FirstUp)
                {
                    currentMousePosition.X = relativetMousePosition.X;
                    currentMousePosition.Y = relativetMousePosition.Y;
                    FirstLeft = false;
                    FirstUp = false;

                }
                if (FirstRight && FirstUp)
                {
                    currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                    currentMousePosition.Y = relativetMousePosition.Y;
                    FirstUp = false;
                    FirstRight = false;
                }
                if (FirstLeft && FirstDown)
                {
                    currentMousePosition.X = relativetMousePosition.X;
                    currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                    FirstLeft = false;
                    FirstDown = false;
                }
                if (FirstRight && FirstDown)
                {
                    currentMousePosition.X = workingAreaWidth - this.ClientSize.Width + relativetMousePosition.X;
                    currentMousePosition.Y = workingAreaHeight - this.ClientSize.Height + relativetMousePosition.Y;
                    FirstRight = false;
                    FirstDown = false;
                }
                //對窗體進行移動
                mainFormPosition.X += currentMousePosition.X - oldMousePosition.X;
                mainFormPosition.Y += currentMousePosition.Y - oldMousePosition.Y;
                //保存上一個時刻的滑鼠位置
                oldMousePosition = currentMousePosition;

                #region 越界處理
                if (mainFormPosition.X < 0)//左邊越界
                {
                    mainFormPosition.X = 0;
                }
                else if (mainFormPosition.Y < 0)//上邊越界
                {
                    mainFormPosition.Y = 0;
                }
                else if (mainFormPosition.X > workingAreaWidth - this.ClientSize.Width)//右邊越界
                {
                    mainFormPosition.X = workingAreaWidth - this.ClientSize.Width;
                }
                else if (mainFormPosition.Y > workingAreaHeight - this.ClientSize.Height)//下邊越界
                {
                    mainFormPosition.Y = workingAreaHeight - this.ClientSize.Height;
                }
                //更新主視窗位置
                Location = mainFormPosition;
                #endregion
            }
        }
        #endregion

        #region 滑鼠在按鈕Button1抬起事件
        private void Button1_MouseUp(object sender, MouseEventArgs e)
        {
            isFirstDown = true;
            //如果主窗體在這個位置,就判斷允許主窗體在滑鼠抬起後執行移動事件
            if (mainFormPosition.X == workingAreaWidth - this.ClientSize.Width)
            {
                isMoveOut = false;
            }
            if (mainFormPosition.X == 0)
            {
                isMoveOut = false;
            }
        }
        #endregion
    }
}

動畫效果如下:

 

這是本人在一個項目中遇到的問題,所以特意總結出來分享給大家。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 正則表達式的用法與案例分析 2018-08-24 21:26:14 【說明】:該文主要為了隨後複習和使用備查,由於做了word文檔筆記,所以此處博文沒有怎麼排版,沒放代碼,以插入圖片為主, 一、正則表達式之特殊字元 註意: 以下的案例中是match()匹配,match是要求從第一個字元開始匹配,所以 ...
  • Flask Flask是一個基於Python開發並且依賴jinja2模板和Werkzeug WSGI服務的一個微型框架,對於Werkzeug本質是Socket服務端,其用於接收http請求並對請求進行預處理,然後觸發Flask框架,開發人員基於Flask框架提供的功能對請求進行相應的處理,並返回給用 ...
  • 01.1 Windows環境下JDK安裝與環境變數配置詳細的圖文教程 本節內容:JDK安裝與環境變數配置 以下是詳細步驟 一、準備工具: 1.JDK JDK 可以到官網下載 "http://www.oracle.com/technetwork/java/javase/downloads/jdk8 d ...
  • 前言 眾所周知,Java中有多種針對文件的操作類,以面向位元組流和字元流可分為兩大類,這裡以寫入為例: 面向位元組流的:FileOutputStream 和 BufferedOutputStream 面向字元流的:FileWriter 和 BufferedWriter 近年來發展出New I/O ,也叫 ...
  • 今日內容介紹 1、Java開發環境搭建 2、HelloWorld案例 3、註釋、關鍵字、標識符 4、數據(數據類型、常量) ...
  • ASP.NET Core MVC中的Filter作用是在請求處理管道的某些階段之前或之後可以運行特定的代碼。 Filter特性在之前的ASP.NET MVC中已經出現,但過去只有Authorization,Exception,Action,Result四種類型,現在又增加了一種Resource類型。 ...
  • *必須要有無線網卡才能設置WIFI首先打開電腦,選中“電腦”或者“我的電腦”,右擊進入“管理”選項“。打開“電腦管理”視窗之後,在左欄菜單選項中找到“服務和應用程式”下的“服務”選項,如圖點擊進入“服務”列表。然後在服務列表最下方找到“WLAN AutoConfig”服務和“WWAN AutoC ...
  • win10在設置顏色里有個從“背景圖片中選取一種主題顏色”的選項,還有在很多內容展示軟體中都使用了這樣的功能。 現在我們需要在 nuget 引用 win2d.uwp 和 Toolkit.uwp 兩個庫。 首先將一個圖片流轉換成 win2d 的 CanvasBitmap 對象,然後通過 CanvasB ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...