C#實現點擊窗體任意位置拖曳

大家讲道理
發布: 2016-11-10 09:30:07
原創
1853 人瀏覽過

基本想法很簡單:

先得到滑鼠點擊的位置並記錄,滑鼠移動時得到移動後的位置計算出差值,然後平移。

代碼:

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;  
            }  
        }  
    
    }  
}
登入後複製
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!