<Window x:Class="WpfApp408.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:WpfApp408"
mc:Ignorable="d"
WindowState="Maximized"
WindowStyle="None"
AllowsTransparency="True"
KeyDown="Window_KeyDown"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Image x:Name="img" Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
RenderOptions.BitmapScalingMode="Fant"
MouseWheel="Image_MouseWheel">
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="scaler"/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<TextBlock Text="{Binding SeqId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Width="200"
Foreground="Red"
FontSize="50"
Panel.ZIndex="2"/>
</Grid>
</Window>
//cs
using System;
using System.Collections.Generic;
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;
using System.Runtime.InteropServices;
using System.Data.SqlTypes;
namespace WpfApp408
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<string> imgsList { get; set; }
private int idx = 0;
public int Idx
{
get
{
return idx;
}
set
{
if(value!=idx)
{
idx = value;
OnPropertyChanged(nameof(Idx));
}
}
}
private int seqId = 0;
public int SeqId
{
get
{
return seqId;
}
set
{
if (value!=seqId)
{
seqId = value;
OnPropertyChanged(nameof(SeqId));
}
}
}
private int imgsCount = 0;
private bool isPaused = false;
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
imgsList = new List<string>(Directory.GetFiles(@"../../Images"));
imgsCount = imgsList.Count;
System.Timers.Timer tmr = new System.Timers.Timer();
tmr.Elapsed += Tmr_Elapsed;
tmr.Interval = 100;
tmr.Start();
}
private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if(isPaused)
{
return;
}
if (++Idx >= imgsCount)
{
Idx = 0;
}
ImgUrl = imgsList[Idx];
++SeqId;
Dispatcher.BeginInvoke(new Action(() =>
{
scaler.ScaleX = 1.0;
scaler.ScaleY = 1.0;
scaler.CenterX = 0;
scaler.CenterY = 0;
}));
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
private string imgUrl;
public string ImgUrl
{
get
{
return imgUrl;
}
set
{
if (value != imgUrl)
{
imgUrl = value;
OnPropertyChanged(nameof(ImgUrl));
}
}
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C)
{
var msgResult = MessageBox.Show("Are you sure to close this window?", "Close this Window",
MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
if (msgResult == MessageBoxResult.Yes)
{
this.Close();
}
}
else if(e.Key==Key.Space)
{
isPaused=!isPaused;
}
}
private void Image_MouseWheel(object sender, MouseWheelEventArgs e)
{
if(e.Delta>0)
{
scaler.ScaleX *= 1.2;
scaler.ScaleY *= 1.2;
}
else
{
scaler.ScaleX /= 1.2;
scaler.ScaleY /= 1.2;
}
scaler.CenterX = e.GetPosition(img).X;
scaler.CenterY = e.GetPosition(img).Y;
}
}
}
![]()
![]()