急问:如何同时捕捉鼠标按下和双击事件?

peniy 2004-09-10 11:03:21
我要在鼠标按下(非双击)时移动一个label。双击时在两个label间加一条直线。

因为按下鼠标后没有人知道将会不会双击只能先设置一个isFirst=true,起一个Timer设为SystemInformation.DoubleClickTime时间后检查这个变量值。代码如下:

private void labelLarge_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{ if( e.Button == MouseButtons.Left && e.Clicks == 1)
{
isFirst = true;
System.Threading.TimerCallback timerDelegate =
new System.Threading.TimerCallback(doLabelLargeMouseDown);
object state = new object[] {sender,e};
tmr = new System.Threading.Timer(timerDelegate,state,
SystemInformation.DoubleClickTime, 0);
}
else if(e.Button == MouseButtons.Left && e.Clicks == 2)
{
isFirst = false;
}
base.OnMouseDown(e);
}


private void doLabelLargeMouseDown(Object state)
{

object[] obj = (object[])state;
MouseEventArgs e = (MouseEventArgs)obj[1];
Object sender = obj[0];
if(isFirst)
{
isFirst = false;
Label organLabel = (Label)sender;

Point offset = new Point(e.X,e.Y);
action = new MoveOrgan(organLabel,offset);
CanvasPanel.Cursor = new Cursor(GetType(), "DRAGMOVE.CUR");


this.startDrag = true;
Console.WriteLine("capture={0}",CanvasPanel.Capture.ToString());
CanvasPanel.Capture = true;//这里不行。
Console.WriteLine("capture={0}",CanvasPanel.Capture.ToString());
isDbClicked = false;
}
else
{
isFirst = false;
if( action is WorkFlowLine)
{
Console.WriteLine("second");
isDbClicked = false;
action.DoAction(this,sender,null);

}
else
{

Console.WriteLine("first");
isDbClicked = true;
Label startLabel = (Label)sender;
action = new WorkFlowLine(startLabel,true);

}
}
}


代码本身可以达到区分按下,与双击的事件。但是,CanvasPanel.Capture = true。前后却总是false.没办法变为true.这是因为线程的原因吗? 怎么处理。
...全文
220 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnlovezxm 2004-09-10
  • 打赏
  • 举报
回复
labelLarge_MouseDown
----------------------------
不要写在MouseDown里面,根据你的需求,可以写在 click 事件里面
click表示单击,DoubleClick是双击,要是你想判断鼠标拖动,可以写在move事件中!
yuchangle 2004-09-10
  • 打赏
  • 举报
回复
我是新手,我想问一下 System.Windows.Forms.MouseEventArgs
为什么我的System.下找不到Windows啊?需要什么using引用吗?好心人顺便告诉我一下,十分感谢~
jimh 2004-09-10
  • 打赏
  • 举报
回复
因为System.Threading.TimerCallback是线程,所以不能对界面做太多的处理,很不幸,CanvasPanel.Capture = true就不幸,
可以改为使用System.Windows.Forms.Timer控件吧,肯定可以的。
wolve 2004-09-10
  • 打赏
  • 举报
回复
没搞懂你的需求.
realljx 2004-09-10
  • 打赏
  • 举报
回复
楼主 建议你 改在 mousemove时间里面 写 检查是不是 鼠标按下状态.
eTopFancy 2004-09-10
  • 打赏
  • 举报
回复
再具体说一下你的详细需求,多人都不明白
peniy 2004-09-10
  • 打赏
  • 举报
回复
谢谢,确实应该写在move事件中,多谢各位。

110,535

社区成员

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

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

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