1. 程式人生 > 其它 >WPF draw circle equals with ellipse has same width and height,zoom

WPF draw circle equals with ellipse has same width and height,zoom

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local
="clr-namespace:WpfApp2" x:Name="mainWindow" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" WindowState="Maximized" > <Window.Resources> <Style x:Key="MyStyle" TargetType="{x:Type GridSplitter}"> <Setter Property="Width" Value="10"
/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GridSplitter}"> <Grid> <Button Content="⁞" /> <Rectangle
Fill="Red" /> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition/> <RowDefinition Height="30"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" > <Button Content="Clear" x:Name="clearBtn" Click="clearBtn_Click"/> <GridSplitter Width="10"></GridSplitter> <Button Content="Reset" x:Name="resetBtn" Click="resetBtn_Click"/> </StackPanel> <Grid x:Name="gd" Grid.Row="1"> <Grid.RenderTransform> <TranslateTransform x:Name="tt"/> </Grid.RenderTransform> <Canvas x:Name="cvs" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible"> </Canvas> </Grid> <StatusBar Grid.Row="2"> <StatusBarItem> <TextBlock Name="statusTB" FontSize="20"/> </StatusBarItem> </StatusBar> </Grid> </Window>
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 WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        double scale = 1.0;
        double delta = 0.5;
        double GlobalX=0, GlobalY=0;
        int level = 5, minLevel = 5, maxLevel = 1000;
        Point m_start;
        Vector m_startOffset;
        public MainWindow()
        {
            InitializeComponent();
            ShowStatus();
            InitEvents();
        }

        private void InitEvents()
        { 
            mainWindow.MouseWheel += MainWindow_MouseWheel;
            mainWindow.Loaded += MainWindow_Loaded;
            MouseMove += MainWindow_MouseMove;
            gd.MouseMove += Gd_MouseMove;
            gd.MouseDown += Gd_MouseDown;
            gd.MouseUp += Gd_MouseUp;
        }

        private void Gd_MouseUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as Grid;
            if(element!=null)
            {
                element.ReleaseMouseCapture();
                gd.ReleaseMouseCapture();
            } 
        }

        private void Gd_MouseDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as Grid;
            if(element!=null)
            {
                TranslateTransform translate = element.RenderTransform as TranslateTransform;
                m_start = e.GetPosition(gd);
                m_startOffset = new Vector(translate.X, translate.Y);
                element.CaptureMouse();
            } 
        }

        private void Gd_MouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as Grid;
            if(element!=null)
            {
                TranslateTransform translate = element.RenderTransform as TranslateTransform;

                if (element.IsMouseCaptured)
                {
                    Vector offset = Point.Subtract(e.GetPosition(gd), m_start);
                    translate.X = e.GetPosition(gd).X;
                    translate.Y = e.GetPosition(gd).Y;
                }
            } 
        }

        private void MainWindow_MouseMove(object sender, MouseEventArgs e)
        {
            var pt = e.GetPosition(gd);
            GlobalX = pt.X;
            GlobalY= pt.Y;
            ShowStatus();
            var elem = Mouse.DirectlyOver;
            if(elem!=null)
            {
                var ellipse = elem as Ellipse;
                if(ellipse!=null)
                {
                    ellipse.ToolTip = ellipse.Tag.ToString();
                }
            }
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            InitShapes();
        }

        private void MainWindow_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                if (level < maxLevel)
                {
                    level++;
                    ZoomIn();
                }
            }
            else
            {
                if (level > minLevel)
                {
                    level--;
                    ZoomOut();
                }
            }

            ShowStatus();
        }
         
        private void InitShapes()
        {
            Point pt = new Point(1, 1);
            int interval = 10;
            int widthLoops = (int)(this.ActualWidth / interval);
            System.Diagnostics.Debug.WriteLine(widthLoops); 
            int heightLoops = (int)(this.ActualHeight / interval);
            System.Diagnostics.Debug.WriteLine(heightLoops);
            int num = 0;
            for (int i = 0; i < widthLoops; i++)
            {
                for (int j = 0; j < heightLoops; j++)
                {
                    double px = pt.X + interval * i;
                    double py = pt.Y + interval * j;
                    if (px < this.ActualWidth && py < this.ActualHeight)
                    {
                        ++num;
                        Ellipse newEllipse = new Ellipse();
                        newEllipse.Visibility = Visibility.Visible;
                        newEllipse.Height = 5;
                        newEllipse.Width = 5;
                        newEllipse.SetValue(Canvas.LeftProperty, px);
                        newEllipse.SetValue(Canvas.TopProperty, py);
                        SolidColorBrush mySolidColorBrush = new SolidColorBrush();
                        mySolidColorBrush.Color = Color.FromArgb(255, 255, 0, 0);
                        newEllipse.Fill = mySolidColorBrush;
                        newEllipse.Stroke = Brushes.Black;
                        newEllipse.StrokeThickness = 1;
                        newEllipse.Tag = px + "," + py;
                        cvs.Children.Add(newEllipse);
                    }
                }
            }
        }

        private void ShowStatus()
        {
            Dispatcher.BeginInvoke(new Action(() =>
            { 
                statusTB.Text = "Level: " + level+",X:"+GlobalX+",Y:"+GlobalY;
            }));
        }

        private void ZoomOut()
        {
            scale -= delta;
            gd.LayoutTransform = new ScaleTransform(scale, scale);
        }

        private void clearBtn_Click(object sender, RoutedEventArgs e)
        {
            cvs.Children.Clear();
        }

        private void resetBtn_Click(object sender, RoutedEventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            scale = 1.0;
            level = 5;
            GlobalX = 0;
            GlobalY = 0;
            gd.LayoutTransform = new ScaleTransform(1.0, 1.0);
            InitShapes();
            ShowStatus();
        }

        private void ZoomIn()
        {
            scale += delta;
            gd.LayoutTransform = new ScaleTransform(scale, scale);
        } 
    }
}