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

Categories

0 votes
1.2k 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 Reply

0 votes
by (71.8m points)

I believe my edit above is the best solution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...