因为我是要在自定义的textbox控件里实现,所以得在控件的内部实现

解决方案 »

  1.   

    响应TextBox的MouseMove事件,根据鼠标位置和TextBox的尺寸判断是否到达控件边界,如果到达,则设置TextBox.Cursor = Cursors.PanNE;
    Cursor是一个枚举,里面有很多的光标形状。
      

  2.   

    响应TextBox的MouseMove事件,根据鼠标位置和TextBox的尺寸判断是否到达控件边界,如果到达,则设置TextBox.Cursor = Cursors.PanNE;
    Cursor是一个枚举,里面有很多的光标形状。
      

  3.   

    响应MouseMove事件是对的,但我在MouseMove的处理代码中:
    if(e.right==textbox.right&&e.bottom==textbox.bottom),这样根本不行,
    if(e.right==textbox.locotion.right&&...............),这样也不行啊。
    根本无发进到if后的代码中。可能我写的代码有笔误,但是无论如何都没法实现啊,请大家试试看。
      

  4.   

    在TextBox的事件里处理,有试出来的人吗???????????
      

  5.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;namespace TrackCom
    {
    public class TrackCom : System.Windows.Forms.Label
    {
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    private const int Rectsize = 6;
    private Point ComStartPoint;
    private bool ComDragFlag;
    private Control BindControl;
    private Rectangle[] rect;
    private int SelDragDirect;
    private Label FrontLabel; public TrackCom(Control cont)
    {
    // This call is required by the Windows.Forms Form Designer.

    this.Parent = cont.Parent;
    this.Top = cont.Top - Rectsize ;
    this.Left = cont.Left - Rectsize ;
    this.Width = cont.Width + 2 * Rectsize ;
    this.Height = cont.Height + 2 * Rectsize ;
    this.BringToFront();
    this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TrackCom_KeyPress);
    this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseUp);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.TrackCom_Paint);
    this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TrackCom_KeyDown);
    this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseMove);
    this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseDown);
    this.Move += new System.EventHandler(this.TrackCom_Move);
    this.Resize += new System.EventHandler(this.TrackCom_Resize); FrontLabel = new Label();
    FrontLabel.Parent = cont;
    FrontLabel.BackColor = System.Drawing.Color.Transparent;
    FrontLabel.Size = cont.Size;
    FrontLabel.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TrackCom_KeyPress);
    FrontLabel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseUp);
    FrontLabel.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TrackCom_KeyDown);
    FrontLabel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseMove);
    FrontLabel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TrackCom_MouseDown); BindControl = cont;
    BindControl.Parent = this;
    BindControl.Top = Rectsize ;
    BindControl.Left = Rectsize ;
    SetRect();
    } /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing)
    {
    BindControl.Parent = this.Parent;
    BindControl.Top = this.Top + Rectsize ;
    BindControl.Left = this.Left + Rectsize ;
    FrontLabel.Dispose();
    if( disposing )
    {
    if(components != null)
    { components.Dispose();
    }
    }
    base.Dispose( disposing );
    } private void SetRect()
    {
    rect = new Rectangle[8];
    rect[0] = new Rectangle(0,0,5,5);
    rect[1] = new Rectangle((Width - Rectsize)/2,0,5,5);
    rect[2] = new Rectangle(Width - Rectsize,0,5,5);
    rect[3] = new Rectangle(0,(Height - Rectsize)/2,5,5);
    rect[4] = new Rectangle(Width - Rectsize,(Height - Rectsize)/2,5,5);
    rect[5] = new Rectangle(0,Height - Rectsize,5,5);
    rect[6] = new Rectangle((Width - Rectsize)/2,Height - Rectsize,5,5);
    rect[7] = new Rectangle(Width - Rectsize,Height - Rectsize,5,5);
    } private void SetBindSize()
    {
    if(BindControl!=null)
    {
    BindControl.Width = this.Width - 2 * Rectsize ;
    BindControl.Height = this.Height - 2 * Rectsize ;
    FrontLabel.Size = BindControl.Size;
    }
    } private void TrackCom_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(ComDragFlag)
    {
    this.Left = ComStartPoint.X;
    this.Top =  ComStartPoint.Y;
    ComDragFlag = false;
    }
    } private void TrackCom_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {

    } private void ChangeWidth(int X)
    {
    int w = this.Width - ComStartPoint.X + X;
    if(w>2*Rectsize && w<int.MaxValue)
    {
    this.Width = w;
    ComStartPoint.X = X;
    }
    } private void ChangeHeight(int Y)
    {
    int h = this.Height - ComStartPoint.Y + Y;
    if(h>2*Rectsize && h<int.MaxValue)
    {
    this.Height = h;
    ComStartPoint.Y = Y;
    }
    } private void ChangeTop(int Y)
    {
    int h,b;
    b = this.Bottom;
    if((h = this.Top - ComStartPoint.Y + Y) < (b - 2*Rectsize))
    {
    this.Top = h;
    this.Height = b - this.Top;
    }
    } private void ChangeLeft(int X)
    {
    int w,r;
    r = this.Right;
    if((w = this.Left - ComStartPoint.X + X) < (r - 2*Rectsize))
    {
    this.Left = w;
    this.Width = r - this.Left;
    }
    }
    private void TrackCom_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    ComStartPoint.X = e.X;
    ComStartPoint.Y = e.Y;
    ComDragFlag = true;
    }
    private void TrackCom_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(ComDragFlag)
    {
    switch(SelDragDirect)
    {
    case -1://整体拖动
    this.Left = this.Left - ComStartPoint.X + e.X;
    this.Top = this.Top - ComStartPoint.Y + e.Y;
    break;
    case 0://左上角拖动
    ChangeLeft(e.X);
    ChangeTop(e.Y);
    break;
    case 1://顶中部拖动
    ChangeTop(e.Y);
    break;
    case 2://右上角拖动
    ChangeWidth(e.X);
    ChangeTop(e.Y);
    break;
    case 3://左中部拖动
    ChangeLeft(e.X);
    break;
    case 4://右中部拖动
    ChangeWidth(e.X);
    break;
    case 5://左下角拖动
    ChangeLeft(e.X);
    ChangeHeight(e.Y);
    break;
    case 6://底中部拖动
    ChangeHeight(e.Y);
    break;
    case 7://右下角拖动
    ChangeWidth(e.X);
    ChangeHeight(e.Y);
    break;
    }
    }
    else
    {
    SelDragDirect=-1; //              \\\\\\\\\\\\\\\
    if(rect[0].Contains(e.X,e.Y))
    {
    SelDragDirect=0;
    this.Cursor = Cursors.SizeNWSE;
    return;
    }

    //               |||||||||||||||
    if(rect[1].Contains(e.X,e.Y))
    {
    SelDragDirect=1;
    this.Cursor = Cursors.SizeNS;
    return;
    }
    //              ////////////////
    if(rect[2].Contains(e.X,e.Y))
    {
    SelDragDirect=2;
    this.Cursor = Cursors.SizeNESW;
    return;
    } //             ------------------
    if(rect[3].Contains(e.X,e.Y))
    {
    SelDragDirect=3;
    this.Cursor = Cursors.SizeWE;
    return;
    }
    if(rect[4].Contains(e.X,e.Y))
    {
    SelDragDirect=4;
    this.Cursor = Cursors.SizeWE;
    return;
    }
    if(rect[5].Contains(e.X,e.Y))
    {
    SelDragDirect=5;
    this.Cursor = Cursors.SizeNESW;
    return;
    }
    if(rect[6].Contains(e.X,e.Y))
    {
    SelDragDirect=6;
    this.Cursor = Cursors.SizeNS;
    return;
    }
    if(rect[7].Contains(e.X,e.Y))
    {
    SelDragDirect=7;
    this.Cursor = Cursors.SizeNWSE;
    return;
    }
    this.Cursor = Cursors.SizeAll;
    }
    } private void TrackCom_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
    if(ComDragFlag)
    {
    ComDragFlag = false;
    }
    } private void TrackCom_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    Graphics g = e.Graphics;

    Pen pen = new Pen(Color.Gold,1);
    Brush brush = new SolidBrush(Color.Blue); for(int i=0;i<8;i++)
    {
    g.FillRectangle(brush,rect[i]);
    g.DrawRectangle(pen,rect[i]);
    }
    }

    private void TrackCom_Move(object sender, System.EventArgs e)
    {
    SetBindSize();
    } private void TrackCom_Resize(object sender, System.EventArgs e)
    {
    SetRect();
    SetBindSize();
    }
    }
    }
      

  6.   

    上面是我一个朋友写的一个移动孔件的类。
    这个类继承自LABEL,在构建的时候传入一个控件
    public TrackCom(Control cont)
    {}
    在LABLE的八个方向定义八个小矩形,
    然后再构建一个透明的LABEL把组件当住。
    当鼠标移到小矩形或透明LABLE上时可以进行拖动大小和位置。
    它使用于任何控件。如果你只想简单的拖放TEXT,那你不妨写一个继承自TEXTBOX的类,然后用
    上面的同样方法来实现拖动。