首页 > 其他分享 >WPF RenderTransform TransformGroup ScaleTransform TranslateTransform

WPF RenderTransform TransformGroup ScaleTransform TranslateTransform

时间:2024-06-02 22:10:41浏览次数:19  
标签:scaler TranslateTransform void System private ScaleTransform Windows using WPF

<Window x: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.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp132"
        mc:Ignorable="d"  
        WindowState="Maximized"
        MouseDown="Window_MouseDown" 
        MouseMove="Window_MouseMove"
        MouseWheel="Window_MouseWheel" 
        MouseUp="Window_MouseUp"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListBox x:Name="lbx"  Grid.Column="0" 
                 ItemsSource="{Binding ImgsList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                 SelectionChanged="lbx_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding Content,RelativeSource={RelativeSource AncestorType=ListBoxItem}}" 
                           Width="200" Height="500" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox> 
        <ContentControl Grid.Column="1" ClipToBounds="True">
            <Viewbox x:Name="viewBox" 
                     HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch" >
                <Image x:Name="img" Source="{Binding SelectedItem,ElementName=lbx}" 
                       HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform x:Name="scaler"/>
                            <TranslateTransform x:Name="translater"/>
                        </TransformGroup>
                    </Image.RenderTransform>
                </Image>
            </Viewbox>
        </ContentControl>      
    </Grid>
</Window>


//.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
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.IO;
namespace WpfApp132
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            InitImgsList();
            this.DataContext = this;
        }

        private void InitImgsList()
        {
            string fullDir = System.IO.Path.GetFullPath(@"..\..\Images");
            if (Directory.Exists(fullDir))
            {
                var files = System.IO.Directory.GetFiles(fullDir, "*", System.IO.SearchOption.AllDirectories);
                ImgsList = new ObservableCollection<string>(files);
                lbx.SelectedIndex = 0;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ObservableCollection<string> imgsList;
        public ObservableCollection<string> ImgsList
        {
            get
            {
                return imgsList;
            }
            set
            {
                if (value != imgsList)
                {
                    imgsList = value;
                    OnPropertyChanged(nameof(ImgsList));
                }
            }
        }

        private bool isMoving = false;
        public bool IsMoving
        {
            get
            {
                return isMoving;
            }
            set
            {
                if (value != isMoving)
                {
                    isMoving = value;
                    OnPropertyChanged(nameof(IsMoving));
                }
            }
        }
         

        Point previousPoint;
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            previousPoint = e.GetPosition(this);
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            IsMoving = true;
        }

        private void Window_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;
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (IsMoving && e.ButtonState == MouseButtonState.Released && e.ChangedButton == MouseButton.Left)
            {
                Point newPoint = e.GetPosition(this);
                double deltaX = newPoint.X - previousPoint.X;
                double deltaY = newPoint.Y - previousPoint.Y;
                translater.X = deltaX;
                translater.Y = deltaY;
            }
        }

        private void lbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            scaler.ScaleX = 1.0;
            scaler.ScaleY = 1.0;
            img.HorizontalAlignment= HorizontalAlignment.Stretch;
            img.VerticalAlignment= VerticalAlignment.Stretch;
        }
    }
}

 

 

标签:scaler,TranslateTransform,void,System,private,ScaleTransform,Windows,using,WPF
From: https://www.cnblogs.com/Fred1987/p/18227715

相关文章

  • Wpf 使用 Prism 开发MyToDo应用程序
    MyToDo是使用WPF,并且塔配Prism框架进行开发的项目。项目中进行了前后端分离设计,客户端所有的数据均通过API接口获取。适合新手入门学习WPF以及Prism框架使用。首页统计以及点击导航到相关模块功能待办事项增删改查功能备忘录增删改查功能登录注册功能系统设备主题颜......
  • wpf 中阿里图标库的使用
    阿里图标库的使用阿里图标库iconfont1.进入阿里图标库主界面后,根据需要搜索自己要用的图标,然后加入到购物车中2.打开“资源管理-我的项目”,进入我的项目界面,然后点击"下载至本地",把资源文件下载到本地3.引用资源(通过编码"&#xefdc"和ttf字体文件实现效果)效果......
  • C#WPF数字大屏项目实战06--报警信息
    1、ItemsControl简介 ItemsControl是用来表示一些条目集合的控件,所以它叫条目控件,它的成员是一些其它控件的集合,其继承关系如下:     其常用的派生控件为:ListBox、ListView、ComboBox,为ItemsControl的具体实现。   ItemsControl的成员条目可以为不同的类型,如自......
  • WPF快速创建绑定属性
    快速添加一个绑定属性,例如privatedouble_CurrentFontSize=12;publicdoubleCurrentFontSize{get{return_CurrentFontSize;}set{_CurrentFontSize=value;OnPropertyChanged("Cu......
  • WPF MVVM如何在ViewModel直接操作控件对象
    早些年在WPF中使用COM组件时,需要在ViewModel中操作COM组件中的控件对象,但是这个控件对象又不支持绑定,后面的解决办法是在窗口加载时,将控件对象以参数传递到Loaded事件的处理命令中,然后将这个对象记录下来,后面就可以直接操作这个控件了。今天同事在使用WebView2的时候,又遇到这个......
  • .NET|--WPF|--如何使用LINQPad创建一个WPF示例
    1.安装包管理器#搜索框内需要填入↓"id=Microsoft.NETCore.App""id=Microsoft.WindowsDesktop.App.Ref"2.代码voidMain(){ varapp=newSystem.Windows.Application(); varmainWindow=newSystem.Windows.Window { Title="SimpleWPFProgra......
  • 原生WPF使用IOC容器
    1、删除App.xaml中的StartupUri=""2、改造App.xaml.cspublicpartialclassApp:Application{publicApp(){ServiceProvider=GetServiceProvider();}privateIServiceProviderGetServiceProvider(){ServiceC......
  • WPF使用事件聚合器,实现任意页面跨页通信
    前言:最近几天有好几个小伙伴玩WPF,遇到不同页面,不知道要怎么传递消息。于是,我今天就来演示一个事件聚合器的玩法,采用prism框架来实现。作为福利,内容附带了主页面打开对话框时候直接通过参数传递消息的一个小例子,具体请自行围观。以下内容,创建wpf项目以及引用prism和实现依赖注入......
  • WPF Image enlarge via MouseWheel, selected image center does not shift
    //xaml<Windowx:Class="WpfApp123.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • wpf 捕获全局异常
    App(){this.Startup+=App_Startup;}privatevoidApp_Startup(objectsender,StartupEventArgse){this.DispatcherUnhandledException+=App_DispatcherUnhandledException;AppDomain.CurrentDomain.UnhandledException+=CurrentD......