C#实现点击窗体任意位置拖动

前端之家收集整理的这篇文章主要介绍了C#实现点击窗体任意位置拖动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Imaging;  
    using System.Windows.Navigation;  
    using System.Windows.Shapes;  
    namespace DragWindowTest  
    {  
        /// <summary>  
        /// MainWindow.xaml 的交互逻辑  
        /// </summary>  
        public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
            }  
            private static bool IsDrag = false;  
            private double enterX;  
            private double enterY;  
            private void Window_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)  
            {  
                IsDrag = true;  
                enterX = e.GetPosition(this).X;  
                enterY = e.GetPosition(this).Y;  
            }  
      
            private void Window_MouseLeftButtonUp(object sender,MouseButtonEventArgs e)  
            {  
                IsDrag = false;  
                enterX = 0;  
                enterY = 0;  
            }  
            private void Window_MouseMove(object sender,MouseEventArgs e)  
            {  
                if (IsDrag)  
                {  
                    this.Left += e.GetPosition(this).X - enterX;  
                    this.Top += e.GetPosition(this).Y - enterY;  
                }  
            }  
       
        }  
    }  

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

猜你在找的C#相关文章