效果图
主体代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace FrameControl
{
public partial class MoveTextBox : UserControl
{
/// <summary>
/// 鼠标点击该控件时的事件
/// </summary>
public event Action<object, EventArgs> CtrMouseClick;
public event Action<object, KeyEventArgs> CtrKeyDown;
enum MousePosOnCtrl
{
NONE = 0,
TOP = 1,
RIGHT = 2,
BOTTOM = 3,
LEFT = 4,
TOPLEFT = 5,
TOPRIGHT = 6,
BOTTOMLEFT = 7,
BOTTOMRIGHT = 8,
}
Dictionary<Panel, MousePosOnCtrl> _dictPanl = new Dictionary<Panel, MousePosOnCtrl>();
MousePosOnCtrl _mpoc; //鼠标在控件中位置
const int MinWidth = 20; //最小宽度
const int MinHeight = 20;//最小高度
public TextBox TextBox
{
get { return this.textBox1; }
set { this.textBox1 = value; }
}
public MoveTextBox()
{
InitializeComponent();
_dictPanl[this.panelTL] = MousePosOnCtrl.TOPLEFT;
_dictPanl[this.panelT] = MousePosOnCtrl.TOP;
_dictPanl[this.panelTR] = MousePosOnCtrl.TOPRIGHT;
_dictPanl[this.panelL] = MousePosOnCtrl.LEFT;
_dictPanl[this.panelR] = MousePosOnCtrl.RIGHT;
_dictPanl[this.panelBL] = MousePosOnCtrl.BOTTOMLEFT;
_dictPanl[this.panelB] = MousePosOnCtrl.BOTTOM;
_dictPanl[this.panelBR] = MousePosOnCtrl.BOTTOMRIGHT;
foreach (var kv in _dictPanl)
{
kv.Key.BringToFront();
kv.Key.MouseMove += Panel_MouseMove;
kv.Key.MouseLeave += Panel_MouseLeave;
kv.Key.MouseDown += Panel_MouseDown;
}
SetControlEnabled(this.textBox1, false);
boundVisible(false);
}
Point _lastLocation; //上个鼠标坐标
Size _lastSize;
private void Panel_MouseDown(object sender, MouseEventArgs e)
{
_lastMousePoint = Cursor.Position;
_lastLocation = this.Location;
_lastSize = this.Size;
}
bool SetCursorShape(int x, int y)
{
switch (_mpoc)
{
case MousePosOnCtrl.TOPLEFT:
case MousePosOnCtrl.BOTTOMRIGHT:
Cursor.Current = Cursors.SizeNWSE; //左上
break;
case MousePosOnCtrl.TOPRIGHT:
case MousePosOnCtrl.BOTTOMLEFT:
Cursor.Current = Cursors.SizeNESW; //右上
break;
case MousePosOnCtrl.TOP:
case MousePosOnCtrl.BOTTOM:
Cursor.Current = Cursors.SizeNS; //上
break;
case MousePosOnCtrl.LEFT:
case MousePosOnCtrl.RIGHT:
Cursor.Current = Cursors.SizeWE; //左
break;
default:
Cursor.Current = Cursors.SizeAll;
break;
}
return true;
}
void ControlMove()
{
Point currMousePoint = Cursor.Position;
int x = currMousePoint.X - _lastMousePoint.X;
int y = currMousePoint.Y - _lastMousePoint.Y;
switch (this._mpoc)
{
case MousePosOnCtrl.TOP:
this.Top = _lastLocation.Y + y;
this.Height = _lastSize.Height - y;
Cursor.Current = Cursors.SizeNS; //上
break;
case MousePosOnCtrl.BOTTOM:
this.Height = _lastSize.Height + y;
Cursor.Current = Cursors.SizeNS; //上
break;
case MousePosOnCtrl.LEFT:
this.Left = _lastLocation.X + x;
this.Width = _lastSize.Width - x;
Cursor.Current = Cursors.SizeWE; //左
break;
case MousePosOnCtrl.RIGHT:
this.Width = _lastSize.Width + x;
Cursor.Current = Cursors.SizeWE; //左
break;
case MousePosOnCtrl.TOPLEFT:
this.Top = _lastLocation.Y + y;
this.Height = _lastSize.Height - y;
this.Left = _lastLocation.X + x;
this.Width = _lastSize.Width - x;
Cursor.Current = Cursors.SizeNWSE; //左上
break;
case MousePosOnCtrl.TOPRIGHT:
this.Top = _lastLocation.Y + y;
this.Height = _lastSize.Height - y;
this.Width = _lastSize.Width + x;
Cursor.Current = Cursors.SizeNESW; //右上
break;
case MousePosOnCtrl.BOTTOMLEFT:
this.Height = _lastSize.Height + y;
this.Left = _lastLocation.X + x;
this.Width = _lastSize.Width - x;
Cursor.Current = Cursors.SizeNESW; //右上
break;
case MousePosOnCtrl.BOTTOMRIGHT:
this.Width = _lastSize.Width + x;
this.Height = _lastSize.Height + y;
Cursor.Current = Cursors.SizeNWSE; //左上
break;
default:
Cursor.Current = Cursors.SizeAll;
break;
}
}
private void Panel_MouseLeave(object sender, EventArgs e)
{
Cursor.Current = Cursors.Default;
_mpoc = MousePosOnCtrl.NONE;
}
private void Panel_MouseMove(object sender, MouseEventArgs e)
{
Panel p = sender as Panel;
_mpoc = _dictPanl[p];
SetCursorShape(e.X, e.Y);
if (e.Button == MouseButtons.Left)
{
ControlMove();
}
}
void boundVisible(bool isVisible)
{
this.panelContain.BorderStyle = isVisible ? BorderStyle.FixedSingle : BorderStyle.None;
foreach (var kv in _dictPanl)
{
kv.Key.Visible = isVisible;
}
}
bool _tbEnable = false;
/// <summary>
/// 使该控件具有焦点,可以输入信息
/// </summary>
/// <param name="focus"></param>
public void SetFocus(bool focus)
{
boundVisible(focus);
if (!focus)
{
if (_tbEnable)
{
SetControlEnabled(this.textBox1, false);
_tbEnable = false;
}
}
}
private void UserControl1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (!_tbEnable)
{
SetControlEnabled(this.textBox1, true);
_tbEnable = true;
}
boundVisible(false);
}
Point _lastMousePoint; //上个鼠标坐标
private void MoveCtr_MouseDown(object sender, MouseEventArgs e)
{
boundVisible(true);
_lastMousePoint = Cursor.Position;
if (CtrMouseClick != null)
{
CtrMouseClick(this, e);
}
}
private void MoveCtr_MouseMove(object sender, MouseEventArgs e)
{
Point curPoint = Cursor.Position;
if (e.Button == MouseButtons.Left)
{
if (_mpoc == MousePosOnCtrl.NONE)
{
Cursor.Current = Cursors.SizeAll;
int x = curPoint.X - _lastMousePoint.X;
int y = curPoint.Y - _lastMousePoint.Y;
this.Location = new Point(this.Location.X + x, this.Location.Y + y);
_lastMousePoint = curPoint;
this.BringToFront();
}
}
}
private void MoveTB_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
this.Top += 1;
break;
case Keys.Down:
this.Top -= 1;
break;
case Keys.Left:
this.Left -= 1;
break;
case Keys.Right:
this.Left += 1;
break;
}
if (CtrKeyDown != null)
{
CtrKeyDown(this, e);
}
}
/// <summary>
/// 复制该控件的基本信息
/// </summary>
/// <returns></returns>
public MoveTextBox Copy()
{
MoveTextBox moveTB = new MoveTextBox();
moveTB.TextBox.Font = this.textBox1.Font;
moveTB.TextBox.ForeColor = this.textBox1.ForeColor;
moveTB.TextBox.BackColor = this.textBox1.BackColor;
moveTB.TextBox.Text = this.textBox1.Text;
moveTB.BackgroundImage = this.BackgroundImage;
return moveTB;
}
[System.Runtime.InteropServices.DllImport("user32.dll ")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
[System.Runtime.InteropServices.DllImport("user32.dll ")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
const int GWL_STYLE = -16;
const int WS_DISABLED = 0x8000000;
public static void SetControlEnabled(Control c, bool enabled)
{
if (enabled)
{
SetWindowLong(c.Handle, GWL_STYLE, (~WS_DISABLED) & GetWindowLong(c.Handle, GWL_STYLE));
}
else
{
SetWindowLong(c.Handle, GWL_STYLE, WS_DISABLED + GetWindowLong(c.Handle, GWL_STYLE));
}
}
}
}
设计器代码
namespace FrameControl
{
partial class MoveTextBox
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panelContain = new System.Windows.Forms.Panel();
this.textBox1 = new System.Windows.Forms.TextBox();
this.panelR = new System.Windows.Forms.Panel();
this.panelBR = new System.Windows.Forms.Panel();
this.panelB = new System.Windows.Forms.Panel();
this.panelBL = new System.Windows.Forms.Panel();
this.panelL = new System.Windows.Forms.Panel();
this.panelTR = new System.Windows.Forms.Panel();
this.panelTL = new System.Windows.Forms.Panel();
this.panelT = new System.Windows.Forms.Panel();
this.panelContain.SuspendLayout();
this.SuspendLayout();
//
// panelContain
//
this.panelContain.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panelContain.Controls.Add(this.textBox1);
this.panelContain.Location = new System.Drawing.Point(2, 2);
this.panelContain.Name = "panelContain";
this.panelContain.Size = new System.Drawing.Size(146, 26);
this.panelContain.TabIndex = 0;
this.panelContain.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.UserControl1_MouseDoubleClick);
this.panelContain.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MoveCtr_MouseDown);
this.panelContain.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MoveCtr_MouseMove);
//
// textBox1
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.BackColor = System.Drawing.Color.White;
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox1.Font = new System.Drawing.Font("宋体", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(146, 26);
this.textBox1.TabIndex = 35;
this.textBox1.Text = "New Item";
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// panelR
//
this.panelR.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.panelR.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelR.Location = new System.Drawing.Point(145, 13);
this.panelR.Name = "panelR";
this.panelR.Size = new System.Drawing.Size(5, 5);
this.panelR.TabIndex = 33;
//
// panelBR
//
this.panelBR.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.panelBR.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelBR.Location = new System.Drawing.Point(145, 25);
this.panelBR.Name = "panelBR";
this.panelBR.Size = new System.Drawing.Size(5, 5);
this.panelBR.TabIndex = 32;
//
// panelB
//
this.panelB.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.panelB.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelB.Location = new System.Drawing.Point(73, 25);
this.panelB.Name = "panelB";
this.panelB.Size = new System.Drawing.Size(5, 5);
this.panelB.TabIndex = 31;
//
// panelBL
//
this.panelBL.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.panelBL.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelBL.Location = new System.Drawing.Point(0, 25);
this.panelBL.Name = "panelBL";
this.panelBL.Size = new System.Drawing.Size(5, 5);
this.panelBL.TabIndex = 30;
//
// panelL
//
this.panelL.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.panelL.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelL.Location = new System.Drawing.Point(0, 13);
this.panelL.Name = "panelL";
this.panelL.Size = new System.Drawing.Size(5, 5);
this.panelL.TabIndex = 27;
//
// panelTR
//
this.panelTR.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.panelTR.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelTR.Location = new System.Drawing.Point(145, 0);
this.panelTR.Name = "panelTR";
this.panelTR.Size = new System.Drawing.Size(5, 5);
this.panelTR.TabIndex = 29;
//
// panelTL
//
this.panelTL.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelTL.Location = new System.Drawing.Point(0, 0);
this.panelTL.Name = "panelTL";
this.panelTL.Size = new System.Drawing.Size(5, 5);
this.panelTL.TabIndex = 28;
//
// panelT
//
this.panelT.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.panelT.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.panelT.Location = new System.Drawing.Point(73, 0);
this.panelT.Name = "panelT";
this.panelT.Size = new System.Drawing.Size(5, 5);
this.panelT.TabIndex = 26;
//
// MoveTB
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Transparent;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.Controls.Add(this.panelR);
this.Controls.Add(this.panelBR);
this.Controls.Add(this.panelB);
this.Controls.Add(this.panelBL);
this.Controls.Add(this.panelL);
this.Controls.Add(this.panelTR);
this.Controls.Add(this.panelTL);
this.Controls.Add(this.panelT);
this.Controls.Add(this.panelContain);
this.Name = "MoveTB";
this.Size = new System.Drawing.Size(150, 30);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.MoveTB_KeyDown);
this.panelContain.ResumeLayout(false);
this.panelContain.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panelContain;
private System.Windows.Forms.Panel panelR;
private System.Windows.Forms.Panel panelBR;
private System.Windows.Forms.Panel panelB;
private System.Windows.Forms.Panel panelBL;
private System.Windows.Forms.Panel panelL;
private System.Windows.Forms.Panel panelTR;
private System.Windows.Forms.Panel panelTL;
private System.Windows.Forms.Panel panelT;
private System.Windows.Forms.TextBox textBox1;
}
}
样例下载
下载:分不能控制的,可以私信