.net,C#如何在运行时通过鼠标拖动改变控件的大小

转载 2007年10月11日 08:42:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsApplication2
...{
    
class ResizeAction
    
...{
        
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 Form frm;
        
public ResizeAction(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);
        }

        
public void MouseMove(object 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 < this.frm.DesktopLocation.X + this.Wtap)
                        locationX 
= this.frm.DesktopLocation.X + this.Wtap;
                    
if (locationY < this.frm.DesktopLocation.Y + this.Htap)
                        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.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;
                
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);
            }

        }

        
public void MouseDown(object sender, MouseEventArgs e)
        
...{
            
if (frm == null)
                
return;
            
if (e.X < this.ctrl.Width - 10 || e.Y < this.ctrl.Height - 10)
            
...{
                
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);
        }

        
public 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();
        }


    }

}


调用:
 
private void Form1_Load(object sender, EventArgs e)
        
...{
            
//WindowsApplication2.ResizeAction rs = new WindowsApplication2.ResizeAction(this.label1,this);
            WindowsApplication2.ResizeAction rs = new WindowsApplication2.ResizeAction(this.button1, this);   
        }

参考:http:
//www.cnblogs.com/DS-CzY/archive/2007/06/30/801377.aspx
 

C#鼠标拖动任意控件(winform)

  • 2011年12月18日 14:37
  • 267KB
  • 下载

在运行时通过鼠标拖动移动控件位置(c#)

前些日子因为工作需要想了解有关于在C#下实现运行时鼠标移动控件的方法,Google了一下还真找到了一个帖子,粗略看去代码还真不少,感觉有点复杂的样子,因为当时手头上还有点别的事情,没来得及细看,就把帖...
  • dekko
  • dekko
  • 2007-09-24 08:43:00
  • 18762

WinForm 实现鼠标拖动控件跟随效果(图文)

1. 运行初始窗口如下:   2. 拖动后效果如下:   3. 代码如下: public partial class Form1 : Form { /* ...
  • MYsce
  • MYsce
  • 2017-05-24 00:12:32
  • 950

c#可以创建任意控件的拖动

经常进行图形界面设计的编程者肯定知道,控件在设计的时候才能随意拖动,运行的时候就不能拖动了. 你肯定会问:运行时能随意拖动控件吗?答案是肯定的,我们的项目涉及到了这一点,我已经实现了,实际上很简单的...
  • vs_the_old_boy
  • vs_the_old_boy
  • 2016-08-09 12:23:47
  • 4349

c#实现控件拖动效果

public partial class Form1 : Form { bool bDraging = false; int x = 0; int y ...
  • robinfoxnan
  • robinfoxnan
  • 2008-05-29 18:19:00
  • 1058

C#当窗体大小改变时,窗体中的控件大小也随之改变

转自:http://www.cnblogs.com/slyzly/articles/1965965.html 第一种:推荐 在窗体中加上如下代码即可实现,但窗体点击放大按钮时却不能改变...
  • zhichao2001
  • zhichao2001
  • 2016-01-21 09:39:35
  • 3308

利用C#更专业的实现运行时调整控件大小和位置

从Delphi、VB到现在的Visual Studio .Net可视化的控件式编程越来越简化我们的开发过程,对于控件的可视化操作必不可少。在Visual Studio .Net的IDE中开发桌面应用程...
  • MYsce
  • MYsce
  • 2017-05-24 00:25:27
  • 2052

如何用C#实现控件的自由拖动

private Point mouse_offset;private void label1_MouseDown(object  sender, MouseEventArgs e){mouse_off...
  • shuerself
  • shuerself
  • 2007-11-23 13:17:00
  • 578

.net c#拖动控件,移动控件位置

 private Point mouse_offset;        private void label1_MouseDown(object sender, MouseEventArgs e)  ...
  • yanleigis
  • yanleigis
  • 2007-10-11 08:22:00
  • 4523

C#实现用户界面控件的拖动

  • 2016年08月26日 14:09
  • 50KB
  • 下载
收藏助手
不良信息举报
您举报文章:.net,C#如何在运行时通过鼠标拖动改变控件的大小
举报原因:
原因补充:

(最多只允许输入30个字)