winform在程序运行过程中拖动和改变控件大小

hchuan 2006-04-06 03:06:00
winform在程序运行中,鼠标选中控件,控件呈选中状态,并能拖动拖动鼠标改变控件大小和位置,如从工具箱中创建控件一样的效果,兄弟提供完整代码。
...全文
780 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Samen168 2007-04-06
  • 打赏
  • 举报
回复
鼠标事件
TheRule 2007-04-06
  • 打赏
  • 举报
回复
向右拉伸的例子:
private int x0, y0;
private Point localtion;
private void button1_MouseMove(object sender, MouseEventArgs e)
{
int i =button1.Width-e.X;
if (i > 0 && i < 5)
{
this.Cursor = Cursors.SizeWE;
}
}

private void button1_MouseDown(object sender, MouseEventArgs e)
{
int i = button1.Width - e.X;
if (i > 0 && i < 5)
{
BeganDrag = true;
localtion = new Point(button1.Location.X, button1.Location.Y);
x0 = e.X;
y0 = e.Y;
}
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
button1.Width = button1.Width + e.X - x0;
this.Cursor = Cursors.Default;
}
如果是想改变其位置,使用:
button1.Location = new Point(button1.Location.X + e.X - x0, button1.Location.Y + e.Y - y0);
tongxudong 2007-04-06
  • 打赏
  • 举报
回复
liujiwe79(专业做控件) 我看你还是低调点,学习学习人家xiaxilin(good good study, day day up!) ,那才是有效的解决方案
Whidbey_Orcas 2007-04-06
  • 打赏
  • 举报
回复
楼上的方法或许可以解决问题,但是有没有考虑刷新?
liujiwe79 2007-04-06
  • 打赏
  • 举报
回复
//是否按下
private bool is_down = false;

private void button1_MouseDown(object sender, MouseEventArgs e)
{
is_down = true;
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
is_down = false;
}

private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (is_down)
{
this.button1.Location = e.Location;
}
}

private void button1_MouseLeave(object sender, EventArgs e)
{
is_down = false;
}
一个简单的按钮,你自己添加这几个事件试试
哈哈007哈 2007-04-06
  • 打赏
  • 举报
回复
}
l = (l < 0) ? 0 : l;
t = (t < 0) ? 0 : t;


//((Panel)this._MControl.Parent.Parent).ScrollControlIntoView(_MControl);

//横向超出 竖向没有超出
if (l + w > this._MControl.Parent.Width && t + h <= this._MControl.Parent.Height)
{
_MControl.SetBounds(l, t, this._MControl.Parent.Width - l, h);
return;
}
//横向没有超出 竖向超出
else if (l + w <= this._MControl.Parent.Width && t + h > this._MControl.Parent.Height)
{
_MControl.SetBounds(l, t, w, this._MControl.Parent.Width - t);
return;
}
//横向超出 竖向超出
else if (l + w > this._MControl.Parent.Width && t + h > this._MControl.Parent.Height)
{
_MControl.SetBounds(l, t, this._MControl.Parent.Width - l, this._MControl.Parent.Width - t);
return;
}

_MControl.SetBounds(l, t, w, h);

}
}

private void handle_MouseUp(object sender, MouseEventArgs e)
{
_dragging = false;
MoveHandles();
ShowHandles();
this._noteDoc.Active_Control(this._MControl);
((Panel)this._MControl.Parent.Parent).ScrollControlIntoView(this._MControl);
}

#endregion private方法

#region private方法

private void Create()
{
//_IsMouseDown = true;
//_oPointClicked = new Point(e.X,e.Y);
for (int i = 0; i < 8; i++)
{
_MControl.Parent.Controls.Add(_lbl[i]);
_lbl[i].BringToFront();
}
HideHandles();
}

private void CreateTextBox()
{
_textbox = new TextBox();
_textbox.CreateControl();
_textbox.Parent = _MControl.Parent;
_textbox.Width = 0;
_textbox.Height = 0;
_textbox.TabStop = true;
_textbox.KeyDown += new System.Windows.Forms.KeyEventHandler(textBox_KeyDown);
}

private void ControlLocality()
{
if (_MControl.Location.X < 0)
{
_MControl.Visible = false;
_MControl.Left = 0;
}
if (_MControl.Location.Y < 0)
{
_MControl.Visible = false;
_MControl.Top = 0;
}
if (_MControl.Location.X + _MControl.Width > _MControl.Parent.Width)
{
_MControl.Visible = false;
_MControl.Left = _MControl.Parent.Width - _MControl.Width;
}
if (_MControl.Location.Y + _MControl.Height > _MControl.Parent.Height)
{
_MControl.Visible = false;
_MControl.Top = _MControl.Parent.Height - _MControl.Height;
}
}

private void HideHandles()
{
for (int i = 0; i < 8; i++)
{
_lbl[i].Visible = false;
}
}

private void MoveHandles()
{
int sX = _MControl.Left - BOX_SIZE;
int sY = _MControl.Top - BOX_SIZE;
int sW = _MControl.Width + BOX_SIZE;
int sH = _MControl.Height + BOX_SIZE;
int hB = BOX_SIZE / 2;
int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB,
sX + sW-hB, sX + sW / 2, sX+hB, sX+hB};
int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB,
sY + sH-hB, sY + sH-hB, sY + sH / 2};
for (int i = 0; i < 8; i++)
{
_lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
}
}

private void ShowHandles()
{
if (_MControl != null)
{
for (int i = 0; i < 8; i++)
{
_lbl[i].Visible = true;

}
}
}

#endregion
}
}
哈哈007哈 2007-04-06
  • 打赏
  • 举报
回复
private void Control_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
_IsMouseDown = false;
MoveHandles();
ShowHandles();
_MControl.Visible = true;
((Panel)this._MControl.Parent.Parent).ScrollControlIntoView(this._MControl);
this._MControl.Select();
this._noteDoc.Active_Control(this._MControl);
}

private void Control_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
//this._MControl.Cursor = Cursors.SizeAll;
if (_IsMouseDown)
{
int l = _MControl.Left + e.X - _oPointClicked.X;
int t = _MControl.Top + e.Y - _oPointClicked.Y;
int w = _MControl.Width;
int h = _MControl.Height;
l = (l < 0) ? 0 : ((l + w > _MControl.Parent.ClientRectangle.Width) ?
_MControl.Parent.ClientRectangle.Width - w : l);
t = (t < 0) ? 0 : ((t + h > _MControl.Parent.ClientRectangle.Height) ?
_MControl.Parent.ClientRectangle.Height - h : t);
_MControl.Left = l;
_MControl.Top = t;
ControlLocality();
}
_MControl.Cursor = Cursors.SizeAll;
//_MControl.Cursor=Cursors.SizeAll;
((Panel)this._MControl.Parent.Parent).ScrollControlIntoView(this._MControl);

}

#endregion

#region 调整大小触模柄鼠标事件

private void handle_MouseDown(object sender, MouseEventArgs e)
{
_dragging = true;
_startl = _MControl.Left;
_startt = _MControl.Top;
_startw = _MControl.Width;
_starth = _MControl.Height;
HideHandles();
((Panel)this._MControl.Parent.Parent).ScrollControlIntoView(this._MControl);
}

// 通过触模柄调整控件大小
// 0 1 2
// 7 3
// 6 5 4
private void handle_MouseMove(object sender, MouseEventArgs e)
{
int l = _MControl.Left;
int w = _MControl.Width;
int t = _MControl.Top;
int h = _MControl.Height;
if (_dragging)
{
switch (((Label)sender).TabIndex)
{
//l算法:控件左边X坐标 + 鼠标在触模柄X坐标 < 控件左边X坐标 + 父控件宽度 - 控件大小 ?控件左边X坐标 + 鼠标在触模柄X坐标 :控件左边X坐标 + 父控件宽度 - 控件大小
//t算法:
//w算法:
//h算法:
case 0: // _dragging top-left sizing box
l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;
t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;
w = _startl + _startw - _MControl.Left;
h = _startt + _starth - _MControl.Top;
break;
case 1: // _dragging top-center sizing box
t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;
h = _startt + _starth - _MControl.Top;
break;
case 2: // _dragging top-right sizing box
w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;
t = _startt + e.Y < _startt + _starth - MIN_SIZE ? _startt + e.Y : _startt + _starth - MIN_SIZE;
h = _startt + _starth - _MControl.Top;
break;
case 3: // _dragging right-middle sizing box
w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;
break;
case 4: // _dragging right-bottom sizing box
w = _startw + e.X > MIN_SIZE ? _startw + e.X : MIN_SIZE;
h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;
break;
case 5: // _dragging center-bottom sizing box
h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;
break;
case 6: // _dragging left-bottom sizing box
l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;
w = _startl + _startw - _MControl.Left;
h = _starth + e.Y > MIN_SIZE ? _starth + e.Y : MIN_SIZE;
break;
case 7: // _dragging left-middle sizing box
l = _startl + e.X < _startl + _startw - MIN_SIZE ? _startl + e.X : _startl + _startw - MIN_SIZE;
w = _startl + _startw - _MControl.Left;
break;
哈哈007哈 2007-04-06
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Sinxsoft.NotePrinter.Forms.Design;

namespace Sinxsoft.NotePrinter
{
public class ControlMove
{
#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};

private NoteDoc _noteDoc = null;
#endregion

#region 构造函数

/**/
/// <summary>
/// 构造控件拖动对象
/// </summary>
/// <param name="moveControl">需要拖动的控件</param>
public ControlMove(Control moveControl)
{
//
// TODO: 在此处添加构造函数逻辑
//
_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);

//构造8个调整大小触模柄
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();

//Control_Click((object)sender, (System.EventArgs)e);
}

public ControlMove(Control moveControl, NoteDoc noteDoc):this(moveControl)
{
this._noteDoc = noteDoc;
}
#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++)
{
_MControl.Parent.Controls.Remove(_lbl[i]);
}
_MControl.Parent.Controls.Remove(_MControl);
_textbox.Parent.Controls.Remove(_textbox);
}

if (e.KeyValue == 17)
{
_IsCtrlKey = true;
//MessageBox.Show("a");
}
}

#endregion

#region 需拖动控件鼠标事件

private void Control_Click(object sender, System.EventArgs e)
{
_textbox.Focus();
_MControl = (sender as Control);
MoveHandles();

//((Panel)this._MControl.Parent.Parent).ScrollControlIntoView(_MControl);

if (_IsCtrlKey == false)
{
for (int i = 0; i < _MControl.Parent.Controls.Count; i++)
{
if ((_MControl.Parent.Controls[i].Text == null || _MControl.Parent.Controls[i].Text.Trim().Length == 0) && _MControl.Parent.Controls[i] is Label && _MControl.Parent.Controls[i].GetType().ToString().IndexOf("Sinxsoft.NotePrinter.Controls") < 0 )
{
_MControl.Parent.Controls[i].Visible = false;
}
}
}
this._noteDoc.Active_Control(this._MControl);
}

private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
_IsMouseDown = true;

_oPointClicked = new Point(e.X, e.Y);

for (int i = 0; i < 8; i++)
{
_MControl.Parent.Controls.Add(_lbl[i]);
_lbl[i].BringToFront();
}
HideHandles();
//this._MControl.Parent.Text = "x:" + e.X.ToString() + " y:" + e.Y.ToString();

}

liujiwe79 2007-04-06
  • 打赏
  • 举报
回复
我一会贴点代码
liujiwe79 2007-04-06
  • 打赏
  • 举报
回复
tongxudong() ( ) 你着急什么,我一会就写点这方面的简单代码,你真是的,这你也管,搂住还没意见呢
storm0912 2007-04-06
  • 打赏
  • 举报
回复
楼主是想做浮动控件吧?这个确实有点难度哦。多看看MSDN和GDI+方面的,应该能够解决
mabaolin 2007-04-06
  • 打赏
  • 举报
回复
哈哈,罐水....
tongxudong 2007-04-06
  • 打赏
  • 举报
回复
liujiwe79(专业做控件) ,你还真是无聊诶~,要么就给人家楼主提供些有建设性的意见和启发,要么就帮忙顶一下,只会唠叨不难,不难,我替楼主鄙视你一下
liujiwe79 2007-04-06
  • 打赏
  • 举报
回复
这个其实也不难,就相当于自己用.net再做一套开发工具而已,不难的,你自己也可以通过事件去处理呀
hertcloud 2007-04-06
  • 打赏
  • 举报
回复
这个 GZ下哦
有难度 感觉要自己写GDI+了
Red_angelX 2007-04-06
  • 打赏
  • 举报
回复
观望...
暗石绿 2007-04-06
  • 打赏
  • 举报
回复

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧