来源: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 = ;
int ctrlLastHeight = ;
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 == )
ctrlLastLeft = ctrlLeft;
if (ctrlLastTop == )
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 < )
sizeageX = ;
if (sizeageY < )
sizeageY = ;
ctrlWidth = sizeageX;
ctrlHeight = sizeageY;
if (ctrlLastWidth == )
ctrlLastWidth = ctrlWidth;
if (ctrlLastHeight == )
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 - || e.Y < this.ctrl.Height - )
{
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# 运行时通过鼠标拖动改变控件的大小的更多相关文章

  1. ios 运行时特征,动态改变控件字体大小

    需求:ex: 在不同尺寸的iPhone上面显示的字体大小不一样 https://github.com/rentzsch/jrswizzle #import <UIKit/UIKit.h> ...

  2. 运行时改变控件的大小(点击后立刻ReleaseCapture,然后计算位移,最后发消息改变位置)——最有趣的是TPanel其实也有窗口标题,因此可发HTCAPTION消息

    //光标在控件不同位置时的样式 // 由于拐角这点手动精确实在困难 所以用范围 范围+3 这样很容易就找到这一点了 procedure CtrlMouseMove(Ctrl: TWinControl; ...

  3. WPF 使用鼠标拖动一个控件的实现[2018.7.15]

    原文:WPF 使用鼠标拖动一个控件的实现[2018.7.15] Q:已经把一个Shape和一个TextBlock组合起来放到了一个Grid中,现在想要实现用鼠标拖动这个Grid到任意位置的功能,如何做 ...

  4. [转]C#鼠标拖动任意控件

    C#鼠标拖动任意控件(winform) 分类: c#2011-08-15 22:51 178人阅读 评论(0) 收藏 举报 winformc#userwindowsobjectapi using Sy ...

  5. MFC中改变控件的大小和位置

    用CWnd类的函数MoveWindow()或SetWindowPos()可以改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  6. MFC中改变控件的大小和位置(zz)

    用CWnd类的函数MoveWindow()或SetWindowPos()能够改变控件的大小和位置. void MoveWindow(int x,int y,int nWidth,int nHeight ...

  7. Android中动态改变控件的大小的一种方法

    在Android中有时候我们需要动态改变控件的大小.有几种办法可以实现  一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸.这个是可以进行位置修改的,onMeasure不行. ...

  8. C# 窗体缩放的时候同步改变控件的大小和字体

    最新在写个小程序,需要窗体填满各种尺寸的显示器,同时需要同步缩放控件的大小.于是就写了个类,简单的调用一下即可解决问题. 这个类可以同步缩放控件的位置,宽度高度,字体大小. 使用的时候在FormLoa ...

  9. MFC 改变控件的大小和位置

    mfc 改变控件大小和位置用到的函数: ) void MoveWindow(int x, int y, int nWidth, int nHeight); ) void MoveWindow(LPCR ...

随机推荐

  1. Tessnet2图片识别(2)

    1. 引用 tessnet2.dll (只有NET2.0版本) 2. 视图页 <%@ Page Language="C#" MasterPageFile="~/Vi ...

  2. IOS开发-OC学习-Foundation框架练习

    OC语言Foundation框架中字典.字符串.数组的应用: NSString *string = @"China|Usa|France"; NSArray *array = [s ...

  3. 理解Action,Service和Dao功能(转)

    真正理解.区分Action,Service和Dao功能   在不分层的系统里,我们可以将所有的代码都写到一个地方,比如struts的Action类.在这里,我们不仅要处理页面逻辑,还要做业务逻辑,还要 ...

  4. UVa 10720 - Graph Construction

    题目大意:给n个整数, 分别代表图中n个顶点的度,判断是否能构成一张图. 看到这个题后,除了所有数之和应该为偶数之外,没有别的想法了,只好在网上搜解题报告了.然后了解了Havel-Hakimi定理.之 ...

  5. UWP项目的包无法通过本地校验程序

    在UWP工程中,我们打出的包Appx需要通过本地的校验程序校验通过后才可以进行商店的提交.在校验程序汇报的不通过原因当中,除了显而易见的因为美术资源不规范.代码调用不合法API等原因之外,还有一些奇怪 ...

  6. Spring 之 配置(Java之负基础实战)

    1.程序加入Spring <?xml version="1.0" encoding="utf-8"?> <web-app xmlns=&quo ...

  7. eQTL

    首先QTL是数量性状位点,比如身高是一个数量性状,其对应的控制基因的位点就是一个数量性状位点,而eQTL就是控制数量性状表达位点,即能控制数量性状基因(如身高基因)表达水平高低的那些基因的位点. 数量 ...

  8. Effective java -- 2 对于所有对象都通用到方法

    第八条:覆盖equals时请遵守通用约定 什么时候需要覆盖equals方法?类具有自己的逻辑相等概念,并且父类的equals方法不能满足需要.重写equals时需要遵循一下约定: 自反性:非null ...

  9. PHP做负载均衡回话保持问题参考

    最近一个项目的服务器老是出现Session数据丢失问题,导致用户莫名其妙的退出,原因是太相信我们的运维人员所谓的负载均衡会话保持的概念.会话保持 的原理就是负载均衡通过Cookie来分发那个客户连接被 ...

  10. DataTimePicker

    日期时间控件 DataTimePicker 功能:拾取系统时间.日期,并以对应格式输出 重要属性: a. date,拾取的时间.  b. Time,拾取的系统时间 举例如:button2.Captio ...