运行时的活动控件,控件可拖拽和改变大小,鼠标在控件上游走。
当鼠标到达控件边角时,改变光标外观。
如何实现??
当鼠标到达控件边角时,改变光标外观。
如何实现??
解决方案 »
- PostMessage传递消息给窗口无反应
- Winform Combobox关联词问题
- 菜鸟询问一个C#中从数据库中取datalist和文本框做比较的问题
- 【300分】Form窗体和console控制台交互问题
- 请教listview
- 请教大家关于向DataGridView中插入新行的问题,谢谢大家!
- 在用ODBC从DBF文件中执行多条SQL指令时报错
- C# 关于flowLayoutPanel的问题
- ?请问:我在vs里面不能用asp,提示说我这这个web服务器运行的不是asp.net 1.1版。
- 请问为什么在。netC#开发环境下设计的多个frame,登陆的用户session信息老是丢失?
- 如何捕捉WINDWOS系统错误窗口
- 服务器端socket问题?高手帮帮忙。
private EnumMousePointPosition MousePointPosition(Size size,MouseEventArgs e)
{
const int band = 10;
//鼠标在size区域内时
if (e.X > -1 * band & e.X <= size.Width & e.Y >= -1 * band && e.Y <= size.Height)
{
#region//鼠标在左边时
if (e.X < band)
{
if (e.Y < band)
{
return EnumMousePointPosition.MouseSizeTopLeft;
}
else if (e.Y > -1 * band + size.Height)
{
return EnumMousePointPosition.MouseSizeBottomLeft;
}
else
{
return EnumMousePointPosition.MouseSizeLeft;
}
}
#endregion
#region//鼠标在右边时
else if (e.X > -1 * band + size.Width)
{
if (e.Y < band)
{
return EnumMousePointPosition.MouseSizeTopRight;
}
else if (e.Y > -1 * band + size.Height)
{
return EnumMousePointPosition.MouseSizeBottomRight;
}
else
{
return EnumMousePointPosition.MouseSizeRight;
}
}
#endregion
#region//鼠标在上方时
if (e.Y < band)
{
return EnumMousePointPosition.MouseSizeTop;
}
#endregion
#region//鼠标在下方时
else if (e.Y > -1*band + size.Height)
{
return EnumMousePointPosition.MouseSizeBottom;
}
#endregion
#region//鼠标不在上述区域而在Size区域内时
else
{
return EnumMousePointPosition.MouseDrag;
}
#endregion
}
//鼠标不在Size区域中
else
{
return EnumMousePointPosition.MouseSizeNone;
}
}
{
private int m_BorderWidth = 4; internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
internal static int WM_NCACTIVATE = 0x86;//窗体的激活状态发生改变的消息 internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏 internal static IntPtr HTLEFT = (IntPtr)10;//向左
internal static IntPtr HTRIGHT = (IntPtr)11;//向右
internal static IntPtr HTTOP = (IntPtr)12;//向上
internal static IntPtr HTTOPLEFT = (IntPtr)13;//向左上
internal static IntPtr HTTOPRIGHT = (IntPtr)14;//向右上
internal static IntPtr HTBOTTOM = (IntPtr)15;//向下
internal static IntPtr HTBOTTOMLEFT = (IntPtr)16;//向左下
internal static IntPtr HTBOTTOMRIGHT = (IntPtr)17;//向右下 protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if (DesignMode)
{
return;
}
if (m.Result == HTCLIENT)
{
m.HWnd = this.Handle; System.Drawing.Rectangle rect = this.RectangleToScreen(this.ClientRectangle);
Point C_Pos = Cursor.Position;
if ((Cursor.Position.X <= rect.Left + m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
m.Result = HTTOPLEFT;//左上
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
m.Result = HTTOPRIGHT;//右上
else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOMLEFT;//左下
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOMRIGHT;//右下
else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1))
m.Result = HTLEFT;//左
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth))
m.Result = HTRIGHT;//右
else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1))
m.Result = HTTOP;//上
else if ((C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOM;//下
else if (C_Pos.Y <= rect.Top + m_BorderWidth)
{
m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
}
}
return;
}
base.WndProc(ref m);
}
}
使用上可以这样://操作button1控件
MoveableWindow mw = new MoveableWindow(this.Button1);
类的代码:class MoveableWindow : NativeWindow
{
private int m_BorderWidth = 4; internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
internal static int WM_NCACTIVATE = 0x86;//窗体的激活状态发生改变的消息 internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏 internal static IntPtr HTLEFT = (IntPtr)10;//向左
internal static IntPtr HTRIGHT = (IntPtr)11;//向右
internal static IntPtr HTTOP = (IntPtr)12;//向上
internal static IntPtr HTTOPLEFT = (IntPtr)13;//向左上
internal static IntPtr HTTOPRIGHT = (IntPtr)14;//向右上
internal static IntPtr HTBOTTOM = (IntPtr)15;//向下
internal static IntPtr HTBOTTOMLEFT = (IntPtr)16;//向左下
internal static IntPtr HTBOTTOMRIGHT = (IntPtr)17;//向右下 private Control m_control;
public MoveableWindow(Control control)
{
m_control = control;
this.AssignHandle(control.Handle);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if (m.Result == HTCLIENT)
{
m.HWnd = this.Handle; System.Drawing.Rectangle rect = m_control.RectangleToScreen(m_control.ClientRectangle);
Point C_Pos = Cursor.Position;
if ((Cursor.Position.X <= rect.Left + m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
m.Result = HTTOPLEFT;//左上
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
m.Result = HTTOPRIGHT;//右上
else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOMLEFT;//左下
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOMRIGHT;//右下
else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1))
m.Result = HTLEFT;//左
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth))
m.Result = HTRIGHT;//右
else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1))
m.Result = HTTOP;//上
else if ((C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOM;//下
else
{
m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
}
}
return;
}
base.WndProc(ref m);
}
}
internal enum MouseAction
{
None,
CrossLeftTop,
CrossLeftBottom,
CrossRightTop,
CrossRightBottom,
SizeAll,
SizeLeft,
SizeTop,
SizeRight,
SizeBottom,
SizeLeftTop,
SizeRightTop,
SizeRightBottom,
SizeLeftBottom,
}private Rectangle lastBounds;
private Point lastMsPoint;
private Size lastSize;
private void label1_MouseDown(object sender, MouseEventArgs e)
{
this.lastBounds = this.label1.Bounds;
this.lastSize = this.label1.Size;
this.lastMsPoint = Control.MousePosition;
}MouseAction ma = MouseAction.None;
private void label1_MouseMove(object sender, MouseEventArgs e)
{
Point mp = Control.MousePosition;
if (!(sender as Label).Capture)
{
Rectangle bound = (sender as Label).Bounds;
ma = this.getAction(bound, this.PointToClient(mp));
this.SetCursor(ma);
return;
} if (e.Button == MouseButtons.Left)
{
switch (ma)
{
case MouseAction.SizeAll:
this.label1.Location = new Point(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeLeft:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom);
break;
case MouseAction.SizeTop:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
break;
case MouseAction.SizeRight:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
break;
case MouseAction.SizeBottom:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeLeftTop:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
break;
case MouseAction.SizeLeftBottom:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeRightBottom:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeRightTop:
this.label1.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
break;
}
}
}private MouseAction getAction(Rectangle bound, Point e)
{
int borderWidth = 6;
Rectangle outerRect = bound;
Rectangle innerRect = outerRect;
innerRect.Inflate(-borderWidth, -borderWidth);
if (innerRect.Contains(e))
{
return MouseAction.SizeAll;
}
else if (e.X > outerRect.X && e.X < innerRect.X && e.Y > outerRect.Y && e.Y < innerRect.Y)//左上
{
return MouseAction.SizeLeftTop;
}
else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y > outerRect.Y && e.Y < innerRect.Y)//右上
{
return MouseAction.SizeRightTop;
}
else if (e.X > outerRect.X && e.X < innerRect.X && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//左下
{
return MouseAction.SizeLeftBottom;
}
else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//右下
{
return MouseAction.SizeRightBottom;
}
else if (e.X > outerRect.X && e.X < innerRect.X)
{
return MouseAction.SizeLeft;
}
else if (e.X < outerRect.Right && e.X > innerRect.Right)
{
return MouseAction.SizeRight;
}
else if (e.Y > outerRect.X && e.Y < innerRect.Y)
{
return MouseAction.SizeTop;
}
else if (e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)
{
return MouseAction.SizeBottom;
}
return MouseAction.None;
}
private void SetCursor(MouseAction action)
{
switch (action)
{
case MouseAction.None:
this.Cursor = Cursors.Default;
break;
case MouseAction.CrossLeftBottom:
this.Cursor = Cursors.Cross;
break;
case MouseAction.CrossLeftTop:
this.Cursor = Cursors.Cross;
break;
case MouseAction.CrossRightBottom:
this.Cursor = Cursors.Cross;
break;
case MouseAction.CrossRightTop:
this.Cursor = Cursors.Cross;
break;
case MouseAction.SizeAll:
this.Cursor = Cursors.SizeAll;
break;
case MouseAction.SizeBottom:
this.Cursor = Cursors.SizeNS;
break;
case MouseAction.SizeLeft:
this.Cursor = Cursors.SizeWE;
break;
case MouseAction.SizeLeftBottom:
this.Cursor = Cursors.SizeNESW;
break;
case MouseAction.SizeLeftTop:
this.Cursor = Cursors.SizeNWSE;
break;
case MouseAction.SizeRight:
this.Cursor = Cursors.SizeWE;
break;
case MouseAction.SizeRightBottom:
this.Cursor = Cursors.SizeNWSE;
break;
case MouseAction.SizeRightTop:
this.Cursor = Cursors.SizeNESW;
break;
case MouseAction.SizeTop:
this.Cursor = Cursors.SizeNS;
break;
default:
this.Cursor = Cursors.Default;
break;
}
}
要做的只是把你的原来的Label类型改成新的Label类型,类代码如下:
internal class SizeableLabel : Label
{
internal enum MouseAction
{
None,
CrossLeftTop,
CrossLeftBottom,
CrossRightTop,
CrossRightBottom,
SizeAll,
SizeLeft,
SizeTop,
SizeRight,
SizeBottom,
SizeLeftTop,
SizeRightTop,
SizeRightBottom,
SizeLeftBottom,
}
private Rectangle lastBounds;
private Point lastMsPoint;
private MouseAction ma = MouseAction.None; protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.lastBounds = this.Bounds;
this.lastMsPoint = this.Parent.PointToClient(Control.MousePosition);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
Point mp = this.Parent.PointToClient(Control.MousePosition);
if (!this.Capture)
{
Rectangle bound = this.Bounds;
ma = this.getAction(bound, mp);
this.setCursor(ma);
return;
} if (e.Button == MouseButtons.Left)
{
switch (ma)
{
case MouseAction.SizeAll:
this.Location = new Point(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeLeft:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom);
break;
case MouseAction.SizeTop:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
break;
case MouseAction.SizeRight:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
break;
case MouseAction.SizeBottom:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeLeftTop:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right, lastBounds.Bottom);
break;
case MouseAction.SizeLeftBottom:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X + mp.X - this.lastMsPoint.X, this.lastBounds.Y, lastBounds.Right, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeRightBottom:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom + mp.Y - this.lastMsPoint.Y);
break;
case MouseAction.SizeRightTop:
this.Bounds = Rectangle.FromLTRB(this.lastBounds.X, this.lastBounds.Y + mp.Y - this.lastMsPoint.Y, lastBounds.Right + mp.X - this.lastMsPoint.X, lastBounds.Bottom);
break;
}
}
}
private MouseAction getAction(Rectangle bound, Point e)
{
int borderWidth = 6;
Rectangle outerRect = bound;
Rectangle innerRect = outerRect;
innerRect.Inflate(-borderWidth, -borderWidth);
if (innerRect.Contains(e))
{
return MouseAction.SizeAll;
}
else if (e.X > outerRect.X && e.X < innerRect.X && e.Y > outerRect.Y && e.Y < innerRect.Y)//左上
{
return MouseAction.SizeLeftTop;
}
else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y > outerRect.Y && e.Y < innerRect.Y)//右上
{
return MouseAction.SizeRightTop;
}
else if (e.X > outerRect.X && e.X < innerRect.X && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//左下
{
return MouseAction.SizeLeftBottom;
}
else if (e.X < outerRect.Right && e.X > innerRect.Right && e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)//右下
{
return MouseAction.SizeRightBottom;
}
else if (e.X > outerRect.X && e.X < innerRect.X)
{
return MouseAction.SizeLeft;
}
else if (e.X < outerRect.Right && e.X > innerRect.Right)
{
return MouseAction.SizeRight;
}
else if (e.Y > outerRect.X && e.Y < innerRect.Y)
{
return MouseAction.SizeTop;
}
else if (e.Y < outerRect.Bottom && e.Y > innerRect.Bottom)
{
return MouseAction.SizeBottom;
}
return MouseAction.None;
} private void setCursor(MouseAction action)
{
switch (action)
{
case MouseAction.None:
this.Cursor = Cursors.Default;
break;
case MouseAction.CrossLeftBottom:
this.Cursor = Cursors.Cross;
break;
case MouseAction.CrossLeftTop:
this.Cursor = Cursors.Cross;
break;
case MouseAction.CrossRightBottom:
this.Cursor = Cursors.Cross;
break;
case MouseAction.CrossRightTop:
this.Cursor = Cursors.Cross;
break;
case MouseAction.SizeAll:
this.Cursor = Cursors.SizeAll;
break;
case MouseAction.SizeBottom:
this.Cursor = Cursors.SizeNS;
break;
case MouseAction.SizeLeft:
this.Cursor = Cursors.SizeWE;
break;
case MouseAction.SizeLeftBottom:
this.Cursor = Cursors.SizeNESW;
break;
case MouseAction.SizeLeftTop:
this.Cursor = Cursors.SizeNWSE;
break;
case MouseAction.SizeRight:
this.Cursor = Cursors.SizeWE;
break;
case MouseAction.SizeRightBottom:
this.Cursor = Cursors.SizeNWSE;
break;
case MouseAction.SizeRightTop:
this.Cursor = Cursors.SizeNESW;
break;
case MouseAction.SizeTop:
this.Cursor = Cursors.SizeNS;
break;
default:
this.Cursor = Cursors.Default;
break;
}
}
}