我想做的是,比如在窗体一侧有Button、label、textbox等控件,可以拖动其到窗体指定范围生成新控件,并且通过鼠标、键盘移动新生成控件的位置。
现在创建控件我知道怎么做,移动指定控件也知道怎么做,但不知道我创建后的控件该怎么移动?
主要是移动控件要知道是要移动哪个控件,比如Button1,可以通过设置其Top,Left属性移动。但是,程序运行后生成的控件,我怎么设定其属性?
我该怎么捕捉移动的是哪个控件,并设置其属性呢?
怎么实现类似设计器中的用鼠标多选控件并移动其位置的功能(用鼠标和键盘控制)?
哦,还有,移动button可以用e.Data.GetData(typeof(Button))来知道移动的是哪个button,用e.Data.GetData(typeof(Label))知道移动的是哪个label
但当button、label、textbox等都存在窗体中时,我怎么知道移动的是哪个控件?
我邮箱[email protected],哪位大侠有源码麻烦给发下哈

解决方案 »

  1.   

    使用skinfeature界面换肤组件啊。 完全支持c#的。界面精致,而且效果很棒!! 而且基本不需要编写代码就能实现播放器界面。
    www.skinfeature.com 
      

  2.   

    我现在知道如何得知移动的是什么类型的控件了。声明个公共type类型的数据,然后在移动控件时先给该数据赋值就好了
      

  3.   

    正在关于这个方面。希望可以探讨
    [email protected]
      

  4.   

    http://blog.csdn.net/csharp_start/archive/2008/08/21/2806959.aspx
      

  5.   

    直接用FORM的话,不是很容易作到,通常,我们都采用创建一个容器(当然这个容器也可以是FORM),在容器中定义一个
    选中的控件集合,你在拖动鼠标范围的时候,会有一个矩形范围,这个范围与控件所在位置的重叠区域可以判断是哪些控件在刚才鼠标拖动的范围内,这个时候,你就要在数组中加入这些控件,并绘制他们被选中时的8个选择点.
    移动的时候,遍历这个数组,来达到所有选择控件的移动,缩放同理.如果是用键盘控制,判断键盘消息,操作比较类似.
    对齐的操作与移动比较相似,无非是找一个对齐基点,这个基点可以是第一个被选中的控件,也可以是最后一个.
      

  6.   

    建议参考WORD的自动化对象开发 
    做个像vs2005一样的东东吧
      

  7.   

    MSDN杂志上有一篇关于这个内容的文章
      

  8.   

    这个功能你在.net中做是选对了,在.net中,通过应用框架包中现成的功能,可以很容易的实现你所需要的东西。
      

  9.   

    #region ' >> using using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Drawing;
    using System.Windows.Forms;#endregion
    namespace MovableControllerTest
    {
    /// <summary>
    /// 用于相关控件的大小变更和移动</summary>
    ///
    public class MovableController
    {
            
            //// constructor 
            //#region ' + constructor () 
            ///// <summary>
            ///// MovableController 初始化新实例</summary>
            /////
            //public MovableController()
            //{
            //}
            //#endregion
            
            
    // field 
    #region ' >> field 
            
    private Control _control; private bool _isDraggingForResizing = false;
    private bool _isDraggingForMove = false; private int _resizingBorderWidth = 5;
    private Size _minimumSize = new Size( 39, 20 );//控件最小尺寸 private Size _controlBeforeSize;//移动之前控件大小
    private Point _controlBeforeLocation;//移动之前控件的坐标
    private Point _cursorBeforeLocation; private LocationKind _draggingKind;//鼠标的种类

    private ArrayList _attachedControls = new ArrayList();//控件动态数组 #endregion
            
            
    // property 
    #region ' + ResizingBorderWidth : int { get,set } 
    /// <summary>
    /// 为变更控件大小 取得或设置边界宽度
    /// 默认值为10</summary>
    /// 
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// value小于0</exception>
    ///
    public int ResizingBorderWidth
    {
    get { return _resizingBorderWidth; }
    set
    {
    if ( value <= 0 ) throw new ArgumentOutOfRangeException( "小于0" ); _resizingBorderWidth = value;
    }
    }
    #endregion #region ' + MinimumSize : Size { get,set } 
    /// <summary>
            /// 取得或设置变更控件大小时的最小值</summary>
    /// <res>
    /// 由于用于大小变更的位置种类有9种
    /// 要设定的大小的高度和宽度都必须是ResizingBorderWidth的3倍以上</res>
    ///
    /// <exception cref="System.ArgumentOutOfRangeException">
    /// value 的 Height 属性未到 ResizingBorderWidth 的 3 倍
            /// value 的 Width 属性未到 ResizingBorderWidth 的 3 倍</exception>
    /// 
    public Size MinimumSize
    {
    get { return _minimumSize; }
    set
    {
                    if (value.Height < ResizingBorderWidth * 3) throw new ArgumentOutOfRangeException("Height 属性未到 ResizingBorderWidth 的 3 倍");
                    if (value.Width < ResizingBorderWidth * 3) throw new ArgumentOutOfRangeException("Width 属性未到 ResizingBorderWidth 的 3 倍"); _minimumSize = value;
    }
    }
    #endregion
    #region '   - AttachedControls : ArrayList { get } 
    /// <summary>
    /// 取得相关控件的集合</summary>
    ///
    private ArrayList AttachedControls
    {
    get { return _attachedControls; }
    }
    #endregion
    #region '   - Control : Control { get,set } 
    /// <summary>
    /// 取得或设定控件</summary>
    ///
    private Control Control
    {
    get { return _control; }
    set
    {
    if ( value == null ) throw new ArgumentNullException( "null 偱偡丅" ); _control = value;
    }
    }
    #endregion
    #region '   - ControlBeforeLocation : Point { get,set } 
    /// <summary>
    /// 取得或设定控件变更前的位置</summary>
    ///
    private Point ControlBeforeLocation
    {
    get { return _controlBeforeLocation; }
    set
    {
    // debug 
    Debug.WriteLine( "ControlBeforeLocation set:" );
    Debug.WriteLine( "    X:" + value.X.ToString() );
    Debug.WriteLine( "    Y:" + value.Y.ToString() ); _controlBeforeLocation = value;
    }
    }
    #endregion
    #region '   - ControlBeforeSize : Size { get,set } 
    /// <summary>
            /// 取得或设定控件变更前的大小</summary>
    ///
    private Size ControlBeforeSize
    {
    get { return _controlBeforeSize; }
    set
    {
    // debug 
    Debug.WriteLine( "ControlBeforeSize set:" );
    Debug.WriteLine( "    Width : " + value.Width.ToString() );
    Debug.WriteLine( "    Height: " + value.Height.ToString() ); _controlBeforeSize = value;
    }
    }
    #endregion
    #region '   - CursorBeforeLocation : Point { get,set } 
    /// <summary>
            /// 取得或设定控件变更前的鼠标指针位置</summary>
    ///
    private Point CursorBeforeLocation
    {
    get { return _cursorBeforeLocation; }
    set
    {
    // debug 
    Debug.WriteLine( "CursorBeforeLocation set:" );
    Debug.WriteLine( "    X: " + value.X.ToString() );
    Debug.WriteLine( "    Y: " + value.Y.ToString() ); _cursorBeforeLocation = value;
    }
    }
    #endregion
    #region '   - DraggingKind : LocationKind { get,set } 
    /// <summary>
            /// 取得或设定由拖拽状态所表示的位置种类</summary>
    ///
    private LocationKind DraggingKind
    {
    get { return _draggingKind; }
    set { _draggingKind = value; }
    }
    #endregion
    #region '   - IsDraggingForResizing : bool { get,set } 
    /// <summary>
    /// 取得或设定为变更控件大小而显示该控件是否正在被拖拽的值</summary>
    /// 
    private bool IsDraggingForResizing
    {
    get { return _isDraggingForResizing; }
    set
    {
    // debug 
    Debug.WriteLine( "IsDraggingForResizing set: " + value.ToString() ); _isDraggingForResizing = value;
    }
    }
    #endregion
    #region '   - IsDraggingForMove : bool { get,set } 
    /// <summary>
            /// 取得或设定为移动控件而显示该控件是否正在被拖拽的值</summary>
    ///
    private bool IsDraggingForMove
    {
    get { return _isDraggingForMove; }
    set
    {
    // debug 
    Debug.WriteLine( "IsDraggingForMove set: " + value.ToString() ); _isDraggingForMove = value;
    }
    }
    #endregion
      

  10.   

    // method 
    #region ' + Attach ( Control ) : void 
    /// <summary>
    /// 关联控件
    /// 忽视已被关联的控件</summary>
    /// <res>
    /// 相关联的控件的高度或宽度未到 MinimumSize 属性值时
            /// 自动将其设定为MinimumSize值</res>
    ///
    ///     <param name="anAttachingControl">
            ///     相关联控件</param>
    /// 
    /// <exception cref="System.ArgumentNullException">
    /// anAttachingControl 值为 null</exception>
    /// 
    public void Attach( Control anAttachingControl )
    {
    if ( anAttachingControl == null ) throw new ArgumentNullException( "null" );

    if ( IsUnderMinimumSize( anAttachingControl.Size ) )
    {
    if ( anAttachingControl.Size.Height < MinimumSize.Height )
    {
    anAttachingControl.Size = new Size( anAttachingControl.Height, MinimumSize.Height );
    }
    if ( anAttachingControl.Size.Width < MinimumSize.Width )
    {
    anAttachingControl.Size = new Size( MinimumSize.Width, anAttachingControl.Width );
    }
    } if ( AttachedControls.Contains( anAttachingControl ) == false )
    {
    AttachedControls.Add( anAttachingControl );
    AttachControlEvent( anAttachingControl );
    } }
    #endregion
    #region ' + Detach ( Control ) : void 
    /// <summary>
            /// 解除相关联的控件
            /// 忽视已解除关联的控件</summary>
    ///
    ///     <param name="anAttachControl">
            ///     解除关联的控件</param>
    ///
    /// <exception cref="System.ArgumentNullException">
    /// aDetachingControl 值为 null</exception>
    ///
    public void Detach( Control aDetachingControl )
    {
    if ( aDetachingControl == null ) throw new ArgumentNullException( "null" ); if ( AttachedControls.Contains( aDetachingControl ) )
    {
    AttachedControls.Remove( aDetachingControl );
    DetachControlEvent( aDetachingControl );
    aDetachingControl.Cursor = Cursors.Default;
    } }
    #endregion
    #region '   - AttachControlEvent ( Control ) : void 
    /// <summary>
    /// 注册控件事件</summary>
    ///
    private void AttachControlEvent( Control aTargetControl )
    {
    aTargetControl.MouseDown += new MouseEventHandler( this.AttachedControl_MouseDown );
    aTargetControl.MouseMove += new MouseEventHandler( this.AttachedControl_MouseMove );
    aTargetControl.MouseUp += new MouseEventHandler( this.AttachedControl_MouseUp );
    aTargetControl.Paint += new PaintEventHandler( this.AttachedControl_Paint );
    }
    #endregion
    #region '   - DetachControlEvent ( Control ) : void 
    /// <summary>
            /// 解除注册控件事件</summary>
    ///
    private void DetachControlEvent( Control aTargetControl )
    {
    aTargetControl.MouseDown -= new MouseEventHandler( this.AttachedControl_MouseDown );
    aTargetControl.MouseMove -= new MouseEventHandler( this.AttachedControl_MouseMove );
    aTargetControl.MouseUp -= new MouseEventHandler( this.AttachedControl_MouseUp );
    aTargetControl.Paint -= new PaintEventHandler( this.AttachedControl_Paint );
    }
    #endregion
    #region '   - GetLocationKind ( Point ) : LocationKind 
    /// <summary>
    /// 通过鼠标指针位置取得当前位置种类</summary>
    ///
    private LocationKind GetLocationKind( Point aCursorLocation )
    {
    LocationKind kind; // is outer
    if ( ( aCursorLocation.X <= 0 &&
    Control.Width < aCursorLocation.X ) ||
    ( aCursorLocation.Y <= 0 &&
    Control.Height < aCursorLocation.Y ) )
    {
    kind = LocationKind.Outer;
    }
    // is UpperLeft
    else if ( aCursorLocation.X <= ResizingBorderWidth &&
    aCursorLocation.Y <= ResizingBorderWidth )
    {
    kind = LocationKind.UpperLeft;
    }
    // is UpperRigth
    else if ( Control.Width - ResizingBorderWidth <= aCursorLocation.X &&
    aCursorLocation.Y <= ResizingBorderWidth )
    {
    kind = LocationKind.UpperRight;
    }
    // is LowerLeft
    else if ( aCursorLocation.X <= ResizingBorderWidth &&
    Control.Height - ResizingBorderWidth <= aCursorLocation.Y )
    {
    kind = LocationKind.LowerLeft;
    }
    // is LowerRight
    else if ( Control.Width - ResizingBorderWidth <= aCursorLocation.X &&
    Control.Height - ResizingBorderWidth <= aCursorLocation.Y )
    {
    kind = LocationKind.LowerRight;
    }
    // is Upper
    else if ( aCursorLocation.Y <= ResizingBorderWidth )
    {
    kind = LocationKind.Upper;
    }
    // is Lower
    else if ( Control.Height - ResizingBorderWidth <=
    aCursorLocation.Y )
    {
    kind = LocationKind.Lower;
    }
    // is Left
    else if ( aCursorLocation.X <= ResizingBorderWidth )
    {
    kind = LocationKind.Left;
    }
    // is Right
    else if ( Control.Width - ResizingBorderWidth <=
    aCursorLocation.X )
    {
    kind = LocationKind.Right;
    }
    // is Inner
    else
    {
    kind = LocationKind.Inner;
    } // debug 
    Debug.WriteLine( "GetLocaitonKind return: " + kind.ToString() ); return kind;
    }
    #endregion
    #region '   - IsUnderMinimumSize ( Size ) : bool 
    /// <summary>
    /// 取得表示控件大小是否在设定的最小值以内的值</summary>
    ///
    private bool IsUnderMinimumSize( Size aControlSize )
    {
    if ( aControlSize.Width <= MinimumSize.Width ||
    aControlSize.Height <= MinimumSize.Height )
    {
    return true;
    } return false;
    }
    #endregion
    #region '   - SetupCursor ( LocationKind ) : void 
    /// <summary>
    /// 根据位置种类设定鼠标指针类型</summary>
    ///
    private void SetupCursor( LocationKind aLocationKind )
    {
    switch ( aLocationKind )
    {
    case LocationKind.Upper:
    case LocationKind.Lower: if ( Control.Cursor != Cursors.SizeNS )
    {
    Control.Cursor = Cursors.SizeNS;
    }
    break; case LocationKind.UpperRight:
    case LocationKind.LowerLeft: if ( Control.Cursor != Cursors.SizeNESW )
    {
    Control.Cursor = Cursors.SizeNESW;
    }
    break; case LocationKind.Right:
    case LocationKind.Left: if ( Control.Cursor != Cursors.SizeWE )
    {
    Control.Cursor = Cursors.SizeWE;
    }
    break; case LocationKind.LowerRight:
    case LocationKind.UpperLeft: if ( Control.Cursor != Cursors.SizeNWSE )
    {
    Control.Cursor = Cursors.SizeNWSE;
    }
    break; case LocationKind.Inner: if ( Control.Cursor != Cursors.SizeAll )
    {
    Control.Cursor = Cursors.SizeAll;
    }
    break; default: Control.Cursor = Cursors.Default;
    break;
    }
    }
    #endregion
      

  11.   

    #region '   - ResizeControl ( Point ) : void 
    /// <summary>
    /// 变更控件大小</summary>
    ///
    private void ResizeControl( Point aCursorLocation )
    {
    int diffWidth;
    int diffHeight;
    Size changedSize;
    Point changedLocation; switch ( DraggingKind )
    {
    // Upper
    case LocationKind.Upper: diffHeight = CursorBeforeLocation.Y + aCursorLocation.Y; changedSize = new Size( Control.Width,
                            Control.Height - diffHeight );
    changedLocation = new Point( Control.Location.X,
                                 Control.Location.Y + diffHeight ); ResizeControl( changedLocation, changedSize );
    break; // UpperRight
    case LocationKind.UpperRight: diffHeight = CursorBeforeLocation.Y + aCursorLocation.Y;
    diffWidth = CursorBeforeLocation.X - aCursorLocation.X; changedSize = new Size( ControlBeforeSize.Width - diffWidth,
                            Control.Height - diffHeight );
    changedLocation = new Point( Control.Location.X,
                                 Control.Location.Y + diffHeight ); ResizeControl( changedLocation, changedSize );
    break; // Rigth
    case LocationKind.Right: diffWidth = CursorBeforeLocation.X - aCursorLocation.X; changedSize = new Size( ControlBeforeSize.Width - diffWidth,
                            ControlBeforeSize.Height ); ResizeControl( changedSize );
    break; // LowerRight
    case LocationKind.LowerRight: diffWidth = CursorBeforeLocation.X - aCursorLocation.X;
    diffHeight = CursorBeforeLocation.Y - aCursorLocation.Y; changedSize = new Size( ControlBeforeSize.Width - diffWidth,
                            ControlBeforeSize.Height - diffHeight ); ResizeControl( changedSize );
    break; // lower
    case LocationKind.Lower: diffHeight = CursorBeforeLocation.Y - aCursorLocation.Y; changedSize = new Size( ControlBeforeSize.Width,
                            ControlBeforeSize.Height - diffHeight ); ResizeControl( changedSize );
    break; // LowerLeft
    case LocationKind.LowerLeft: diffHeight = CursorBeforeLocation.Y - aCursorLocation.Y;
    diffWidth = CursorBeforeLocation.X + aCursorLocation.X; changedSize = new Size( Control.Width - diffWidth, ControlBeforeSize.Height - diffHeight );
    changedLocation = new Point( Control.Location.X + diffWidth, Control.Location.Y ); ResizeControl( changedLocation, changedSize );
    break; // Left
    case LocationKind.Left: diffWidth = CursorBeforeLocation.X + aCursorLocation.X; changedSize = new Size( Control.Width - diffWidth,
                            Control.Height );
    changedLocation = new Point( Control.Location.X + diffWidth,
                                 Control.Location.Y ); ResizeControl( changedLocation, changedSize );
    break; // UpperLeft
    case LocationKind.UpperLeft: diffHeight = CursorBeforeLocation.Y + aCursorLocation.Y;
    diffWidth = CursorBeforeLocation.X + aCursorLocation.X; changedSize = new Size( Control.Width - diffWidth, Control.Height - diffHeight );
    changedLocation = new Point( Control.Location.X + diffWidth, Control.Location.Y + diffHeight ); ResizeControl( changedLocation, changedSize );
    break; default: break;
    } Control.Refresh();
                
    }
    #endregion
    #region '   - ResizeControl ( Size ) : void 
    /// <summary>
            /// 变更控件大小</summary>
    ///
    private void ResizeControl( Size aNewSize )
    {
                
    if ( IsUnderMinimumSize( aNewSize ) )
    {
    int width = MinimumSize.Width;
    int height = MinimumSize.Height; if ( MinimumSize.Width < aNewSize.Width )
    {
    width = aNewSize.Width+37;
    }
    if ( MinimumSize.Height < aNewSize.Height )
    {
    height = aNewSize.Height+39;
    } Control.Size = new Size( width, height );
    }
    else
    {
    Control.Size = aNewSize;
    }
    }
    #endregion
    #region '   - ResizeControl ( Point, Size ) : void 
    /// <summary>
            /// 变更控件大小</summary>
    ///
    private void ResizeControl( Point aNewLocation, Size aNewSize )
    {
    if ( IsUnderMinimumSize( aNewSize ) )
    {
    int width = aNewSize.Width;
    int height = aNewSize.Height;
    int x = aNewLocation.X;
    int y = aNewLocation.Y; if ( aNewSize.Width <= MinimumSize.Width )
    {
    width = MinimumSize.Width; if ( DraggingKind == LocationKind.UpperRight )
    {
    x = ControlBeforeLocation.X;
    }
    else
    {
    x = Control.Left + Control.Size.Width - MinimumSize.Width;
    }
    }
    if ( aNewSize.Height <= MinimumSize.Height )
    {
    if ( DraggingKind == LocationKind.LowerLeft )
    {
    y = ControlBeforeLocation.Y;
    }
    else
    {
    y = Control.Top + Control.Size.Height - MinimumSize.Height;
    }
    height = MinimumSize.Height;
    } Control.SetBounds( x,
                       y,
                       width,
                       height,
                       BoundsSpecified.All );
    }
    else
    {
    Control.SetBounds( aNewLocation.X,
                       aNewLocation.Y,
                       aNewSize.Width,
                       aNewSize.Height,
                       BoundsSpecified.All );
    }
    }
    #endregion
    #region '   - MoveControl ( Point ) : void 
    /// <summary>
    /// 移动控件</summary>
    ///
    private void MoveControl( Point aCursorLocation )
    {
    Control.Location =
    new Point( ControlBeforeLocation.X - ( CursorBeforeLocation.X - aCursorLocation.X ),
               ControlBeforeLocation.Y - ( CursorBeforeLocation.Y - aCursorLocation.Y ) );
    ControlBeforeLocation = Control.Location;
    Control.Refresh();
    }
    #endregion
            
      

  12.   

    楼上,有Demo么?能不能发个给我?[email protected]