日期:2014-05-17 浏览次数:20911 次
1.使用鼠标控制位置:在任意位置点击左键拖动
2.使用鼠标控制大小:在控件右下角待变为‘调整光标’是可以调整大小
3.使用键盘控制位置:alt + 方向键
4.使用键盘控制大小:ctrl + 方向键
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace AppGenCode.Printer
{
public enum EnumAdjust
{
Nothing =0,
Size = 1,
Postion = 2
}
public partial class AdjustTextBox : TextBox
{
public AdjustTextBox()
{
InitializeComponent();
InitCtrl();
}
public AdjustTextBox(IContainer container)
{
container.Add(this);
InitializeComponent();
InitCtrl();
}
private bool m_isMoving = false;
private Point m_offset = new Point(0,0);
private Size m_intSize = new Size(0, 0);
private EnumAdjust m_adjust = EnumAdjust.Nothing;
private int m_x, m_y, m_w, m_h = 0;
private Rectangle RecAdjustSize
{
get { return new Rectangle(this.Width-15,this.Height-15,15,15);}
}
private Rectangle RecAdjustPostion
{
get { return new Rectangle(0, 0, this.Width, this.Height); }
}
private void InitCtrl()
{
this.Text = "asdf1234";
this.Multiline = true;
this.BorderStyle = BorderStyle.FixedSingle;
this.ReadOnly = true;
}
#region 鼠标事件
protected override void OnMouseDown(MouseEventArgs e)
{
m_isMoving = true;
m_offset = new Point(e.X, e.Y);
m_intSize.Width = this.Width;
m_intSize.Height = this.Height;
if (RecAdjustSize.Contains(e.X, e.Y)) //调整大小
{
this.Cursor = Cursors.SizeNWSE;
m_adjust = EnumAdjust.Size;
}
else if (RecAdjustPostion.Contains(e.X, e.Y)) //调整位置
{
this.Cursor = Cursors.SizeAll;
m_adjust = EnumAdjust.Postion;
}
else
{
m_adjust = EnumAdjust.Nothing;
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
Cursor = Cursors.Default;
m_isMoving = false;
base.OnMouseUp(mevent);
}
protected override void OnMouseMove(MouseEventArgs e)
{
CursorCtrl(e.X, e.Y);
m_x = this.Left;
m_y = this.Top;
m_w = this.Width;
m_h = this.Height;
if (m_isMoving)
{
switch (m_adjust)
{
case EnumAdjust.Size:
m_w = m_intSize.Width + (e.X - m_offset.X);
m_h = m_intSize.Height + (e.