首页 新闻 搜索 专区 学院

winform控件拖动时闪烁的问题

0
悬赏园豆:100 [已关闭问题] 关闭于 2011-06-14 09:40
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。

暗夜de孤独者的主页 暗夜de孤独者 | 初学一级 | 园豆:0
提问于:2011-05-26 13:48
< >
分享
所有回答(4)
0

我以前也是web的 winform都搞不懂哦

flybisu | 园豆:119 (初学一级) | 2011-05-26 15:42
0

窗体中有双向缓冲属性

jingshaq | 园豆:210 (菜鸟二级) | 2011-05-26 17:19
对的,是有,但是不好使啊,不知道咋弄
支持(0) 反对(0) 暗夜de孤独者 | 园豆:0 (初学一级) | 2011-05-26 17:21
0

用WPF吧

freewzx2005 | 园豆:8 (初学一级) | 2011-05-26 19:33
0

doublebuffer设置为true

.NET敏捷开发框架 | 园豆:916 (小虾三级) | 2011-06-02 12:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册