Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

.net - C# WPF - DragMove and click

I need a control to call DragMove() for a Window on MouseLeftButton down, but still function when clicked.

If DragMove() is called, Click and MouseLeftButtonUp are not ever fired because DragMove() is a blocking call until they release the mouse button.

Does anyone know a workaround to make this work?

I have tried this hack based on Thread.Sleep which allows a click to work if it's quicker than 100 milliseconds, but it does not work reliably for users:

                ThreadPool.QueueUserWorkItem(_ =>
                    {
                        Thread.Sleep(100);

                        Dispatcher.BeginInvoke((Action)
                            delegate
                            {
                                if (Mouse.LeftButton == MouseButtonState.Pressed)
                                {
                                    window.DragMove();
                                }
                            });
                    });

EDIT: Well this hack worked...

                window.DragMove();
                RaiseEvent(new MouseButtonEventArgs(e.MouseDevice, e.Timestamp, MouseButton.Left) 
                    { 
                        RoutedEvent = MouseLeftButtonUpEvent 
                    });

Anyone have a better one?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe my edit above is the best solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...