zoukankan      html  css  js  c++  java
  • wpf拖拽封装类

    <UserControl x:Class="WpfApplication1.DragItem"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" MouseDown="UserControl_MouseDown_1">
        <Grid Background="#FF191919" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Label  Content="项目文字" Foreground="White" FontSize="16" Width="450" Height="36" VerticalContentAlignment="Center" BorderThickness="2" BorderBrush="#FF424242"/>
        </Grid>
    </UserControl>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    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 WpfApplication1
    {
        /// <summary>
        /// DragItem.xaml 的交互逻辑
        /// </summary>
        public partial class DragItem : UserControl
        {
            public static Point dragDownMousePoint;
            public static Point dragDownControlPoint;
    
            public DragItem()
            {
                InitializeComponent();
            }
    
            public DragItem(string content)
            {
                InitializeComponent();
                this.Content.Content = content;   
            }
    
            private void mouseMove(object sender, MouseEventArgs e)
            {
                Window w = Window.GetWindow(this);
                Point p = e.GetPosition(w);
    
                double moveX = p.X - dragDownMousePoint.X;
                double moveY = p.Y - dragDownMousePoint.Y;
    
                Point targetPoint = new Point(dragDownControlPoint.X + moveX, dragDownControlPoint.Y + moveY);
                if (double.IsNaN(dragDownControlPoint.X))
                {
                    targetPoint.X = moveX;
                }
                if (double.IsNaN(dragDownControlPoint.Y))
                {
                    targetPoint.Y = moveY;
                }
    
                Canvas parent = (Canvas)this.Parent;
    
                if (targetPoint.X < 0)
                {
                    Canvas.SetLeft(this, 0);
                }
                else if (targetPoint.X + this.ActualWidth > parent.ActualWidth)
                {
                    Canvas.SetLeft(this, parent.ActualWidth - this.ActualWidth);
                }
                else
                {
                    Canvas.SetLeft(this, targetPoint.X);
                }
    
                if (targetPoint.Y< 0)
                {
                    Canvas.SetTop(this, 0);
                }
                else if (targetPoint.Y + this.ActualHeight > parent.ActualHeight)
                {
                    Canvas.SetTop(this, parent.ActualHeight - this.ActualHeight);
                }
                else
                {
                    Canvas.SetTop(this, targetPoint.Y);
                }
                Console.WriteLine(targetPoint.X + this.ActualWidth + "    " + parent.ActualWidth);
            }
    
            private void mouseUp(object sender, MouseEventArgs e)
            {
                Mouse.RemoveMouseMoveHandler(Window.GetWindow(this), mouseMove);
                Mouse.RemoveMouseUpHandler(Window.GetWindow(this), mouseUp);
                Mouse.Capture(null);
            }
    
            private void UserControl_MouseDown_1(object sender, MouseButtonEventArgs e)
            {
                Point p = e.GetPosition(Window.GetWindow(this));
                dragDownMousePoint = p;
                dragDownControlPoint = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
    
                Mouse.AddMouseMoveHandler(Window.GetWindow(this), mouseMove);
                Mouse.AddMouseUpHandler(Window.GetWindow(this), mouseUp);
                Mouse.Capture(this);
            }
    
            private void Label_Loaded_1(object sender, RoutedEventArgs e)
            {
    
            }
        }
    }
  • 相关阅读:
    sklearn的preprocessing模块--数据预处理
    [系列转载]一.建模前的数据清洗与处理
    2.2 数学科学的数学之矩阵-行列式
    4)函数极限与连续函数
    6)导数
    java编写基于netty的RPC框架
    购买阿里云 实现内网 穿透 仅86元/年,而且
    OAuth 2.0
    java中JVM内存管理(1)
    java实现,使用opencv合成全景图,前端使用krpano展示
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/2882462.html
Copyright © 2011-2022 走看看