c#簡單封裝調整窗體大小不受容器遮擋
c#簡單封裝調整窗體大小不受容器遮擋
在《主互操作程式集開發Office外掛》的過程中,需要調整外掛窗體的大小,於是簡單封裝了一個類庫,只為方便使用,其他沒多研究!
使用方法:
Form ft=new Form();
ft();
CRL.Control.EasyMover em=new CRL.Control.EasyMover(ft); //ft可以是其他控制元件
//是否允許調整大小
em.CanResize=false;
//設定最小寬度、高度
em.MinWidth=300;
em.MinHeight=250;
//使子控制元件不受容器阻擋
em.ReleaseCaptureFor(子控制元件);
視訊演示:
視訊載入中...
完整程式碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Collections;
namespace CRL.Control
{
public enum MoveDirection
{
Left,Right, Top, Bottom, LeftTop, RightBottom, RightTop, LeftBottom
}
public class Win32
{
public const int WM_SYSCOMMAND=0x0112;
public const int SC_MOVE=0xF010;
public const int HTCAPTION=0x0002;
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
}
public class EasyMover
{
public EasyMover(System.Windows.Forms.Control control)
{
this.control=control;
Init();
}
private System.Windows.Forms.Control control=null;
private Point oldPoint=new Point(0, 0);
private bool canDrag=false, isMove=false,
isDrag=false, canResize=true,
isMinWidth=false, isMinHeight=false;
private int minWidth=50, minHeight=50;
private int edge=5, dy=0, dx=0,_dy=0,_dx=0;
public event MouseEventHandler MouseMove;
public event MouseEventHandler MouseDown;
public event MouseEventHandler MouseUp;
private MoveDirection direction=MoveDirection.Left;
private Cursor current=Cursors.Default;
public bool CanResize
{
get { return canResize; }
set { canResize=value; }
}
public bool IsMoved
{
get { return isMove; }
}
public bool IsDragged
{
get { return isDrag; }
}
public int MinWidth</span>
{
get { return minWidth; }
set {minWidth=value;}
}
public int MinHeight</span>
{
get { return minHeight; }
set { minHeight=value; }
}
private void Init()
{
control.MouseDown +=new MouseEventHandler(ControlMouseDown);
control.MouseMove +=new MouseEventHandler(ControlMouseMove);
control.MouseUp +=new MouseEventHandler(ControlMouseUp);
}
void ControlMouseDown(object sender, MouseEventArgs e)
{
oldPoint=Cursor.Position;
if (canResize&&canDrag)
{
isDrag=true;
isMove=false;
}
else
{
isDrag=false;
isMove=true;
}
Cursor.Current=current;
if (MouseDown !=null)
MouseDown(control,e);
}
void ControlMouseMove(object sender, MouseEventArgs e)
{
Point currPoint=Cursor.Position;
dx=currPoint.X - oldPoint.X;
dy=currPoint.Y - oldPoint.Y;
_dy=e.Y;
_dx=e.X;
if (canResize && !isDrag)
{
canDrag=true;
if (_dy < edge && _dx > edge && _dx < control.Width - edge) //top
{
direction=MoveDirection.Top;
Cursor.Current=Cursors.SizeNS;
}
else if (control.Height - _dy < edge && _dx > edge && _dx < control.Width - edge) //bottom
{
direction=MoveDirection.Bottom;
Cursor.Current=Cursors.SizeNS;
}
else if (_dx < edge && _dy > edge && _dy < control.Height - edge) //left
{
direction=MoveDirection.Left;
Cursor.Current=Cursors.SizeWE;
}
else if (control.Width - _dx < edge && _dy > edge && _dy < control.Height - edge) //right
{
direction=MoveDirection.Right;
Cursor.Current=Cursors.SizeWE;
}
else if (_dx <=edge && _dy <=edge) //左上
{
direction=MoveDirection.LeftTop;
Cursor.Current=Cursors.SizeNWSE;
}
else if (_dy >=control.Height - edge && _dx >=control.Width - edge) //右下
{
direction=MoveDirection.RightBottom;
Cursor.Current=Cursors.SizeNWSE;
}
else if (_dy <=edge && _dx >=control.Width - edge) //右上
{
direction=MoveDirection.RightTop;
Cursor.Current=Cursors.SizeNESW;
}
else if (_dx <=edge && _dy >=control.Height - edge) //左下
{
direction=MoveDirection.LeftBottom;
Cursor.Current=Cursors.SizeNESW;
}
else
{
canDrag=false;
}
current=Cursor.Current;
}
if (e.Button==MouseButtons.Left)
{
if (isMove)
{
control.Left=control.Left + dx;
control.Top=control.Top + dy;
}
if (canResize&&isDrag)
{
isMinHeight=false;
isMinWidth=false;
if (control.Width <=MinWidth)
{
isMinWidth=true;
}
if (control.Height <=MinHeight)
{
isMinHeight=true;
}
if (direction==MoveDirection.Right)
{
control.Width=control.Width + dx;
}
if (direction==MoveDirection.Left)
{
if (!isMinWidth) control.Left=control.Left + dx;
control.Width=control.Width + (-dx);
}
else if (direction==MoveDirection.Top)
{
if (!isMinHeight) control.Top=control.Top + dy;
control.Height=control.Height + (-dy);
}
else if (direction==MoveDirection.Bottom)
{
control.Height=control.Height + dy;
}
else if (direction==MoveDirection.LeftTop)
{
if (!isMinWidth) control.Left=control.Left + dx;
if (!isMinHeight) control.Top=control.Top + dy;
control.Width=control.Width + (-dx);
control.Height=control.Height + (-dy);
}
else if (direction==MoveDirection.RightTop)
{
if (!isMinHeight) control.Top=control.Top + dy;
control.Width=control.Width + dx;
control.Height=control.Height + (-dy);
}
else if (direction==MoveDirection.LeftBottom)
{
if (!isMinWidth) control.Left=control.Left + dx;
control.Width=control.Width + (-dx);
control.Height=control.Height + dy;
}
else if (direction==MoveDirection.RightBottom)
{
control.Width=control.Width + dx;
control.Height=control.Height + dy;
}
}
if (control.Width <=MinWidth)
{
control.Width=MinWidth;
}
if (control.Height <=MinHeight)
{
control.Height=MinHeight;
}
}
oldPoint=currPoint;
if (MouseMove !=null)
MouseMove(control, e);
}
void ControlMouseUp(object sender, MouseEventArgs e)
{
isMove=false;
isDrag=false;
Cursor.Current=Cursors.Default;
if (MouseUp !=null)
MouseUp(control, e);
}
public void ReleaseCaptureFor(System.Windows.Forms.Control child)
{
if (!HasControl(child,control))
throw new Exception(string.Format("設定的控制元件[{0}]不是[{1}]的任意子控制元件!",child.Name,this.control.Name));
child.MouseDown +=new MouseEventHandler(ChildControlMouseDown);
child.MouseMove +=new MouseEventHandler(ChildControlMouseMove);
child.MouseUp +=new MouseEventHandler(ChildControlMouseUp);
}
private bool HasControl(System.Windows.Forms.Control child, System.Windows.Forms.Control control)
{
foreach(System.Windows.Forms.Control c in control.Controls)
{
if (c==child)
return true;
else
if (HasControl(child,c)) return true;
}
return false;
}
void ChildControlMouseDown(object sender, MouseEventArgs e)
{
Win32.ReleaseCapture();
Win32.SendMessage(control.Handle, Win32.WM_SYSCOMMAND, Win32.SC_MOVE + Win32.HTCAPTION, 0);
}
void ChildControlMouseMove(object sender, MouseEventArgs e)
{
Win32.ReleaseCapture();
Win32.SendMessage(control.Handle, Win32.WM_SYSCOMMAND, Win32.SC_MOVE + Win32.HTCAPTION, 0);
}
void ChildControlMouseUp(object sender, MouseEventArgs e)
{
Win32.ReleaseCapture();
Win32.SendMessage(control.Handle, Win32.WM_SYSCOMMAND, Win32.SC_MOVE + Win32.HTCAPTION, 0);
}
}
}