Difference between MouseClick and DoubleClick

后端 未结 4 1490
傲寒
傲寒 2021-01-15 13:54

WPF, Shapes

I have a custom Shape, and try to implement MouseClick and MouseDoubleClick stuff on

相关标签:
4条回答
  • 2021-01-15 13:55

    As I said in a comment, I once had this very same problem. I needed to detect double clicks, but clicks and double clicks should behave as separate events. Meaning, if a double click was fired, click should not fire (because each action had different and not compatible effects in the UI).

    The "native" double click didn't work for me because click events fired whether a double click happened or not. Here's some sample code in Actionscript. I'm sure you'll be able to follow the (simple) logic to implement it in .net. Note that I'm hardcoding the double click time, as I couldn't get the system value for it from Actionscript. It's not ideal, but it's I compromise I had to make. You should use the time interval provided by the OS (or your runtime) instead, if it's available to you.

    Hope it helps.

    package {
        import flash.events.*;
        import flash.display.*;
        import flash.utils.*;
    
        public class Button extends Sprite {
    
            public function DCButton() {
                super();
                graphics.beginFill(0xff0000);
                graphics.drawRect(0,0,100,100);
                graphics.endFill();
                addEventListener(MouseEvent.CLICK,handleClick);
            }
    
            private var _clickCount:int = 0;
            private var _doubleClickInterval:int = 200;
    
            private function handleClick(e:MouseEvent):void {
                _clickCount++;
                //  start the timer and when it completes, check the number of clicks
                //  based on this, dispatch either click or doubleClick events
                if(_clickCount == 1) {
                    setTimeout(checkDoubleClick,_doubleClickInterval,e);
                }
                else
                {
                    checkDoubleClick(e);
                }
    
            }   
    
            private function checkDoubleClick(e:MouseEvent):void {
    
                if(_clickCount == 0) return;
    
                var type:String;
    
                if(_clickCount == 1) {
                    type="click";
                } else if(_clickCount > 1) {
                    type="doubleClick";
                }
    
                trace(type);
    
                _clickCount = 0;
                //  dispatch the detected event here...
    
            }           
        }
    
    
    }
    
    0 讨论(0)
  • 2021-01-15 13:57

    Between first click and second click in double click I think there is a time around 50 and 400 millisecond, So the bellow code is not a best one but do the trick:

    bool firstClicked = false;
    
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.ClickCount == 1)
            {
                firstClicked = true;
                Task tsk = new TaskFactory()
                                .StartNew(
                                () => 
                                    {
                                        Thread.Sleep(SystemInformation.DoubleClickTime);
                                        if (firstClicked)
                                        {
                                            // if you want to use `e`
                                            // e.Source
                                            // Open Informational Window
                                        }
                                            firstClicked = false;
                                        }
                                            );
    
                                    }
    
            }
            else if (e.ClickCount == 2)
            {
                firstClicked = false;
                // Select Item
            }
        }
    
    0 讨论(0)
  • 2021-01-15 14:02

    Every DoubleClick starts with a Click.

    0 讨论(0)
  • 2021-01-15 14:21

    maybe try an attached behaviour?

    Try this link for an explanation

    Maybe you can aggregate the events...i think that is what you are trying to do, right?

    0 讨论(0)
提交回复
热议问题