如何让自定义控件支持鼠标拖动右小角让控件变大变小?
有没有直接的办法?比如继承.net framework中的某个类
------解决方案--------------------鼠标不一定一定要在控件的某个角上才能调整它的大小,包括可以调整大小的Form,它也是要鼠标在它的可见区域以内才可以调整大小,不过是有一个边的大小,只要鼠标在控件的可见区域以内且小于指定的边的大小就可以被视为控件是可以被鼠标调整的.如果你一定要做的像Windows自带的那样的效果,你可以参见下面的代码:
class MoveableControl:UserControl
{
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 int m_BorderWidth = 4;
private bool m_Sizeable = true;
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 ((C_Pos.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);
}
}