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

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Drawing;
  4. namespace ControlSizeChangeEx
  5. {
  6. /// <summary>
  7. /// This class implements sizing and moving functions for
  8. /// runtime editing of graphic controls
  9. /// </summary>
  10. public class PickBox
  11. {
  12. //////////////////////////////////////////////////////////////////
  13. // PRIVATE CONSTANTS AND VARIABLES
  14. //////////////////////////////////////////////////////////////////
  15. private const int BOX_SIZE = 8;
  16. private Color BOX_COLOR = Color.White;
  17. private ContainerControl m_container;
  18. private Control m_control;
  19. private Label[] lbl = new Label[8];
  20. private int startl;
  21. private int startt;
  22. private int startw;
  23. private int starth;
  24. private int startx;
  25. private int starty;
  26. private bool dragging;
  27. private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,
  28. Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,
  29. Cursors.SizeNESW, Cursors.SizeWE};
  30. private Cursor oldCursor;
  31. private const int MIN_SIZE = 20;
  32. //
  33. // Constructor creates 8 sizing handles & wires mouse events
  34. // to each that implement sizing functions
  35. //
  36. public PickBox()
  37. {
  38. for (int i = 0; i < 8; i++)
  39. {
  40. lbl[i] = new Label();
  41. lbl[i].TabIndex = i;
  42. lbl[i].FlatStyle = 0;
  43. lbl[i].BorderStyle = BorderStyle.FixedSingle;
  44. lbl[i].BackColor = BOX_COLOR;
  45. lbl[i].Cursor = arrArrow[i];
  46. lbl[i].Text = "";
  47. lbl[i].BringToFront();
  48. lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown);
  49. lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove);
  50. lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp);
  51. }
  52. }
  53. //////////////////////////////////////////////////////////////////
  54. // PUBLIC METHODS
  55. //////////////////////////////////////////////////////////////////
  56. //
  57. // Wires a Click event handler to the passed Control
  58. // that attaches a pick box to the control when it is clicked
  59. //
  60. public void WireControl(Control ctl)
  61. {
  62. ctl.Click += new EventHandler(this.SelectControl);
  63. }
  64. /////////////////////////////////////////////////////////////////
  65. // PRIVATE METHODS
  66. /////////////////////////////////////////////////////////////////
  67. //
  68. // Attaches a pick box to the sender Control
  69. //
  70. private void SelectControl(object sender, EventArgs e)
  71. {
  72. if (m_control is Control)
  73. {
  74. m_control.Cursor = oldCursor;
  75. //Remove event any pre-existing event handlers appended by this class
  76. m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown);
  77. m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove);
  78. m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
  79. m_control = null;
  80. }
  81. m_control = (Control)sender;
  82. //Add event handlers for moving the selected control around
  83. m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown);
  84. m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove);
  85. m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp);
  86. //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)
  87. for (int i = 0; i < 8; i++)
  88. {
  89. m_control.Parent.Controls.Add(lbl[i]);
  90. lbl[i].BringToFront();
  91. }
  92. //Position sizing handles around Control
  93. MoveHandles();
  94. //Display sizing handles
  95. ShowHandles();
  96. oldCursor = m_control.Cursor;
  97. m_control.Cursor = Cursors.SizeAll;
  98. }
  99. public void Remove()
  100. {
  101. HideHandles();
  102. m_control.Cursor = oldCursor;
  103. }
  104. private void ShowHandles()
  105. {
  106. if (m_control != null)
  107. {
  108. for (int i = 0; i < 8; i++)
  109. {
  110. lbl[i].Visible = true;
  111. }
  112. }
  113. }
  114. private void HideHandles()
  115. {
  116. for (int i = 0; i < 8; i++)
  117. {
  118. lbl[i].Visible = false;
  119. }
  120. }
  121. private void MoveHandles()
  122. {
  123. int sX = m_control.Left - BOX_SIZE;
  124. int sY = m_control.Top - BOX_SIZE;
  125. int sW = m_control.Width + BOX_SIZE;
  126. int sH = m_control.Height + BOX_SIZE;
  127. int hB = BOX_SIZE / 2;
  128. int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,
  129. sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};
  130. int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,
  131. sY + sH-hB, sY + sH-hB, sY + sH / 2};
  132. for (int i = 0; i < 8; i++)
  133. lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
  134. }
  135. /////////////////////////////////////////////////////////////////
  136. // MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL
  137. /////////////////////////////////////////////////////////////////
  138. //
  139. // Store control position and size when mouse button pushed over
  140. // any sizing handle
  141. //
  142. private void lbl_MouseDown(object sender, MouseEventArgs e)
  143. {
  144. dragging = true;
  145. startl = m_control.Left;
  146. startt = m_control.Top;
  147. startw = m_control.Width;
  148. starth = m_control.Height;
  149. HideHandles();
  150. }
  151. //
  152. // Size the picked control in accordance with sizing handle being dragged
  153. // 0 1 2
  154. // 7 3
  155. // 6 5 4
  156. //
  157. private void lbl_MouseMove(object sender, MouseEventArgs e)
  158. {
  159. int l = m_control.Left;
  160. int w = m_control.Width;
  161. int t = m_control.Top;
  162. int h = m_control.Height;
  163. if (dragging)
  164. {
  165. switch (((Label)sender).TabIndex)
  166. {
  167. case 0: // Dragging top-left sizing box
  168. l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
  169. t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
  170. w = startl + startw - m_control.Left;
  171. h = startt + starth - m_control.Top;
  172. break;
  173. case 1: // Dragging top-center sizing box
  174. t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
  175. h = startt + starth - m_control.Top;
  176. break;
  177. case 2: // Dragging top-right sizing box
  178. w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
  179. t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE;
  180. h = startt + starth - m_control.Top;
  181. break;
  182. case 3: // Dragging right-middle sizing box
  183. w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
  184. break;
  185. case 4: // Dragging right-bottom sizing box
  186. w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE;
  187. h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
  188. break;
  189. case 5: // Dragging center-bottom sizing box
  190. h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
  191. break;
  192. case 6: // Dragging left-bottom sizing box
  193. l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
  194. w = startl + startw - m_control.Left;
  195. h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE;
  196. break;
  197. case 7: // Dragging left-middle sizing box
  198. l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE;
  199. w = startl + startw - m_control.Left;
  200. break;
  201. }
  202. l = (l < 0) ? 0 : l;
  203. t = (t < 0) ? 0 : t;
  204. m_control.SetBounds(l, t, w, h);
  205. }
  206. }
  207. //
  208. // Display sizing handles around picked control once sizing has completed
  209. //
  210. private void lbl_MouseUp(object sender, MouseEventArgs e)
  211. {
  212. dragging = false;
  213. MoveHandles();
  214. ShowHandles();
  215. }
  216. /////////////////////////////////////////////////////////////////
  217. // MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM
  218. /////////////////////////////////////////////////////////////////
  219. //
  220. // Get mouse pointer starting position on mouse down and hide sizing handles
  221. //
  222. private void ctl_MouseDown(object sender, MouseEventArgs e)
  223. {
  224. dragging = true;
  225. startx = e.X;
  226. starty = e.Y;
  227. HideHandles();
  228. }
  229. //
  230. // Reposition the dragged control
  231. //
  232. private void ctl_MouseMove(object sender, MouseEventArgs e)
  233. {
  234. if (dragging)
  235. {
  236. int l = m_control.Left + e.X - startx;
  237. int t = m_control.Top + e.Y - starty;
  238. int w = m_control.Width;
  239. int h = m_control.Height;
  240. l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ?
  241. m_control.Parent.ClientRectangle.Width - w : l);
  242. t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ?
  243. m_control.Parent.ClientRectangle.Height - h : t);
  244. m_control.Left = l;
  245. m_control.Top = t;
  246. }
  247. }
  248. //
  249. // Display sizing handles around picked control once dragging has completed
  250. //
  251. private void ctl_MouseUp(object sender, MouseEventArgs e)
  252. {
  253. dragging = false;
  254. MoveHandles();
  255. ShowHandles();
  256. }
  257. }
  258. }

  

创建一个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. Reflection应用场景-利用反射机制将表单数据自动填充到JavaBean中

  2. 实现一个小目标,动动小指,分享可得iphone7/ipad/U盘|奥威软件

    为什么很多人喜欢冬天呢?是因为冬天有着“床边挂起长长棉袜,而你做着甜甜美梦”的平安夜?还是因为冬天有着“具有无法言述的浪漫情怀”的圣诞节? 是不是还沉浸在平安夜,圣诞节的双节狂欢中, 好像不管长到多大 ...

  3. 编译错误:/usr/bin/ld: cannot find -lz

    编译时出现错误/usr/bin/ld: cannot find -lz,安装zlib和zlib-devel yum install zlib yum install zlib-devel

  4. iis发布网站怎么支持.json文件

  5. pod install 无限卡顿

    pod install  被墙了,请大家换成pod install --verbose --no-repo-update

  6. 夺命雷公狗-----React---18--value和defaultValue的区别

    <!DOCTYPE> <html> <head> <meta charset="utf-8"> <title></ ...

  7. web分享QQ好友、QQ空间、新浪微博的api接口

    QZone: "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={{URL}}&title={{TITL ...

  8. Entity Framework7 入门之全功能.NET版本下使用EF7(含源码)另附数据迁移常见错误处理

    Entity Framework7 入门之全功能.NET(Console, WinForms, WPF等)使用EF7 昨天,我们介绍了EF的新特性和开发计划,如果你还不了解,请移步 Entity Fr ...

  9. IE9浏览器中的My97日历控件刷新后无法打开问题解决办法

    解决办法如下: 1:找到WdatePicker.js 2:将$crossFrame:true 改为$crossFrame:false. 3:重启服务,刷新页面发现OK.

  10. MySql SELECT INTO 记录为空的问题

    SELECT Id INTO @Id FROM GiftCode WHERE Status = 1 AND GiftId = #GiftId# ORDER BY Id ASC LIMIT 1;     ...