首页 > 其他分享 >WPF canvas mousewheel to zoom in or out

WPF canvas mousewheel to zoom in or out

时间:2024-06-03 20:23:27浏览次数:27  
标签:mousewheel scaler Windows void System using WPF elp out

<Window x:Class="WpfApp133.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:WpfApp133"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Canvas x:Name="cvs" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                MouseWheel="cvs_MouseWheel"  
                MouseDown="cvs_MouseDown" 
                MouseMove="cvs_MouseMove"
                MouseUp="cvs_MouseUp">
            <Viewbox> 
            <Image Source="\Images\cl1.jpg" Stretch="UniformToFill" />
            </Viewbox>
            <Canvas.RenderTransform>
                <TransformGroup>
                    <ScaleTransform x:Name="scaler"/>
                    <TranslateTransform x:Name="translater"/>
                </TransformGroup> 
            </Canvas.RenderTransform>
        </Canvas>
    </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 WpfApp133
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
            DrawEllipse();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            int width = (int)this.ActualWidth;
            int height = (int)this.ActualHeight;
            for (int i = 0; i < width; i += 100)
            {
                for (int j = 0; j < height; j += 100)
                {
                    Ellipse elp = new Ellipse();
                    elp.Width = 10;
                    elp.Height = 10;
                    elp.Fill = new SolidColorBrush(Colors.Red);
                    Canvas.SetLeft(elp, i);
                    Canvas.SetTop(elp, j);
                    cvs.Children.Add(elp);
                }
            }
        }

        private void DrawEllipse()
        {

        }

        private void cvs_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            Point pt = e.GetPosition(this);
            if (e.Delta > 0)
            {
                scaler.ScaleX *= 1.2;
                scaler.ScaleY *= 1.2;
            }
            else
            {
                scaler.ScaleX /= 1.2;
                scaler.ScaleY /= 1.2;
            }
            scaler.CenterX = pt.X;
            scaler.CenterY = pt.Y;
        }

        Point previousPt;
        bool isMoving = false;
        private void cvs_MouseDown(object sender, MouseButtonEventArgs e)
        {
            previousPt = e.GetPosition(this);
        }

        private void cvs_MouseMove(object sender, MouseEventArgs e)
        {
            isMoving= true;
        }

        private void cvs_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if(isMoving && e.ChangedButton==MouseButton.Left && e.ButtonState==MouseButtonState.Released)
            {
                Point newPt=e.GetPosition(this);
                double deltaX=newPt.X - previousPt.X;
                double deltaY = newPt.Y - previousPt.Y;
                translater.X += deltaX;
                translater.Y += deltaY;
                isMoving= false;
            }
        }
    }
}

 

标签:mousewheel,scaler,Windows,void,System,using,WPF,elp,out
From: https://www.cnblogs.com/Fred1987/p/18229547

相关文章

  • WPF draw ellipse as array or matrix
    <Windowx:Class="WpfApp133.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • WPF DataGrid自动增长序号列
    ///<summary>///自动增长序号列///</summary>publicclassDataGridRowIndexColumn:DataGridTextColumn{///<summary>///可以指定开始序号///</summary>publicintStartIndex{get{return(int)GetValue(StartIndex......
  • vs中怎么设置统一的output路径
    背景:1、一个sln下有多个csproj项目,让所有csproj生成的dll路径在sln根目录下的ouput文件夹解决办法:1、在sln目录下新建Directory.Build.props文件,文件内容如下:<Project><PropertyGroupCondition="'$(Configuration)'=='Debug'"><CustomOutputPath&g......
  • How about a push-type floor scrubber
    Thehand-pushfloorscrubberisanelectriccleaningequipmentsuitablefordifferentfloormaterialsanddifferentcleaningplaces.Whilecleaningthefloor,itsucksupthesewageandtakesthesewageawayfromthesite.Theworkingprincipleissimple,......
  • React路由 基础:react-router-dom
    这里简单介绍一下react路由基础的知识,以及基础操作一,安装命令npminstallreact-router-dom二,路由模式1.ReactRouter支持两种路由模式:BrowserRouter和HashHistory。2.BrowserRouter使用URL中的/来定义路由,例如:http://xxx.com/Search3.HashHistory使用URL中的#来定义路由,例如......
  • Java中java.awt.FlowLayout的常量字段值
    流布局用于安排有向流中的组件,这非常类似于段落中的文本行。流的方向取决于容器的componentOrientation属性,它可能是以下两个值中的一个:ComponentOrientation.LEFT_TO_RIGHTComponentOrientation.RIGHT_TO_LEFT流布局一般用来安排面板中的按钮。它使得按钮呈水平放置,直到同一......
  • 文献阅读——Single Clause Assumption without Activation Literals to Speed-up IC
    SingleClauseAssumptionwithoutActivationLiteralstoSpeed-upIC3 @inproceedings{DBLP:conf/fmcad/FroleyksB21,author={NilsFroleyksandArminBiere},title={SingleClauseAssumptionwithoutActivationLitera......
  • WPF RenderTransform TransformGroup ScaleTransform TranslateTransform
    <Windowx:Class="WpfApp132.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • posterlayout:A new benchmark and approach for content-aware visual-textual presen
    1.introductionPKUPosterLayout包括9974对海报布局和905张图像,DSF设计序列生成算法,一种基于CNN-LSTM的生成对抗网络(DS-GAN),受到图像的条件制约,学习设计序列的分布,从而生成具有内容感知的视觉-文本展示布局。2.RelatedworkLayoutGAN,LayoutGAN++,CGL-GAN。3.ANewBenchmark:......
  • Wpf 使用 Prism 开发MyToDo应用程序
    MyToDo是使用WPF,并且塔配Prism框架进行开发的项目。项目中进行了前后端分离设计,客户端所有的数据均通过API接口获取。适合新手入门学习WPF以及Prism框架使用。首页统计以及点击导航到相关模块功能待办事项增删改查功能备忘录增删改查功能登录注册功能系统设备主题颜......