首页 后端开发 C#.Net教程 C#实现点击窗体任意位置拖动

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

Nov 10, 2016 am 09:30 AM

基本思路很简单:

先得到鼠标点击的位置并记录,鼠标移动时得到移动后的位置计算出差值,然后平移。

代码:

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;  
            }  
        }  
    
    }  
}
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

c语言编译器怎么加下一格 c语言编译器怎么加下一格 Mar 03, 2025 pm 05:44 PM

c语言编译器怎么加下一格

C语言中NULL的替代方案有哪些 C语言中NULL的替代方案有哪些 Mar 03, 2025 pm 05:37 PM

C语言中NULL的替代方案有哪些

c语言编译器复制代码方法 c语言编译器复制代码方法 Mar 03, 2025 pm 05:43 PM

c语言编译器复制代码方法

c语言编译器哪个好? c语言编译器哪个好? Mar 03, 2025 pm 05:39 PM

c语言编译器哪个好?

c语言编译器网页版有哪些? c语言编译器网页版有哪些? Mar 03, 2025 pm 05:42 PM

c语言编译器网页版有哪些?

C语言中NULL在现代编程中还重要吗 C语言中NULL在现代编程中还重要吗 Mar 03, 2025 pm 05:35 PM

C语言中NULL在现代编程中还重要吗

c语言在线编程网站 c语言编译器官方网站汇总 c语言在线编程网站 c语言编译器官方网站汇总 Mar 03, 2025 pm 05:41 PM

c语言在线编程网站 c语言编译器官方网站汇总

c语言编译器安装教程(电脑版) c语言编译器安装教程(电脑版) Mar 03, 2025 pm 05:41 PM

c语言编译器安装教程(电脑版)

See all articles