来源: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. java系列--过滤器

    在web.xml配置过滤器:过滤器一定要放在所以Servlet前面 过滤器的生命周期: 过滤器的应用: 1.编码格式 2.权限验证 3.数据库关闭

  2. 优酷、YouTube、Twitter及JustinTV几个视频网站的架构

      优酷视频网站架构 一.网站基本数据概览据2010年统计,优酷网日均独立访问人数(uv)达到了8900万,日均访问量(pv)更是达到了17亿,优酷凭借这一数据成为google榜单中国内视频网站排名最 ...

  3. Swoole:重新定义PHP

    Swoole PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异 ...

  4. 前端程序员应该知道的 15 个 jQuery 小技巧

    下面这些简单的小技巧能够帮助你玩转jQuery. 返回顶部按钮 预加载图像 检查图像是否加载 自动修复破坏的图像 悬停切换类 禁用输入字段 停止加载链接 切换淡入/幻灯片 简单的手风琴 让两个div高 ...

  5. thinkphp CURD 1

    二.ThinkPHP 3 读取数据    (重点)    对数据的读取 Read    $m=new Model('User');    $m=M('User'); select    $m-> ...

  6. IIS URLReWrite URL 重写模块 下载地址

    https://www.microsoft.com/zh-cn/download/details.aspx?id=7435

  7. JSP EL表达式使用

    JSP EL表达式使用: Servlet: package com.stono.servlet; import java.io.IOException; import java.util.HashMa ...

  8. 马丁 福勒 Martin Fowler 关于依赖注入和反转控制的区别

    马丁 福勒 Martin Fowler 关于依赖注入和反转控制的区别 http://martinfowler.com/articles/injection.html 中文翻译:http://files ...

  9. Android离线缓存

    android做到一定程度,需要考虑缓存的问题,不信可以掏出手机看看淘宝等一些app是否无网的情况下还可以浏览,不过大部分app并没有考虑到这些问题,解决Android的缓存有哪些方法呢 1.IO流读 ...

  10. C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!

    说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...