在執行時使用滑鼠移動控制元件和改變控制元件的大小 (轉)

worldblog發表於2007-12-13
在執行時使用滑鼠移動控制元件和改變控制元件的大小 (轉)[@more@]

在執行時使用滑鼠移動和改變控制元件的大小:namespace prefix = o ns = "urn:schemas--com::office" />

 

我寫了一個類,使用它可以透過滑鼠自由移動所有放到窗體上的控制元件,也可以自由的改變其大小。這個類只可以處理窗體上的控制元件,如果你有興趣,可以補充一點程式碼,使得這個類可以處理更復雜的情形,比如,在Panel控制元件上的控制元件。

類程式碼

public class Resize

  {

  bool IsMoving=false;

  int ctrlLastWidth=0;

  int ctrlLastHeight=0;

  int ctrlWidth;

  int ctrlHeight;

  int ctrlLeft;

  int ctrlTop;

  int cursorL;

  int cursorT;

  int ctrlLastLeft;

  int ctrlLastTop;

  int Htap;

  int Wtap;

  bool ctrlIsResizing=false;

    System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();

  private Control ctrl;

  private Fofrm;

  public Resize(Control c,Form frm)

  {

    ctrl=c;

    this.frm=frm;

    this.Htap=this.frm.Height-this.frm.ClientRectangle.Height;

    this.Wtap=this.frm.Width-this.frm.ClientRectangle.Width;

    ctrl.MouseDown+=new MouseEventHandler(MouseDown);

    ctrl.MouseMove+=new MouseEventHandler(MouseMove);

    ctrl.MouseUp+=new MouseEventHandler(MouseUp);

  }

  private void MouseMove( sender,MouseEventArgs e)

  {

  if (frm==null)

    return;

  if (e.Button==MouseButtons.Left)

  {

    if(this.IsMoving)

    {

    if (ctrlLastLeft == 0)

      ctrlLastLeft = ctrlLeft;

    if (ctrlLastTop==0)

      ctrlLastTop = ctrlTop;

    int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Location.X);

    int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Location.Y);

      if(locationX

      locationX=this.frm.DesktopLocation.X+this.Wtap;

      if(locationY

      locationY=this.frm.DesktopLocation.Y+this.Htap;

      this.ctrlLeft=locationX;

      this.ctrlTop=locationY;

      ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);

      ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

      ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System..Forms.FrameStyle.Dashed);

      ctrlLastLeft = ctrlLeft;

      ctrlLastTop = ctrlTop;

      ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);

      ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

      ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

    return;

    }

    int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.Wtap-this.ctrl.Location.X);

    int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.Htap-this.ctrl.Location.Y);

    if (sizeageX < 2)

      sizeageX = 1;

    if (sizeageY < 2)

      sizeageY = 1;

    ctrlWidth = sizeageX;

    ctrlHeight = sizeageY;

    if (ctrlLastWidth == 0)

      ctrlLastWidth = ctrlWidth;

    if (ctrlLastHeight==0)

      ctrlLastHeight = ctrlHeight;

    if (ctrlIsResizing)

    {

      ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left+this.Wtap,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);

      ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);

    }

    ctrlIsResizing = true;

    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

    ctrlLastWidth = ctrlWidth;

    ctrlLastHeight = ctrlHeight;

    ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);

    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

  }

  }

  private  void MouseDown(object sender,MouseEventArgs e)

  {

  if (frm==null)

    return;

  if (e.X

  {

    this.IsMoving=true;

    this.ctrlLeft=this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left;

    this.ctrlTop=this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top;

    this.cursorL=Cursor.Position.X;

    this.cursorT=Cursor.Position.Y;

    this.ctrlWidth=this.ctrl.Width;

    this.ctrlHeight=this.ctrl.Height;

  }

    ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);

    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

  }

  private void MouseUp(object sender,MouseEventArgs e)

  {

  if (frm==null)

    return;

    ctrlIsResizing = false;

  if (this.IsMoving)

  {

    ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);

    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

    this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X-this.Wtap;

    this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y-this.Htap;

    this.IsMoving=false;

    this.ctrl.Refresh();

    return;

  }

    ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);

    ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

    ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

    this.ctrl.Width = ctrlWidth;

    this.ctrl.Height = ctrlHeight;

    this.ctrl.Refresh();

  }

  }

 

}

如何使用這個類

假設窗體上有兩個控制元件listBox1和toolBar1,你可以在適當的時機(一般在窗體的Load事件中)加入下面的語句就可以了。

Resize r1=new  Resize(this.toolBar1,this);

Resize r4= new Resize(this.listBox1,this);

其他

你也可以新增一些方法,使得在需要的時候可以停止滑鼠移動控制元件的功能,比如:

public void Stop()

{

ctrl.MouseDown- =new MouseEventHandler(MouseDown);

ctrl.MouseMove- =new MouseEventHandler(MouseMove);

ctrl.MouseUp- =new MouseEventHandler(MouseUp);

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-992955/,如需轉載,請註明出處,否則將追究法律責任。

相關文章