现在的位置: 首页 > 综合 > 正文

c# winform 一个可以用鼠标改变控件位置和大小的类,调用即可

2011年07月26日 ⁄ 综合 ⁄ 共 3375字 ⁄ 字号 评论关闭
C#代码 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.ComponentModel;  
  5. using System.Data;  
  6. using System.Drawing;  
  7. using System.Windows.Forms;  
  8.   
  9. namespace AllTest  
  10. {  
  11.     class BarcodeControl  
  12.     {  
  13.         #region private  
  14.   
  15.         private const int MIN_SIZE = 10; //对控件缩放的最小值   
  16.         private const int BOX_SIZE = 7;  //调整大小触模柄方框大小   
  17.   
  18.         public bool _IsCtrlKey = false;  
  19.         private TextBox _textbox;  
  20.         private Control _MControl = null;  
  21.         private bool _IsMouseDown = false;  
  22.         private Point _oPointClicked;  
  23.         private Color BOX_COLOR = Color.White;  
  24.         private Label[] _lbl = new Label[8];  
  25.         private int _startl, _startt, _startw, _starth;  
  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.  
  31.         #endregion  
  32.   
  33.         #region 构造函数  
  34.   
  35.         /// <summary>   
  36.         /// 构造控件拖动对象   
  37.         /// </summary>   
  38.         /// <param name="moveControl">需要拖动的控件 </param>   
  39.         public BarcodeControl(Control moveControl)  
  40.         {  
  41.             //   
  42.             // TODO: 在此处添加构造函数逻辑   
  43.             //   
  44.             _MControl = moveControl;  
  45.             _MControl.MouseDown += new MouseEventHandler(this.Control_MouseDown);  
  46.             _MControl.MouseUp += new MouseEventHandler(this.Control_MouseUp);  
  47.             _MControl.MouseMove += new MouseEventHandler(this.Control_MouseMove);  
  48.             _MControl.Click += new System.EventHandler(this.Control_Click);  
  49.   
  50.             //构造8个调整大小触模柄   
  51.             for (int i = 0; i < 8; i++)  
  52.             {  
  53.                 _lbl[i] = new Label();  
  54.                 _lbl[i].TabIndex = i;  
  55.                 _lbl[i].FlatStyle = 0;  
  56.                 _lbl[i].BorderStyle = BorderStyle.FixedSingle;  
  57.                 _lbl[i].BackColor = BOX_COLOR;  
  58.                 _lbl[i].Cursor = _arrArrow[i];  
  59.                 _lbl[i].Text = "";  
  60.                 _lbl[i].BringToFront();  
  61.                 _lbl[i].MouseDown += new MouseEventHandler(this.handle_MouseDown);  
  62.                 _lbl[i].MouseMove += new MouseEventHandler(this.handle_MouseMove);  
  63.                 _lbl[i].MouseUp += new MouseEventHandler(this.handle_MouseUp);  
  64.             }  
  65.   
  66.             CreateTextBox();  
  67.             Create();  
  68.   
  69.             //Control_Click((object)sender, (System.EventArgs)e);   
  70.         }  
  71.  
  72.         #endregion  
  73.   
  74.         #region 需拖动控件键盘事件  
  75.   
  76.         private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)  
  77.         {  
  78.             if (e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 39 || e.KeyValue == 40)  
  79.             {  
  80.                 if (e.KeyValue == 37)  
  81.                     _MControl.Left -= 1;  
  82.                 if (e.KeyValue == 38)  
  83.                     _MControl.Top -= 1;  
  84.                 if (e.KeyValue == 39)  
  85.                     _MControl.Left += 1;  
  86.                 if (e.KeyValue == 40)  
  87.                     _MControl.Top += 1;  
  88.                 MoveHandles();  
  89.                 ControlLocality();  
  90.                 _MControl.Visible = true;  
  91.             }  
  92.   
  93.             if (e.KeyValue == 46)  
  94.             {  
  95.                 for (int i = 0; i < 8; i++)  

抱歉!评论已关闭.