Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code.
C#
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;
using System.Diagnostics;
namespace Visualexample
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public bool letsmove = false;
        
        DrawingVisual b = null;
        DrawingVisual selectedvisual = null;
        Size somesize = new Size(50,50);
        public MainWindow()
        {
            InitializeComponent();
            
            Tablet.Style = (Style)Grid1.FindResource("Canvasstyle");
            Tablet.MouseLeftButtonDown += drawmethod;
           

        }
        public void drawrectangle(DrawingVisual a,Point somepoint)
        {
           
            Pen somepen = new Pen(Brushes.Green,5);
            Brush somebrush = Brushes.Red;
            
            using(DrawingContext dc=a.RenderOpen())
            {
                dc.DrawRectangle(somebrush,somepen,new Rect(somepoint,somesize));
            }
        }
      
        private void drawmethod(object sender, MouseButtonEventArgs e)
        {
         
           
            Point clickedpoint = e.GetPosition(Tablet);
            if (RadADD.IsChecked == true)
            {
                b = new DrawingVisual();
                drawrectangle(b, clickedpoint);
                Tablet.Addvisual(b);
            }
            else if (Radrem.IsChecked == true)
            {
                b = Tablet.getvisual(ref Tablet, clickedpoint);
                if (b != null)
                {
                    Tablet.Removevisual(b);
                }
            }
            else if (Radsel.IsChecked == true)
            {
                b = Tablet.getvisual(ref Tablet, clickedpoint);
                
                letsmove = true;
            }
          
            
        }
        [DebuggerStepThrough]
        private void Move(object sender,MouseEventArgs e)
        {
            if (letsmove == true)
            {
                Point pointdragger = e.GetPosition(Tablet);
                if(b!=null)
                drawrectangle(b, pointdragger);
               
            }
           
        }
        private void MouseUP(object sender,MouseButtonEventArgs e)
        {
             letsmove=false;
        }
      
     
    }
    [DebuggerStepThrough]
    public class someconvass : Panel
    {
        List<Visual> thing = new List<Visual>();
        public DrawingVisual getvisual(ref someconvass k,Point p)
        {
            HitTestResult hitresult = VisualTreeHelper.HitTest(k, p);
            return hitresult.VisualHit as DrawingVisual;
        }
        public someconvass()
        {
            
   
        }
        
        public void Addvisual(Visual a)
        {
            AddVisualChild(a);
            AddLogicalChild(a);
            thing.Add(a);
        }
      
        public void Removevisual(Visual a)
        {
            RemoveVisualChild(a);
            RemoveLogicalChild(a);
            thing.Remove(a);
        }
        protected override int VisualChildrenCount
        {
            get
            {
                return thing.Count;
            }
        }
        protected override Visual GetVisualChild(int index)
        {
           
            return thing[index];
        }
       
    }
    
}

XML
<Window x:Class="Visualexample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="500" xmlns:local="clr-namespace:Visualexample" ResizeMode="NoResize"> 
<Window.Resources>
        <Style x:Key="Canvasstyle">
            <Setter Property="Canvas.Width" Value="362"/>
            <Setter Property="Canvas.Height" Value="500"/>
            <Setter Property="Canvas.Background" Value="White"/>
            <Setter Property="Canvas.HorizontalAlignment" Value="Right"/>
        </Style>
    </Window.Resources>
    <Grid Name="Grid1">
       
            <ToolBarTray Orientation="Vertical"  HorizontalAlignment="Left" Width="130">
        <ToolBar Width="130" HorizontalAlignment="Left" Margin="0,0,0,-476">
            <RadioButton Content="DrawRectangle" Name="RadADD"/>
        <RadioButton Margin="0,20,0,0" Content="RemoveRectangle" Name="Radrem"/>
        <RadioButton Margin="0,40,0,0" Content="Select/Move" Name="Radsel"/>
       
        </ToolBar>
        </ToolBarTray>
        <Border HorizontalAlignment="Right" Width="370" ToolTip="Hello" >
            <local:someconvass x:Name="Tablet" MouseLeftButtonDown="drawmethod"  MouseLeftButtonUp="MouseUP"  MouseMove="Move"/>
        </Border>
        
    </Grid>
    
</Window>

Explain me please how works MouseMove event handler.When i drag rectangle,there are creates new rectangle and drags to another place of Panel container.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900