WPF 鼠标拖拽平移

WPF 鼠标拖拽平移

码农世界 2024-05-23 前端 69 次浏览 0个评论

 

效果

xaml


            
                

 代码

 Point initialMousePoint = new();
        double horizontalOffset = 1;
        bool isMouseDown = false;
        private double mouseDist = 0;
        private void ScrollViewer_OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { 
            initialMousePoint = e.GetPosition(scrollViewer);
            horizontalOffset = scrollViewer.HorizontalOffset;
            isMouseDown = true;
            e.Handled = false;
        }
        private void ScrollViewer_OnPreviewMouseMove(object sender, MouseEventArgs e) {
            if (isMouseDown && e.LeftButton == MouseButtonState.Pressed) { 
                scrollViewer.ScrollToHorizontalOffset(horizontalOffset + (initialMousePoint.X - e.GetPosition(scrollViewer).X)); 
            }
            e.Handled = false;
        }
        private void ScrollViewer_OnPreviewMouseUp(object sender, MouseButtonEventArgs e) {
            isMouseDown = false;
            e.Handled = false; 
            mouseDist = (e.GetPosition(scrollViewer) - initialMousePoint).Length; 
        }
        private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { 
            if (mouseDist < 1) { 
                if (sender is Button btn) {
                    btn.Content = DateTime.Now.Millisecond.ToString();
                }
            }
        }

转载请注明来自码农世界,本文标题:《WPF 鼠标拖拽平移》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,69人围观)参与讨论

还没有评论,来说两句吧...

Top