Skip to content

Commit 0a2aea3

Browse files
committed
改进缩放
1 parent d1df1bd commit 0a2aea3

13 files changed

+301
-138
lines changed

FileExplorer/AppBootstrapper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Microsoft.Practices.Prism.MefExtensions;
2-
using Microsoft.Practices.Prism.Modularity;
32
using Microsoft.Practices.ServiceLocation;
43
using System.ComponentModel.Composition.Hosting;
54
using System.Windows;
@@ -22,11 +21,17 @@ protected override void ConfigureAggregateCatalog()
2221
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
2322
}
2423

24+
protected override CompositionContainer CreateContainer()
25+
{
26+
return new CompositionContainer(AggregateCatalog);
27+
}
28+
2529
protected override void InitializeShell()
2630
{
2731
base.InitializeShell();
32+
2833
Application.Current.MainWindow = (Window)this.Shell;
29-
Application.Current.MainWindow.Show();
34+
Application.Current.MainWindow.Show();
3035
}
3136
}
3237
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace FileExplorer.Converter
6+
{
7+
class BoolToThicknessConverter : IValueConverter
8+
{
9+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
int result = 0;
12+
bool isTrue = false;
13+
if (value == null)
14+
{
15+
return result;
16+
}
17+
Boolean.TryParse(value.ToString(), out isTrue);
18+
if (!isTrue && parameter != null)
19+
{
20+
int.TryParse(parameter.ToString(), out result);
21+
}
22+
return result;
23+
}
24+
25+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
26+
{
27+
throw new NotImplementedException();
28+
}
29+
}
30+
}

FileExplorer/FileExplorer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
</Compile>
130130
<Compile Include="Converter\AndConverter.cs" />
131131
<Compile Include="Converter\BooleanReserveConverter.cs" />
132+
<Compile Include="Converter\BoolToThicknessConverter.cs" />
132133
<Compile Include="Converter\CompositeCoverter.cs" />
133134
<Compile Include="Converter\EmpytToVisibilityConverter.cs" />
134135
<Compile Include="Converter\OrConverter.cs" />

FileExplorer/ShellView.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using FileExplorer.Helper;
22
using FileExplorer.ViewModel;
3+
using FileExplorer.ViewTest;
34
using Microsoft.Practices.Prism.Modularity;
45
using Microsoft.Practices.Prism.Regions;
56
using Microsoft.Practices.ServiceLocation;
@@ -20,7 +21,6 @@ public partial class ShellView : Window
2021
{
2122
[Import]
2223
public Lazy<CategoryGroupViewModel> ViewModel { get; set; }
23-
2424
public ShellView()
2525
{
2626
InitializeComponent();

FileExplorer/Style/Converters.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
</local:CompositeCoverter>
1010

1111
<local:EmpytToVisibilityConverter x:Key="empytToVisibilityConverter"/>
12+
<local:BoolToThicknessConverter x:Key="boolToThicknessConverter"/>
1213

1314
</ResourceDictionary>

FileExplorer/ViewTest/ImageItem.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Windows.Media.Imaging;
55
using System.IO;
66
using System;
7+
using System.ComponentModel.Composition;
78

89
namespace FileExplorer.ViewTest
910
{
@@ -14,13 +15,22 @@ public enum ImageSize
1415
Max = 2
1516
}
1617

18+
[Export]
1719
public class ImageItem : ViewModelBase, IDragSource
1820
{
1921
private string filePath;
2022
public string FilePath
2123
{
2224
get { return filePath; }
23-
set { SetProperty(ref filePath, value, "FilePath"); }
25+
set { SetProperty(ref filePath, value, "FilePath", "FileName"); }
26+
}
27+
28+
public string FileName
29+
{
30+
get
31+
{
32+
return this.FilePath.IsNullOrEmpty() ? string.Empty : Path.GetFileName(this.FilePath);
33+
}
2434
}
2535

2636
private ImageSource imageSource;

FileExplorer/ViewTest/ImageViewModel.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace FileExplorer.ViewTest
1111
{
12-
[Export(typeof(ImageViewModel))]
12+
[Export]
1313
public class ImageViewModel : ViewModelBase, IDropHandler
1414
{
1515
private ObservableCollection<ImageItem> items = new ObservableCollection<ImageItem>();
@@ -19,22 +19,26 @@ public ObservableCollection<ImageItem> Items
1919
private set { SetProperty(ref items, value, "Items"); }
2020
}
2121

22-
private ObservableCollection<ImageItem> editintItems = new ObservableCollection<ImageItem>();
23-
public ObservableCollection<ImageItem> EditintItems
22+
private ObservableCollection<ImageItem> editingItems = new ObservableCollection<ImageItem>();
23+
public ObservableCollection<ImageItem> EditingItems
2424
{
25-
get { return editintItems; }
26-
private set { SetProperty(ref editintItems, value, "EditintItems"); }
25+
get { return editingItems; }
26+
private set { SetProperty(ref editingItems, value, "EditingItems"); }
2727
}
2828

2929
public void LoadImages()
3030
{
3131
Items.Clear();
32-
Items.Add(new ImageItem() { FilePath = @"E:\aa.png" });
33-
Items.Add(new ImageItem() { FilePath = @"E:\bb.png" });
34-
Items.Add(new ImageItem() { FilePath = @"E:\aa.png" });
32+
for (int i = 1; i < 9; i++)
33+
{
34+
Items.Add(new ImageItem() { FilePath = string.Format(@" E:\素材\Imag\{0}.png", i) });
35+
}
3536

36-
this.EditintItems.Clear();
37-
Items.All(item => { this.EditintItems.Add(item); return true; });
37+
this.EditingItems.Clear();
38+
for (int i = 0; i < 4; i++)
39+
{
40+
this.EditingItems.Add(Items[i]);
41+
}
3842

3943
DragDrop.DefaultDropHandler = this;
4044
}
@@ -46,7 +50,7 @@ public void OnDragOver(DropInfo dropInfo)
4650
/// TOTO: 需要加入去重
4751
if (dropInfo.Data is ImageItem || dropInfo.Data is IEnumerable<ImageItem>)
4852
{
49-
if (this.EditintItems.Any(item => item == (dropInfo.Data as ImageItem)))
53+
if (this.EditingItems.Any(item => item == (dropInfo.Data as ImageItem)))
5054
{
5155
dropInfo.Effects = DragDropEffects.None;
5256
}
@@ -74,13 +78,16 @@ public void OnDrop(DropInfo dropInfo)
7478
{
7579
if (!imgItem.IsNull())
7680
{
77-
this.EditintItems.Remove(imgView.ViewModel);
81+
this.EditingItems.Remove(imgView.ViewModel);
7882
imgView.DataContext = imgItem;
83+
this.EditingItems.Add(imgItem);
7984
}
8085
else if (!dragItems.IsNullOrEmpty())
8186
{
82-
this.EditintItems.Remove(imgView.ViewModel);
83-
imgView.DataContext = items.FirstOrDefault();
87+
ImageItem firstItem = items.FirstOrDefault();
88+
this.EditingItems.Remove(imgView.ViewModel);
89+
imgView.DataContext = firstItem;
90+
this.EditingItems.Add(firstItem);
8491
}
8592
}
8693
else
@@ -95,11 +102,11 @@ public void OnDrop(DropInfo dropInfo)
95102
{
96103
if (!imgItem.IsNull())
97104
{
98-
this.EditintItems.Add(imgItem);
105+
this.EditingItems.Add(imgItem);
99106
}
100107
else if (!dragItems.IsNullOrEmpty())
101108
{
102-
dragItems.All(i => { this.EditintItems.Add(i); return true; });
109+
dragItems.All(i => { this.EditingItems.Add(i); return true; });
103110
}
104111
}
105112
}

FileExplorer/ViewTest/PanAndZoom.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ namespace FileExplorer.ViewTest
99
{
1010
public class PanAndZoomViewer : ContentControl
1111
{
12+
public event EventHandler ZoomChanged;
13+
1214
public double DefaultZoomFactor { get; set; }
1315
public FrameworkElement source;
1416
private Point ScreenStartPoint = new Point(0, 0);
1517
public TranslateTransform translateTransform;
1618
public ScaleTransform zoomTransform;
1719
public TransformGroup transformGroup;
1820
private Point startOffset;
21+
1922

2023
public PanAndZoomViewer()
2124
{
22-
this.DefaultZoomFactor = 1.1;
25+
this.DefaultZoomFactor = 1.2;
2326
}
2427

2528
public double ZoomFactor
@@ -36,6 +39,10 @@ public override void OnApplyTemplate()
3639
void Setup()
3740
{
3841
this.source = this.Content as FrameworkElement;
42+
if (this.source == null)
43+
{
44+
throw new InvalidOperationException("Conent can't be null");
45+
}
3946

4047
this.translateTransform = new TranslateTransform();
4148
this.zoomTransform = new ScaleTransform();
@@ -44,30 +51,42 @@ void Setup()
4451
this.transformGroup.Children.Add(this.translateTransform);
4552
this.source.RenderTransform = this.transformGroup;
4653
this.Focusable = true;
47-
this.KeyDown += new KeyEventHandler(source_KeyDown);
54+
this.PreviewKeyDown += new KeyEventHandler(control_KeyDown);
4855
this.MouseMove += new MouseEventHandler(control_MouseMove);
49-
this.MouseDown += new MouseButtonEventHandler(source_MouseDown);
50-
this.MouseUp += new MouseButtonEventHandler(source_MouseUp);
56+
this.MouseDown += new MouseButtonEventHandler(control_MouseDown);
57+
this.MouseUp += new MouseButtonEventHandler(control_MouseUp);
5158
this.MouseWheel += new MouseWheelEventHandler(source_MouseWheel);
5259
}
53-
54-
void source_KeyDown(object sender, KeyEventArgs e)
60+
61+
void control_KeyDown(object sender, KeyEventArgs e)
5562
{
5663
// hit escape to reset everything
57-
if (e.Key == Key.Escape) Reset();
64+
if (e.Key == Key.Escape)
65+
{
66+
Reset();
67+
}
5868
}
5969

6070
void source_MouseWheel(object sender, MouseWheelEventArgs e)
6171
{
6272
// zoom into the content. Calculate the zoom factor based on the direction of the mouse wheel.
6373
double zoomFactor = this.DefaultZoomFactor;
64-
if (e.Delta <= 0) zoomFactor = 1.0 / this.DefaultZoomFactor;
74+
if (e.Delta <= 0)
75+
{
76+
zoomFactor = 1.0 / this.DefaultZoomFactor;
77+
}
6578
// DoZoom requires both the logical and physical location of the mouse pointer
66-
var physicalPoint = e.GetPosition(this);
79+
80+
///<**************************************>
81+
///TODO: MUST THE SOURCE POSITION ,IF A PICTURE STRETH MODE IS
82+
///UNIFORM WILL NOT ZOOM ACCURATELY
83+
///<**************************************>
84+
var physicalPoint = e.GetPosition(this.source);
6785
DoZoom(zoomFactor, this.transformGroup.Inverse.Transform(physicalPoint), physicalPoint);
86+
e.Handled = true;
6887
}
6988

70-
void source_MouseUp(object sender, MouseButtonEventArgs e)
89+
void control_MouseUp(object sender, MouseButtonEventArgs e)
7190
{
7291
if (this.IsMouseCaptured)
7392
{
@@ -77,15 +96,15 @@ void source_MouseUp(object sender, MouseButtonEventArgs e)
7796
}
7897
}
7998

80-
void source_MouseDown(object sender, MouseButtonEventArgs e)
99+
void control_MouseDown(object sender, MouseButtonEventArgs e)
81100
{
82101
// Save starting point, used later when determining how much to scroll.
83102
this.ScreenStartPoint = e.GetPosition(this);
84103
this.startOffset = new Point(this.translateTransform.X, this.translateTransform.Y);
85104
this.CaptureMouse();
86105
this.Cursor = Cursors.ScrollAll;
87106
}
88-
107+
89108
void control_MouseMove(object sender, MouseEventArgs e)
90109
{
91110
if (this.IsMouseCaptured)
@@ -94,8 +113,10 @@ void control_MouseMove(object sender, MouseEventArgs e)
94113
// use the Pan Animation to animate to the new location based on the delta between the
95114
// starting point of the mouse and the current point.
96115
var physicalPoint = e.GetPosition(this);
97-
this.translateTransform.BeginAnimation(TranslateTransform.XProperty, CreatePanAnimation(physicalPoint.X - this.ScreenStartPoint.X + this.startOffset.X), HandoffBehavior.Compose);
98-
this.translateTransform.BeginAnimation(TranslateTransform.YProperty, CreatePanAnimation(physicalPoint.Y - this.ScreenStartPoint.Y + this.startOffset.Y), HandoffBehavior.Compose);
116+
this.translateTransform.BeginAnimation(TranslateTransform.XProperty,
117+
CreatePanAnimation(physicalPoint.X - this.ScreenStartPoint.X + this.startOffset.X), HandoffBehavior.Compose);
118+
this.translateTransform.BeginAnimation(TranslateTransform.YProperty,
119+
CreatePanAnimation(physicalPoint.Y - this.ScreenStartPoint.Y + this.startOffset.Y), HandoffBehavior.Compose);
99120
}
100121
}
101122

@@ -127,7 +148,6 @@ private DoubleAnimation CreateZoomAnimation(double toValue)
127148
return da;
128149
}
129150

130-
public event EventHandler ZoomChanged;
131151

132152
/// <summary>Zoom into or out of the content.</summary>
133153
/// <param name="deltaZoom">Factor to mutliply the zoom level by. </param>
@@ -182,7 +202,7 @@ public void Reset()
182202

183203
internal double GetZoom()
184204
{
185-
return zoomTransform.ScaleX;
205+
return ZoomFactor;
186206
}
187207
}
188208
}

0 commit comments

Comments
 (0)