在用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 = 8;
private Color BOX_COLOR = Color.White;
private ContainerControl m_container;
private Control m_control;
private Label[] lbl = new Label[8];
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 = 20;
//
// Constructor creates 8 sizing handles & wires mouse events
// to each that implement sizing functions
//
public PickBox()
{
for (int i = 0; i < 8; i++)
{
lbl[i] = new Label();
lbl[i].TabIndex = i;
lbl[i].FlatStyle = 0;
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 = 0; i < 8; 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 = 0; i < 8; i++)
{
lbl[i].Visible = true;
}
}
}
private void HideHandles()
{
for (int i = 0; i < 8; 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 / 2;
int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,
sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};
int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,
sY + sH-hB, sY + sH-hB, sY + sH / 2};
for (int i = 0; i < 8; 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 0: // 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 1: // 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 2: // 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 3: // Dragging right-middle sizing box
w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
break;
case 4: // 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 5: // Dragging center-bottom sizing box
h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
break;
case 6: // 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 7: // 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 < 0) ? 0 : l;
t = (t < 0) ? 0 : 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 < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ?
m_control.Parent.ClientRectangle.Width - w : l);
t = (t < 0) ? 0 : ((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(你希望改变的控件);方法就行了。

C#自定义大小与改变大下的方法的更多相关文章

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

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

  2. UILabletext去掉乱码 控制颜色 行高 自定义大小 。显示不同的字体颜色、字体大小、行间距、首行缩进、下划线等属性(NSMutableAttributedString)

    text去掉乱码 设置不同颜色 行高 自定义大小 #import <Foundation/Foundation.h> @interface TextsForRow : NSObject @ ...

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

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

  4. vue使用qrcode生成二维码,可以自定义大小

    1,qrcanvas-vue插件,https://gera2ld.github.io/qrcanvas-vue/#logo.只支持像素大小的二维码 2,qrcode支持移动端自定义大小二维码 &quo ...

  5. JAVA代码:生成一个集合,自定义大小,100以内的随机整数

    JAVA代码:生成一个集合,自定义大小,100以内的随机整数 方法一:(Random类) package com.dawa.test; import java.util.ArrayList; impo ...

  6. Linux培训教程 linux系统下分割大文件的方法

    在linux中分割大文件,比如一个5gb日志文件,需要把它分成多个小文件,分割后以利于普通的文本编辑器读取. 有时,需要传输20gb的大文件,Linux培训 教程件到另一台服务器,也需要把它分割成多个 ...

  7. < python PIL - 批量图像处理 - 生成自定义大小图像 >

    < python PIL - 批量图像处理 - 生成自定义大小图像 > 直接用python自带的PIL图像库,对一个文件夹下所有jpg/png的图像进行自定义像素变换 from PIL i ...

  8. WPF中如何调整TabControl的大小,使其跟随Window的大小而改变?

    多年不写技术博客,手生的很,也不知道大家都关注什么,最近在研究Wpf及3d模型的展示,碰到很多问题,这个是最后一个问题,写出来小结一下...... WPF中如何调整TabControl的大小,使其跟随 ...

  9. DOM内容操作和自定义、样式改变

    自定义 function 方法名或函数名(参数1,参数2,...) { 方法体: return返回值:(可不写) } function abc() { alert("123"); ...

随机推荐

  1. CSS 的定位方式和含义

    CSS 的定位方式和含义 总结一下 CSS 的定位方式.CSS 的定位 position 是处理页面布局时非常重要的属性. CSS 中有 3 种基本的定位机制:普通流.浮动和绝对定位. 在没有指定的情 ...

  2. 删除smartygit的配置文件

    SmartGit Windows: %APPDATA%\syntevo\SmartGit\OS X: ~/Library/Preferences/SmartGit/Unix/Linux: ~/.sma ...

  3. (转载)Resin安装配置及使用教程

    Resin是一个提供高性能的,支持 Java/PHP 的应用服务器.目前有两个版本:一个是GPL下的开源版本,提供给一些爱好者.开发人员和低流量网站使用:一种是收费的专业版本,增加了一些更加适用于生产 ...

  4. 把 MWeb Lite 的文档库文档和数据搬到 MWeb 正式版中

    MWeb Lite 版的文档库中的文档要搬到 MWeb 正式版中,如果 Lite 版的文档中没有图片或者只有少量图片,可以用导入导出为 Markdown 的方法. 否则的话请用以下方式(注意下面这个方 ...

  5. GPS部标平台的架构设计(六)-Android手机客户端和手机查车设计

    对于GPS软件平台,虽然有功能非常丰富的PC端或BS客户端,但是客户也是需要移动客户端来作为自己的辅助工具,也是需要的.做为GPS平台的设计者和开发者,在开发移动客户端的时候,也需要从常规的服务器开发 ...

  6. No compiler is provided in this environment. Perhaps you are running on a JRE ra

    No compiler is provided in this environment. Perhaps you are running on a JRE ra,有需要的朋友可以参考下. 控制台输出的 ...

  7. Word2013中制作按钮控件

    1.由于“开发工具”不经常用,所以在功能选项面板中没有“开发工具”这一栏.所以我们需要设置.在功能选项面板中选择“文件”,在跳转出来的版面中选择“选项”.

  8. Caffe框架下的图像回归测试

    Caffe框架下的图像回归测试 参考资料: 1. http://stackoverflow.com/questions/33766689/caffe-hdf5-pre-processing 2. ht ...

  9. GUI生成exe文件

    gui如何生成exe文件: 已经有gui.m和gui.fig文件 1 安装编译器.已经安装好了vs10的. 2 设置编译器.在matlab命令行输入mex -setup,选择安装的c编译器 3 调用编 ...

  10. 《C++ API设计》作者Martin Reddy访谈问题征集

    Martin Reddy博士是软件行业的一名老兵,有着15年以上的从业经验,共撰写过40多篇论文,拥有3项软件专利,并与他人合著了Level of Detail for 3D Graphics.另外, ...