public frmSetKuaiDi()
{
InitializeComponent();
cini.Path = System.Environment.CurrentDirectory + @"\PrintKuaiDi.ini";
this.DoubleBuffered = true;//设置本窗体
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
}
/// <summary>
/// 订阅事件
/// </summary>
private void initProperty()
{
for (int i = 0; i < this.panel1.Controls.Count; i++)
{
this.panel1.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
this.panel1.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave);
this.panel1.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
}
}
/// <summary>
/// 定义鼠标按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
p.X = e.X;
p.Y = e.Y;
p1.X = e.X;
p1.Y = e.Y;
}
/// <summary>
/// 定义鼠标离开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyMouseLeave(object sender, System.EventArgs e)
{
m_MousePointPosition = EnumMousePointPosition.MouseSizeNone; this.Cursor = Cursors.Arrow;
}
/// <summary>
/// 定义鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Control lCtrl = (sender as Control);
if (e.Button == MouseButtons.Left)
{
switch (m_MousePointPosition)
{
case EnumMousePointPosition.MouseDrag:
lCtrl.Left = lCtrl.Left + e.X - p.X;
lCtrl.Top = lCtrl.Top + e.Y - p.Y;
lCtrl.Height = lCtrl.Height + e.Y - p.Y;
lCtrl.Width = lCtrl.Width + e.X - p1.X;
break;
case EnumMousePointPosition.MouseSizeBottom:
lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeBottomRight:
lCtrl.Width = lCtrl.Width + e.X - p1.X;
lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeRight:
lCtrl.Width = lCtrl.Width + e.X - p1.X;
lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTop:
lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
break;
case EnumMousePointPosition.MouseSizeLeft:
lCtrl.Left = lCtrl.Left + e.X - p.X;
lCtrl.Width = lCtrl.Width - (e.X - p.X);
break;
case EnumMousePointPosition.MouseSizeBottomLeft:
lCtrl.Left = lCtrl.Left + e.X - p.X;
lCtrl.Width = lCtrl.Width - (e.X - p.X);
lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTopRight:
lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
lCtrl.Width = lCtrl.Width + (e.X - p1.X);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
p1.X = e.X;
p1.Y = e.Y; //'记录光标拖动的当前点
break;
case EnumMousePointPosition.MouseSizeTopLeft:
lCtrl.Left = lCtrl.Left + e.X - p.X;
lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
lCtrl.Width = lCtrl.Width - (e.X - p.X);
lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
break;
default:
break;
}
if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
}
else
{
m_MousePointPosition = MoveUtil.MousePointPosition(lCtrl.Size, Band, e); //'判断光标的位置状态
switch (m_MousePointPosition) //'改变光标
{
case EnumMousePointPosition.MouseSizeNone:
this.Cursor = Cursors.Arrow; //'箭头
break;
case EnumMousePointPosition.MouseDrag:
this.Cursor = Cursors.SizeAll; //'四方向
break;
case EnumMousePointPosition.MouseSizeBottom:
this.Cursor = Cursors.SizeNS; //'南北
break;
case EnumMousePointPosition.MouseSizeTop:
this.Cursor = Cursors.SizeNS; //'南北
break;
case EnumMousePointPosition.MouseSizeLeft:
this.Cursor = Cursors.SizeWE; //'东西
break;
case EnumMousePointPosition.MouseSizeRight:
this.Cursor = Cursors.SizeWE; //'东西
break;
case EnumMousePointPosition.MouseSizeBottomLeft:
this.Cursor = Cursors.SizeNESW; //'东北到南西
break;
case EnumMousePointPosition.MouseSizeBottomRight:
this.Cursor = Cursors.SizeNWSE; //'东南到西北
break;
case EnumMousePointPosition.MouseSizeTopLeft:
this.Cursor = Cursors.SizeNWSE; //'东南到西北
break;
case EnumMousePointPosition.MouseSizeTopRight:
this.Cursor = Cursors.SizeNESW; //'东北到南西
break;
default:
break;
}
}
}
拖动控件的时候闪的厉害啊,网上说要用双缓冲和窗体重绘什么的,也不太懂啊,一些代码我也用了,也不怎么起作用,有知道的大侠请帮下忙,看一下,谢谢了。最好能有代码,再告诉下代码放在哪,以前都是做web的,没怎么接触winform。
我以前也是web的 winform都搞不懂哦
窗体中有双向缓冲属性
用WPF吧
doublebuffer设置为true