来源:http://blog.csdn.net/yanleigis/article/details/1819447

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

转载于:https://www.cnblogs.com/foreverstars/p/3205527.html

C# 运行时通过鼠标拖动改变控件的大小相关推荐

  1. MFC 改变控件字体大小

    LOGFONT: 一个结构体,用来逻辑上表达一个字体   ,在Windows内部,字体是以一个名为LOGFONT的结构来表示 typedef struct tagLOGFONT { LONG lfHe ...

  2. 随着窗体大小变化改变控件的大小和字体

    public  struct FormSizeChange     {       public  Size currentSize;//当前窗体大小       public  int count; ...

  3. Winform窗体控件自适应大小

    自己写的winform窗体自适应大小代码,代码比较独立,很适合贴来贴去不会对原有程序造成影响,可以直接继承此类或者把代码复制到自己的代码里面直接使用 借鉴了网上的一些资料,最后采用重写WndProc方 ...

  4. php 页面拖动改变大小,鼠标拖动改变DIV等网页元素的大小的实现方法

    1.初次实现 1.1 html代码 div change width by drag div change width by drag 1.2 js代码 var eleLeft = $('#myDiv ...

  5. Jquery实现鼠标拖动改变div高度

    前言 鼠标拖动该DIV实现自动改变高度扩展内容显示区域. 以下是一个设计原型,基于jQuery实现,只实现了拖动效果,没有做页面美化,可以根据需求做相应修改. 转自:http://www.cnblog ...

  6. 判断鼠标不在控件上_基础设施:一套基本控件

    类可以把需要重复使用的代码封装到一起多次使用, 但这样的代码仍不能被不同的程序使用. 把自己常用的代码编到一起,打包成库的话,就能制成一套自用工具库作为基础设施. 这里常用的部分,首先就是界面控件. ...

  7. 判断鼠标不在控件上_VB常用控件属性讲解单选按钮、复选框总结

    1 单选按钮.复选框做为VB编程中的选择性控件,在实际程序中有着广泛的应用,所以熟练掌握其特殊属性的用法就很重要了! 单选按钮.复选框的实际应用图 复选框(CheckBox)控件 属性 说明 Name ...

  8. Android 通过代码改变控件的布局方式

    在很多情况下当我们在xml中布局的方式并不能满足我们的要求,而这时我们就需要通过在代码中控制控件的布局 根据不同的条件来控制布局.首先来了解一下安卓中的一些单位 dip: device indepen ...

  9. C# 滑块/滑杆/拖动条控件trackBar

    WindForm中的滑块/滑杆/拖动条控件为trackBar,如下图所示.        需要改变拖动条对应的值.显示样式和设置默认值,按下图设置.

最新文章

  1. 清华校友总会AI大数据专委会(筹)第一次理事会顺利召开
  2. vim匹配特定的行并删除
  3. boost::foreach模块一些杂项的测试程序
  4. 云时代,我们需要怎样的数据库?
  5. PHP 入门 - 10.应用技术
  6. 2019汇总之从4个关键词看单细胞与肝癌文献
  7. loadrunner接口性能测试分享
  8. Windows Phone 用WebBrowser加载HTML页面
  9. 四.激光SLAM框架学习之A-LOAM框架---项目工程代码介绍---2.scanRegistration.cpp--前端雷达处理和特征提取
  10. 源码:Hadoop-3.0.3 启动过程中执行start-all.sh开始
  11. composer 更新php位置,使用composer安装/更新PHP包
  12. ENVI入门系列教程---一、数据预处理---4.1 遥感图像正射校正
  13. 纳米晶磁芯:非晶带材的“秘密”!
  14. 为什么HierachyViewer无法连接真机调试
  15. 使用VMware虚拟机搭建Panabit透明网桥环境
  16. android中的m、mm、mmm、make指令
  17. 三层交换机实现vlan间通信
  18. latex公式常见问题:居中与左对齐,多行公式左对齐,上下标
  19. Java语言每日一练—第9天:根据输入的数据判断是星期几
  20. sql server嵌套查询

热门文章

  1. excel合并计算_【Excel】合并计算和模拟分析的应用
  2. UE4学习-场景介绍、基本操作、快捷键
  3. linux更改甜器名称,Linux添加swap分区
  4. python医学图像读取_对python读取CT医学图像的实例详解
  5. 从底部上滑失灵_宝鸡终于也有超火的高空玻璃水滑啦!就在玉池公社!8月10日见~...
  6. python生成api文档_Django 自动生成api接口文档教程
  7. sqlserver date类型和字符串比较_VB编程基本数据类型完整剖析
  8. flutter打包的app有多大_Flutter原生混合开发
  9. springmvcdate类型接收不到_无线电小课堂:如何在市区环境有效接收短波信号,选什么天线?...
  10. 卸载pytorch_Pytorch中的hook的使用详解