Use the mouse to move control and change the size of the control at runtime

zhaozj2021-02-16  45

Use the mouse to move control and change the size of the control at runtime

I wrote a class that uses it to freely move all the controls on the form through the mouse, and can also change their size. This class can only handle controls on the form. If you are interested, you can add a little code so that this class can handle more complex situations, such as controls on the Panel control.

Class code

Public Class Resize

{

Bool ismoving = false;

INT ctrllastwidth = 0;

INT ctrlaStheight = 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 CTRL;

Private form frm;

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 (Object Sender, MouseEventArgs E)

{

IF (frm == null)

Return;

IF (E.Button == MouseButtons.Left)

{

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 = location = new system.drawing.point (this.ctrllastleft, this.ctrllasttop);

CtrlRectangle.size = new system.drawing.size (Ctrlwidth, CtrlHeight);

Controlpaint.drawreversibleframe (CtrlRectangle, Color.empty, System.Windows.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;

CtrlisResizing

{

CtrlRectangle.Location = new system.drawing.point (this.frm.desktoplocation.x this.ctrl.deft 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 (Ctrolwidth, CtrlHeight); ControlPaint.drawreversibleFrame (CtrlRectangle, Color.empty, System.Windows.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.PSITION.X;

THIS.CURSORT = CURSOR.PSITION.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;

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 (Ctrolwidth, CtrlHeight); ControlPaint.drawreversibleFrame (CtrlRectangle, Color.empty, System.Windows.FRAMESTYLE.DASHED);

THIS.CTRL.WIDTH = Ctrlwidth;

THIS.CTRL.HEIGHT = Ctrlheight;

THIS.CTRL.REFRESH ();

}

}

}

How to use this class

Suppose there are two controls Listbox1 and Toolbar1 on the form, you can add the following statements in the appropriate timing (generally in the form of the form).

Resize r1 = new resize (THIS.TOOLBAR1, THIS);

Resize R4 = New Resize (this.listbox1, this);

other

You can also add some methods to stop the functionality of mouse mobile controls when needed, such as:

Public void stop ()

{

Ctrl.mousedown- = new mouseeventhandler (mousedown);

Ctrl.mousemove- = new mouseeventhandler (mousemove);

Ctrl.mouseup- = new mouseeventhandler (mouseup);

}

转载请注明原文地址:https://www.9cbs.com/read-26521.html

New Post(0)