来源: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. oracle系列--级联删除和级联更新

    必须声明:此博客转载于Oracle外键级联删除和级联更新http://www.2cto.com/database/201507/417496.html 鉴于此前收藏的精彩博客无料被删除了,很是痛心,所 ...

  2. Angular - - $http请求服务

    $http $http是Angular的一个核心服务,它有利于浏览器通过XMLHttpRequest 对象或者 JSONP和远程HTTP服务器交互. $HTTP API 是基于 $q服务暴露的defe ...

  3. C++第一天学习

    代码1 #include<iostream> int main(){ int a; std::cout << "hello c++" << st ...

  4. 连接linux 服务器

    File > Quick Connect ,Hostname 是ip , Username是用户名

  5. java 数据设置和显示

    1. 首先设置ModelAndView 的值 @Override public ModelAndView handleRequest(HttpServletRequest arg0, HttpServ ...

  6. mysql5.5慢日志设置和查询

    mysql> showvariables like '%version%'; +-------------------------+---------------------+ | Variab ...

  7. ORACLE Postgresql中文排序

    当我们order排序不能够实现我们想要的内容时候,尝试一下NLSSORT这个函数吧 他不仅仅按照姓氏排序,名也会排序: nls_param用于指定语言特征,格式为nls_sort      = sor ...

  8. sql server停止和重启命令

    http://www.ynpxrz.com/n822732c2024.aspx 我们知道:sql server重启分分两步走 1.停止 net stop mssqlserver 2.重启 net st ...

  9. jQuery插入节点(移动节点)

    jQuery插入节点(移动节点) <%@ page language="java" import="java.util.*" pageEncoding=& ...

  10. SQL Server 手把手教你使用profile进行性能监控

    200 ? "200px" : this.width)!important;} --> 介绍 经常会有人问profile工具该怎么使用?有没有方法获取性能差的sql的问题.自 ...