WinForm限制窗體不能移到屏幕外的方法
本文實(shí)例講述了WinForm限制窗體不能移到屏幕外的方法。分享給大家供大家參考。具體實(shí)現(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);
}
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#中實(shí)現(xiàn)Json序列化與反序列化的幾種方式
C#中實(shí)現(xiàn)Json的序列化與反序列化也算是個(gè)老話題,那么在這篇文章中我們將老話重提,本文中將會(huì)學(xué)到如何使用C#,來序列化對(duì)象成為Json格式的數(shù)據(jù),以及如何反序列化Json數(shù)據(jù)到對(duì)象。有需要的朋友們可以參考借鑒,下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2016-12-12
詳解如何通過wireshark實(shí)現(xiàn)捕獲C#上傳的圖片
這篇文章主要為大家詳細(xì)介紹了如何通過wireshark實(shí)現(xiàn)捕獲C#上傳的圖片,文中的示例代碼簡潔易懂,具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-11-11
C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序
這篇文章介紹了C#構(gòu)造函數(shù)在基類和父類中的執(zhí)行順序,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
基于Kubernetes實(shí)現(xiàn)前后端應(yīng)用的金絲雀發(fā)布(兩種方案)
這篇文章主要介紹了基于Kubernetes實(shí)現(xiàn)前后端應(yīng)用的金絲雀發(fā)布,文中給大家提到了兩種常用方案,通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(4)
這篇文章主要介紹了C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì),學(xué)習(xí)內(nèi)容是總結(jié)銷售信息的保存以及加載銷售信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-11-11
C#添加Windows服務(wù) 定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了C#添加Windows服務(wù),定時(shí)任務(wù)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實(shí)例
這篇文章主要介紹了C#程序中使用LINQ to XML來查詢XML格式數(shù)據(jù)的實(shí)例,LINQ to XML是.NET框架中集成的接口,可以將XML數(shù)據(jù)放到內(nèi)存中進(jìn)行處理,需要的朋友可以參考下2016-03-03
C#實(shí)現(xiàn)驗(yàn)證身份證是否合法的方法
這篇文章主要介紹了C#實(shí)現(xiàn)驗(yàn)證身份證是否合法的方法,實(shí)例分析了通過自定義函數(shù)實(shí)現(xiàn)針對(duì)身份證合法性驗(yàn)證的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03

