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)
            {
    
            }
        }
    }
  • 相关阅读:
    《应用Yii1.1和PHP5进行敏捷Web开发》学习笔记(转)
    YII 小模块功能
    Netbeans代码配色主题大搜集
    opensuse 启动巨慢 解决方法 90s多
    opensuse 安装 网易云音乐 rpm netease music
    linux qq rpm deb opensuse
    openSUSE 安装 alien
    第一行代码 Android 第2版
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/2882462.html
Copyright © 2011-2022 走看看