WinForm限制窗體不能移到屏幕外的方法
更新時間:2015年08月20日 16:07:50 作者:我心依舊
這篇文章主要介紹了WinForm限制窗體不能移到屏幕外的方法,實例分析了C#中WinForm窗體操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了WinForm限制窗體不能移到屏幕外的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Runtime.InteropServices; namespace AppForm { /// <summary> /// WinForm限制窗體不能移到屏幕外 /// </summary> public class FrmBase : 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); } } } }
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
詳解如何通過wireshark實現(xiàn)捕獲C#上傳的圖片
這篇文章主要為大家詳細介紹了如何通過wireshark實現(xiàn)捕獲C#上傳的圖片,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解下2023-11-11C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序
這篇文章介紹了C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04基于Kubernetes實現(xiàn)前后端應(yīng)用的金絲雀發(fā)布(兩種方案)
這篇文章主要介紹了基于Kubernetes實現(xiàn)前后端應(yīng)用的金絲雀發(fā)布,文中給大家提到了兩種常用方案,通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-12-12C#影院售票系統(tǒng)畢業(yè)設(shè)計(4)
這篇文章主要介紹了C#影院售票系統(tǒng)畢業(yè)設(shè)計,學(xué)習(xí)內(nèi)容是總結(jié)銷售信息的保存以及加載銷售信息,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-11-11C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實例
這篇文章主要介紹了C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實例,LINQ to XML是.NET框架中集成的接口,可以將XML數(shù)據(jù)放到內(nèi)存中進行處理,需要的朋友可以參考下2016-03-03