在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小。怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴!

using System;
using System.Windows.Forms;
using System.Drawing;
namespace ControlSizeChangeEx
{
/// <summary>
/// This class implements sizing and moving functions for
/// runtime editing of graphic controls
/// </summary>
public class PickBox
{
//////////////////////////////////////////////////////////////////
// PRIVATE CONSTANTS AND VARIABLES
//////////////////////////////////////////////////////////////////
private const int BOX_SIZE = ;
private Color BOX_COLOR = Color.White;
private ContainerControl m_container;
private Control m_control;
private Label[] lbl = new Label[];
private int startl;
private int startt;
private int startw;
private int starth;
private int startx;
private int starty;
private bool dragging;
private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,
Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,
Cursors.SizeNESW, Cursors.SizeWE};
private Cursor oldCursor;
private const int MIN_SIZE = ;
//
// Constructor creates 8 sizing handles & wires mouse events
// to each that implement sizing functions
//
public PickBox()
{
for (int i = ; i < ; i++)
{
lbl[i] = new Label();
lbl[i].TabIndex = i;
lbl[i].FlatStyle = ;
lbl[i].BorderStyle = BorderStyle.FixedSingle;
lbl[i].BackColor = BOX_COLOR;
lbl[i].Cursor = arrArrow[i];
lbl[i].Text = "";
lbl[i].BringToFront();
lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown);
lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove);
lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp);
}
}
//////////////////////////////////////////////////////////////////
// PUBLIC METHODS
//////////////////////////////////////////////////////////////////
//
// Wires a Click event handler to the passed Control
// that attaches a pick box to the control when it is clicked
//
public void WireControl(Control ctl)
{
ctl.Click += new EventHandler(this.SelectControl);
}
/////////////////////////////////////////////////////////////////
// PRIVATE METHODS
/////////////////////////////////////////////////////////////////
//
// Attaches a pick box to the sender Control
//
private void SelectControl(object sender, EventArgs e)
{
if (m_control is Control)
{
m_control.Cursor = oldCursor;
//Remove event any pre-existing event handlers appended by this class
m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
m_control = null;
}
m_control = (Control)sender;
//Add event handlers for moving the selected control around
m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp);
//Add sizing handles to Control's <a href="http://lib.csdn.net/base/docker" class='replace_word' title="Docker知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Container</a> (Form or PictureBox)
for (int i = ; i < ; i++)
{
m_control.Parent.Controls.Add(lbl[i]);
lbl[i].BringToFront();
}
//Position sizing handles around Control
MoveHandles();
//Display sizing handles
ShowHandles();
oldCursor = m_control.Cursor;
m_control.Cursor = Cursors.SizeAll;
}
public void Remove()
{
HideHandles();
m_control.Cursor = oldCursor;
}
private void ShowHandles()
{
if (m_control != null)
{
for (int i = ; i < ; i++)
{
lbl[i].Visible = true;
}
}
}
private void HideHandles()
{
for (int i = ; i < ; i++)
{
lbl[i].Visible = false;
}
}
private void MoveHandles()
{
int sX = m_control.Left - BOX_SIZE;
int sY = m_control.Top - BOX_SIZE;
int sW = m_control.Width + BOX_SIZE;
int sH = m_control.Height + BOX_SIZE;
int hB = BOX_SIZE / ;
int[] arrPosX = new int[] {sX+hB, sX + sW / , sX + sW-hB, sX + sW-hB,
sX + sW-hB, sX + sW / , sX+hB, sX+hB};
int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / , sY + sH-hB,
sY + sH-hB, sY + sH-hB, sY + sH / };
for (int i = ; i < ; i++)
lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
}
/////////////////////////////////////////////////////////////////
// MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL
/////////////////////////////////////////////////////////////////
//
// Store control position and size when mouse button pushed over
// any sizing handle
//
private void lbl_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
startl = m_control.Left;
startt = m_control.Top;
startw = m_control.Width;
starth = m_control.Height;
HideHandles();
}
//
// Size the picked control in accordance with sizing handle being dragged
// 0 1 2
// 7 3
// 6 5 4
//
private void lbl_MouseMove(object sender, MouseEventArgs e)
{
int l = m_control.Left;
int w = m_control.Width;
int t = m_control.Top;
int h = m_control.Height;
if (dragging)
{
switch (((Label)sender).TabIndex)
{
case : // Dragging top-left sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
w = startl + startw - m_control.Left;
h = startt + starth - m_control.Top;
break;
case : // Dragging top-center sizing box
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
h = startt + starth - m_control.Top;
break;
case : // Dragging top-right sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
h = startt + starth - m_control.Top;
break;
case : // Dragging right-middle sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
break;
case : // Dragging right-bottom sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case : // Dragging center-bottom sizing box
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case : // Dragging left-bottom sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
w = startl + startw - m_control.Left;
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case : // Dragging left-middle sizing box
l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
w = startl + startw - m_control.Left;
break;
}
l = (l < ) ? : l;
t = (t < ) ? : t;
m_control.SetBounds(l, t, w, h);
}
}
//
// Display sizing handles around picked control once sizing has completed
//
private void lbl_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
MoveHandles();
ShowHandles();
}
/////////////////////////////////////////////////////////////////
// MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM
/////////////////////////////////////////////////////////////////
//
// Get mouse pointer starting position on mouse down and hide sizing handles
//
private void ctl_MouseDown(object sender, MouseEventArgs e)
{
dragging = true;
startx = e.X;
starty = e.Y;
HideHandles();
}
//
// Reposition the dragged control
//
private void ctl_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
int l = m_control.Left + e.X - startx;
int t = m_control.Top + e.Y - starty;
int w = m_control.Width;
int h = m_control.Height;
l = (l < ) ? : ((l + w > m_control.Parent.ClientRectangle.Width) ?
m_control.Parent.ClientRectangle.Width - w : l);
t = (t < ) ? : ((t + h > m_control.Parent.ClientRectangle.Height) ?
m_control.Parent.ClientRectangle.Height - h : t);
m_control.Left = l;
m_control.Top = t;
}
}
//
// Display sizing handles around picked control once dragging has completed
//
private void ctl_MouseUp(object sender, MouseEventArgs e)
{
dragging = false;
MoveHandles();
ShowHandles();
}
}
}

控件自定义拖动、改变大小

创建一个PickBox对象 ,  调用此对象的WireControl(你希望改变的控件);方法就行了。

转自:http://blog.csdn.net/ihaolau/article/details/6447991

【转】C# 控件的自定义拖动、改变大小方法的更多相关文章

  1. C# 控件的自定义拖动、改变大小方法

    在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...

  2. 【教程】【FLEX】#006 控件位置的拖动

    上一篇笔记学习了怎么从 把一个控件拖放到另外一个控件里面(即 A --> B里面). 而现在呢,则是学习  怎么把 A 拖到另外一个位置. (A -->A位置改变): 先说一下实现的思路( ...

  3. WPF 控件库——可拖动选项卡的TabControl

    WPF 控件库系列博文地址: WPF 控件库——仿制Chrome的ColorPicker WPF 控件库——仿制Windows10的进度条 WPF 控件库——轮播控件 WPF 控件库——带有惯性的Sc ...

  4. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  5. WPF Calendar 日历控件 样式自定义

    原文:WPF Calendar 日历控件 样式自定义 粗略的在代码上做了些注释 blend 生成出来的模版 有的时候 会生成 跟 vs ui界面不兼容的代码 会导致可视化设计界面 报错崩溃掉 但是确不 ...

  6. DEV 之 有些控件不允许拖动。

    DEV 之 有些控件不允许拖动.  设置一个参数即可解决问题

  7. DevExpress控件-GridControl根据条件改变单元格(Dev GridControl 单元格着色)

    DevExpress控件-GridControl根据条件改变单元格颜色,如下图: 解决办法:可以参考:http://www.cnblogs.com/zeroone/p/4311191.html 第一步 ...

  8. WPF 4 DataGrid 控件(自定义样式篇)

    原文:WPF 4 DataGrid 控件(自定义样式篇)      在<WPF 4 DataGrid 控件(基本功能篇)>中我们已经学习了DataGrid 的基本功能及使用方法.本篇将继续 ...

  9. iOS 监听控件某个属性的改变observeValueForKeyPath

    创建一个测试的UIButton #import "ViewController.h" @interface ViewController () @property(nonatomi ...

随机推荐

  1. 1.2输出100以内的素数&输出前100个素数。

    输出100以内的素数只是一个嵌套,在1.1的基础上添加一层循环,只需要注意从2开始,并且变量需要换一个. #include<stdio.h> int main() { ; ; i < ...

  2. 手机通过数据线连接电脑后,找不到设备--Android Studio

    手机通过数据线连接电脑后,找不到可用的设备.允许USB调试,,因为小米手机还要设置USB开发者模式. 1.打开设置/关于手机 四次点击“MIUI版本”如图: 2.返回上一页打开更多设置 看到开发者选项 ...

  3. 《web全栈工程师的自我修养》读书笔记

    有幸读了yuguo<web全栈工程师的自我修养>,颇有收获,故在此对读到的内容加以整理,方便指导,同时再回顾一遍书中的内容. 概览 整本书叙述的是作者的成长经历,通过经验的分享,给新人或者 ...

  4. C程序汇编运行模式简析

    SJTUBEAR 原创作品转载请注明出处 /<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 1. 汇编 ...

  5. .net一般处理程序(httphandler)实现文件下载功能

    Handler文件代码如下: public class MDMExporterWeb : IHttpHandler { public void ProcessRequest(HttpContext c ...

  6. 有时打开myeclipse,部署报错解决方案

    1.首先关闭MyEclipse工作空间. 2.然后删除工作空间下的 "/.metadata/.plugins/org.eclipse.core.runtime/.settings/com.g ...

  7. android分页请求,重复数据如何处理

    1.如图 如图上的ks031数据,在数据请求时,第一次请求20条数据,再次加载下一页20条数据时,后台的数据处理导致ks031排序到了第2页,出现加载重复现象, 这种情况则是怎么处理? 有谁明白,求指 ...

  8. Macbook 的 print screen 是什么

  9. Linux下GNOME桌面的安装

    yum grouplist //列出yum仓库里的软件组列表 GNOME桌面的安装 yum install soft1 soft2 //使用yum源安装软件 yum groupinstall grou ...

  10. java深入技术九 (注解)

    java注解 (Annotation) 一般起到说明,配置的作用,在java.lang.annotation 定义,本质上没有增强java的能力 1.常用注解:@Override,强制编译器检查标注的 ...