c# winform 一个可以用鼠标改变控件位置和大小的类,调用即可
2011年07月26日
⁄ 综合
⁄ 共 3375字 ⁄ 字号
小 中 大
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
-
- namespace AllTest
- {
- class BarcodeControl
- {
- #region private
-
- private const int MIN_SIZE = 10;
- private const int BOX_SIZE = 7;
-
- public bool _IsCtrlKey = false;
- private TextBox _textbox;
- private Control _MControl = null;
- private bool _IsMouseDown = false;
- private Point _oPointClicked;
- private Color BOX_COLOR = Color.White;
- private Label[] _lbl = new Label[8];
- private int _startl, _startt, _startw, _starth;
- private bool _dragging;
- private Cursor[] _arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS,
- Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS,
- Cursors.SizeNESW, Cursors.SizeWE};
-
- #endregion
-
- #region 构造函数
-
-
-
-
-
- public BarcodeControl(Control moveControl)
- {
-
-
-
- _MControl = moveControl;
- _MControl.MouseDown += new MouseEventHandler(this.Control_MouseDown);
- _MControl.MouseUp += new MouseEventHandler(this.Control_MouseUp);
- _MControl.MouseMove += new MouseEventHandler(this.Control_MouseMove);
- _MControl.Click += new System.EventHandler(this.Control_Click);
-
-
- 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.handle_MouseDown);
- _lbl[i].MouseMove += new MouseEventHandler(this.handle_MouseMove);
- _lbl[i].MouseUp += new MouseEventHandler(this.handle_MouseUp);
- }
-
- CreateTextBox();
- Create();
-
-
- }
-
- #endregion
-
- #region 需拖动控件键盘事件
-
- private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
- {
- if (e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 39 || e.KeyValue == 40)
- {
- if (e.KeyValue == 37)
- _MControl.Left -= 1;
- if (e.KeyValue == 38)
- _MControl.Top -= 1;
- if (e.KeyValue == 39)
- _MControl.Left += 1;
- if (e.KeyValue == 40)
- _MControl.Top += 1;
- MoveHandles();
- ControlLocality();
- _MControl.Visible = true;
- }
-
- if (e.KeyValue == 46)
- {
- for (int i = 0; i < 8; i++)