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)
            {
    
            }
        }
    }
  • 相关阅读:
    (winform cookie)如何向某网址Post信息,并得到CookieContainer以便以后直接通过验证
    NBear官方MSN群
    NBear 支持基于操作符重载的强类型Where及OrderBy查询
    NBear视频 0.1 NBear类库结构及功能简介[发布时间:9/5]
    NBear 使用Entity Configurator设置实体元数据、生成数据库创建脚本
    ORM中的继承关系映射全解——单表继承体系、一实体一具体表、一实体一扩展表、接口映射
    实例解析继承体系重构及ORM映射
    NBearV2.1.0,新增Oracle和MySql Db Provider,诚征测试
    NBear视频 1.1 实体定义基础[发布时间:9/5]
    使用主动实体(ActiveEntity)简化继承体系映射类的写操作
  • 原文地址:https://www.cnblogs.com/wangjixianyun/p/2882462.html
Copyright © 2011-2022 走看看