日期:2014-05-19  浏览次数:20784 次

c# 窗体上控件如何多选并且可移动
好比窗口上有2个按钮控件,我们知道,在设计状态下,可以同时选中这2个按钮并且移动他们。
现在我想问,在程序运行状态下,如果达到同样效果。
各位高手,给点意见,最好给个例程序给小弟我研究研究。
邮箱:dixianming@yahoo.com.cn
谢谢

------解决方案--------------------
using System;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace testmovecontrol
{
class MoveableControl:Button
{
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) && this.m_Sizeable)
m.Result = HTTOPLEFT;//左上
else if ((C_Pos.X > = rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth) && this.m_Sizeable)
m.Result = HTTOPRIGHT;//右上
else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y > = rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)
m.Result = HTBOTTOMLEFT;//左下
else if ((C_Pos.X > = rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y > = rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)
m.Result = HTBOTTOMRIGHT;//右下
else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1) && this.m_Sizeable)
m.Result = HTLEFT;//左
else if ((C_Pos.X > = rect.Left + rect.Width - m_BorderWidth) && this.m_Sizeable)
m.Result = HTRIGHT;//右
else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1) && this.m_Sizeable)
m.Result = HTTOP;//上
else if ((C_Pos.Y > = rect.Top + rect.Height - m_BorderWidth) && this.m_Sizeable)
m.Result = HTBOTTOM;//下
else
{
m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
}
}
return;
}
base.WndProc(ref m);
}
}
}

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace EastTicket.CustotmCol
{
class MovePanel:Panel
{
bool whetherSelected = false;
Point p = new Point();
public bool WhetherSelected
{
get { return whetherSelected; }
}


protected override void OnCreateControl()
{
base.OnCreateControl();
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);