write down,forget
  • adidaseqt
  • eqtturbored
  • eqtsupport9317
  • eqtsupport
  • 9317adidas
  • adidaseqtboost9317
  • eqtsupport93
  • 9317eqt
  • eqt support 9317 adv
  • support 9317 adv
  • eqtadv
  • eqt9317
  • eqtadv9317
  • support93
  • originalseqt
  • adidas eqt
  • eqt support 9317
  • eqt support
  • eqt adv
  • eqt 9317
  • WPF初学,控件定位、拖拽与画线

    <Category: WPF> 查看评论

    看图,控件可以在canvas里面自由布局,控件自由拖动(事件还有点问题,图方便,无视),贝塞尔曲线自动连接两个控件(参数可以调整),通过这个例子搞明白了原理,以后想怎么做都方便了,哈哈

    1
    2

    直接看代码吧:

    window1.xaml

    <Window x:Class=”WpfDragContrl.Window1″

    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

    Title=”Window1″ Height=”300″ Width=”300″>

    <Grid>

    <Canvas Name=”container”>

    <Canvas   Canvas.Top=”40″ Width=”30″ Height=”30″ Background=”Blue” x:Name=”c1″ MouseLeftButtonDown=”c_MouseLeftButtonDown” MouseMove=”c_MouseMove” MouseLeftButtonUp=”c_MouseUp” > </Canvas>

    <Canvas  Width=”30″ Height=”30″ Background=”Blue” x:Name=”c2″ MouseLeftButtonDown=”c_MouseLeftButtonDown” MouseMove=”c_MouseMove” MouseLeftButtonUp=”c_MouseUp” > </Canvas>

    </Canvas>

    </Grid>

    </Window>

    window1.xaml.cs
    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 WpfDragContrl
    {
     /// <summary>
     /// Interaction logic for Window1.xaml
     /// </summary>
     public partial class Window1 : Window
     {
     Path connector = new Path();
     private PathGeometry connectorGeometry;
     private PathFigure connectorPoints;
     private BezierSegment connectorCurve;
     public Window1()
     {
     InitializeComponent();
     
     connector.Stroke = Brushes.Red;
     connector.StrokeThickness = 3;
     connectorGeometry = new PathGeometry();
     connectorPoints = new PathFigure();
     connectorCurve = new BezierSegment();
     SetBezierLine();
     connectorPoints.Segments.Add(connectorCurve);
     connectorGeometry.Figures.Add(connectorPoints);
     connector.Data = connectorGeometry;
     container.Children.Add(connector);
     }
     private void SetBezierLine()
     {
     connectorPoints.StartPoint =new Point((double)c1.GetValue(Canvas.LeftProperty) + c1.Width / 2,(double)c1.GetValue(Canvas.TopProperty) + c1.Height / 2);
     var endpoint= new Point((double)c2.GetValue(Canvas.LeftProperty) + c2.Width / 2,(double)c2.GetValue(Canvas.TopProperty) + c2.Height / 2);
     connectorCurve.Point1 = new Point(50,50);
     connectorCurve.Point2 = new Point(80,58);
     connectorCurve.Point3 = endpoint;
     }
     private void c_MouseMove(object sender, MouseEventArgs e)
     {
     if(!isdrag) return;
     var control = (sender as UIElement);
    control.SetValue(Canvas.LeftProperty, e.GetPosition(container).X-control.DesiredSize.Width/2);
    control.SetValue(Canvas.TopProperty, e.GetPosition(container).Y-control.DesiredSize.Height/2);
    var vector= VisualTreeHelper.GetOffset(control);
    this.Title=vector.X+”/”+vector.Y;
    SetBezierLine();
    }
    private bool isdrag = false;
    private void c_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
    isdrag = true;
    }
    private void c_MouseUp(object sender, MouseButtonEventArgs e)
    {
    isdrag = false;
    }
    private void DrawBezierLine(UIElement element1,UIElement element2)
    {
    }
    }
    }
    <Window x:Class=”WpfDragContrl.Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
    Title=”Window1″ Height=”300″ Width=”300″>
    <Grid>
    <Canvas Name=”container”>
    <Canvas   Canvas.Top=”40″ Width=”30″ Height=”30″ Background=”Blue” x:Name=”c1″ MouseLeftButtonDown=”c_MouseLeftButtonDown” MouseMove=”c_MouseMove” MouseLeftButtonUp=”c_MouseUp” > </Canvas>
    <Canvas  Width=”30″ Height=”30″ Background=”Blue” x:Name=”c2″ MouseLeftButtonDown=”c_MouseLeftButtonDown” MouseMove=”c_MouseMove” MouseLeftButtonUp=”c_MouseUp” > </Canvas>
    </Canvas>
    </Grid>
    </Window>

    源码下载:WpfDragContrl

    本文来自: WPF初学,控件定位、拖拽与画线

    
    eqt support adidas eqt support 93 primeknit og colorway ba7506 adidas eqt running 93 updated with primeknit construction adidas eqt boost 93 17 white turbo red adidas eqt support 9317 white turbo red adidas eqt support 93 17 adidas eqt support 9317 adidas eqt support 9317 turbo red releases tomorrow adidas originals adidas eqt tactile green pack adidas eqt tactile green pack adidas eqt light green pack womens adidas eqt light green pack coming soon adidas eqt milled leather pack release date adidas originals eqt milled leather pack adidas eqt support ultra boost turbo red white adidas adv support burnt orange grey where to buy the adidas eqt support 9317 turbo red adidas eqt boost 91 16 turbo red adidas eqt support 93 turbo red adidas eqt support 9317 white turbo red available now