Skip to content

Commit e13e238

Browse files
author
yanjinhuagood
committed
add in CropControl.
1 parent cf9c54f commit e13e238

File tree

13 files changed

+185
-4
lines changed

13 files changed

+185
-4
lines changed

README-zh.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
<a href="https://github.com/ChisterWu" target="_blank"><img style="border-radius:50%!important" width="64px" alt="ChisterWu" src="https://avatars.githubusercontent.com/u/28770378?v=4"></a> <a href="https://github.com/huangjia2107" target="_blank"><img style="border-radius:50%!important" width="64px" alt="huangjia2107" src="https://avatars.githubusercontent.com/u/13675358?v=4"></a> <a href="https://github.com/liukaishuang" target="_blank"><img style="border-radius:50%!important" width="64px" alt="liukaishuang" src="https://avatars.githubusercontent.com/u/25221289?v=4"></a> <a href="https://github.com/TwilightLemon" target="_blank"><img style="border-radius:50%!important" width="64px" alt="TwilightLemon" src="https://avatars.githubusercontent.com/u/25076044?v=4"></a> <a href="https://github.com/githowe" target="_blank"><img style="border-radius:50%!important" width="64px" alt="githowe" src="https://avatars.githubusercontent.com/u/31241366?v=4"></a> <a href="https://github.com/LuGuangguang" target="_blank"><img style="border-radius:50%!important" width="64px" alt="LuGuangguang" src="https://avatars.githubusercontent.com/u/34500722?v=4"></a> <a href="https://github.com/DinoChan" target="_blank"><img style="border-radius:50%!important" width="64px" alt="DinoChan" src="https://avatars.githubusercontent.com/u/6076257?v=4"></a> <a href="https://github.com/mingyaodagege" target="_blank"><img style="border-radius:50%!important" width="64px" alt="mingyaodagege" src="https://avatars.githubusercontent.com/u/48346030?v=4"></a> <a href="https://github.com/PettyHandSome" target="_blank"><img style="border-radius:50%!important" width="64px" alt="PettyHandSome" src="https://avatars.githubusercontent.com/u/16127328?v=4"></a>
3030

3131
***
32+
### CropControl(WPF实现图片切成九宫格控件)
33+
34+
<img src="https://gitee.com/WPFDevelopersOrg/ResourcesCache/raw/main/resources/WPFDevelopersResource/CropControl.gif"/>
35+
36+
----------
37+
3238
### NumberCard(WPF实现3D翻转倒计时控件)
3339

3440
<img src="https://gitee.com/WPFDevelopersOrg/ResourcesCache/raw/main/resources/WPFDevelopersResource/NumberCard.gif"/>

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<a href="https://github.com/ChisterWu" target="_blank"><img style="border-radius:50%!important" width="64px" alt="ChisterWu" src="https://avatars.githubusercontent.com/u/28770378?v=4"></a> <a href="https://github.com/huangjia2107" target="_blank"><img style="border-radius:50%!important" width="64px" alt="huangjia2107" src="https://avatars.githubusercontent.com/u/13675358?v=4"></a> <a href="https://github.com/liukaishuang" target="_blank"><img style="border-radius:50%!important" width="64px" alt="liukaishuang" src="https://avatars.githubusercontent.com/u/25221289?v=4"></a> <a href="https://github.com/TwilightLemon" target="_blank"><img style="border-radius:50%!important" width="64px" alt="TwilightLemon" src="https://avatars.githubusercontent.com/u/25076044?v=4"></a> <a href="https://github.com/githowe" target="_blank"><img style="border-radius:50%!important" width="64px" alt="githowe" src="https://avatars.githubusercontent.com/u/31241366?v=4"></a> <a href="https://github.com/LuGuangguang" target="_blank"><img style="border-radius:50%!important" width="64px" alt="LuGuangguang" src="https://avatars.githubusercontent.com/u/34500722?v=4"></a> <a href="https://github.com/DinoChan" target="_blank"><img style="border-radius:50%!important" width="64px" alt="DinoChan" src="https://avatars.githubusercontent.com/u/6076257?v=4"></a> <a href="https://github.com/mingyaodagege" target="_blank"><img style="border-radius:50%!important" width="64px" alt="mingyaodagege" src="https://avatars.githubusercontent.com/u/48346030?v=4"></a> <a href="https://github.com/PettyHandSome" target="_blank"><img style="border-radius:50%!important" width="64px" alt="PettyHandSome" src="https://avatars.githubusercontent.com/u/16127328?v=4"></a>
2020

2121
***
22+
### CropControl(WPF实现图片切成九宫格控件)
23+
24+
<img src="https://github.com/WPFDevelopersOrg/ResourcesCache/raw/main/resources/WPFDevelopersResource/CropControl.gif"/>
25+
26+
----------
27+
2228
### NumberCard
2329

2430
<img src="https://github.com/WPFDevelopersOrg/ResourcesCache/raw/main/resources/WPFDevelopersResource/NumberCard.gif"/>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.CropControlExample"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
7+
xmlns:wpfdev="https://github.com/yanjinhuagood/WPFDevelopers"
8+
mc:Ignorable="d"
9+
d:DesignHeight="450" d:DesignWidth="800">
10+
<Grid>
11+
<wpfdev:CropControl ImageSource="pack://application:,,,/WPFDevelopers.Samples;component/Images/Crop/0.jpg"/>
12+
</Grid>
13+
</UserControl>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Windows.Controls;
2+
3+
namespace WPFDevelopers.Samples.ExampleViews
4+
{
5+
/// <summary>
6+
/// CropControlExample.xaml 的交互逻辑
7+
/// </summary>
8+
public partial class CropControlExample : UserControl
9+
{
10+
public CropControlExample()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}

src/WPFDevelopers.Samples/Helpers/MenuEnum.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public enum MenuEnum
5252
SpeedRockets,
5353
CountdownTimer,
5454
NumberCard,
55+
CropControl,
5556
TaskbarInfo,
5657
}
5758
}
14.1 KB
Loading

src/WPFDevelopers.Samples/ViewModels/MainVM.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Collections.ObjectModel;
4-
using System.Linq;
5-
using System.Text;
63
using System.Windows;
74
using System.Windows.Controls;
85
using System.Windows.Input;
@@ -233,6 +230,9 @@ void MenuItemSelection(string _menuName)
233230
case MenuEnum.NumberCard:
234231
ControlPanel = new NumberCardExample();
235232
break;
233+
case MenuEnum.CropControl:
234+
ControlPanel = new CropControlExample();
235+
break;
236236
//将TaskbarInfo放到最后
237237
case MenuEnum.TaskbarInfo:
238238
var taskbar = new TaskbarItemInfoExample();

src/WPFDevelopers.Samples/WPFDevelopers.Samples.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
<Compile Include="ExampleViews\CountdownTimerExample.xaml.cs">
9595
<DependentUpon>CountdownTimerExample.xaml</DependentUpon>
9696
</Compile>
97+
<Compile Include="ExampleViews\CropControlExample.xaml.cs">
98+
<DependentUpon>CropControlExample.xaml</DependentUpon>
99+
</Compile>
97100
<Compile Include="ExampleViews\DashboardExample.xaml.cs">
98101
<DependentUpon>DashboardExample.xaml</DependentUpon>
99102
</Compile>
@@ -274,6 +277,10 @@
274277
<SubType>Designer</SubType>
275278
<Generator>MSBuild:Compile</Generator>
276279
</Page>
280+
<Page Include="ExampleViews\CropControlExample.xaml">
281+
<SubType>Designer</SubType>
282+
<Generator>MSBuild:Compile</Generator>
283+
</Page>
277284
<Page Include="ExampleViews\DashboardExample.xaml">
278285
<SubType>Designer</SubType>
279286
<Generator>MSBuild:Compile</Generator>
@@ -605,6 +612,9 @@
605612
<ItemGroup>
606613
<Resource Include="Images\SpeedRockets\飞机.png" />
607614
</ItemGroup>
615+
<ItemGroup>
616+
<Resource Include="Images\Crop\0.jpg" />
617+
</ItemGroup>
608618
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
609619
<PropertyGroup>
610620
<PostBuildEvent>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
using System.Windows.Controls.Primitives;
5+
using System.Windows.Input;
6+
using System.Windows.Media;
7+
using System.Windows.Media.Animation;
8+
using System.Windows.Media.Imaging;
9+
using System.Windows.Shapes;
10+
11+
namespace WPFDevelopers.Controls
12+
{
13+
[TemplatePart(Name = UniformGridTemplateName, Type = typeof(UniformGrid))]
14+
public class CropControl: Control
15+
{
16+
private const string UniformGridTemplateName = "PART_UniformGrid";
17+
18+
private UniformGrid _uniformGrid;
19+
20+
public ImageSource ImageSource
21+
{
22+
get { return (ImageSource)GetValue(ImageSourceProperty); }
23+
set { SetValue(ImageSourceProperty, value); }
24+
}
25+
26+
public static readonly DependencyProperty ImageSourceProperty =
27+
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(CropControl), new PropertyMetadata(null));
28+
29+
public int RowColumn
30+
{
31+
get { return (int)GetValue(RowColumnProperty); }
32+
set { SetValue(RowColumnProperty, value); }
33+
}
34+
35+
public static readonly DependencyProperty RowColumnProperty =
36+
DependencyProperty.Register("RowColumn", typeof(int), typeof(CropControl), new PropertyMetadata(3));
37+
38+
static CropControl()
39+
{
40+
DefaultStyleKeyProperty.OverrideMetadata(typeof(CropControl), new FrameworkPropertyMetadata(typeof(CropControl)));
41+
}
42+
public override void OnApplyTemplate()
43+
{
44+
base.OnApplyTemplate();
45+
_uniformGrid = GetTemplateChild(UniformGridTemplateName) as UniformGrid;
46+
if (ImageSource == null || _uniformGrid == null) return;
47+
48+
BitmapSource imgSource = (BitmapSource)ImageSource;
49+
int w = 0, h = 0;
50+
if (!imgSource.PixelWidth.Equals(0)
51+
&&
52+
!imgSource.PixelHeight.Equals(0))
53+
{
54+
w = imgSource.PixelWidth / RowColumn;
55+
h = (int)imgSource.PixelHeight / RowColumn;
56+
_uniformGrid.Width = imgSource.PixelWidth;
57+
_uniformGrid.Height = imgSource.PixelHeight;
58+
}
59+
for (int i = 0; i < RowColumn; i++)
60+
{
61+
for (int j = 0; j < RowColumn; j++)
62+
{
63+
var rect = new Rectangle
64+
{
65+
Fill = new ImageBrush { ImageSource = new CroppedBitmap(imgSource, new Int32Rect(j * w, i * h, w, h)) },
66+
StrokeThickness = .5,
67+
Stroke = Brushes.White,
68+
Cursor = Cursors.Hand
69+
};
70+
rect.RenderTransformOrigin = new Point(.5, .5);
71+
rect.RenderTransform = new ScaleTransform();
72+
rect.MouseMove += (sender, ex) =>
73+
{
74+
var rect1 = sender as Rectangle;
75+
Panel.SetZIndex(rect1, 1);
76+
var doubleAnimation = new DoubleAnimation
77+
{
78+
To = 2,
79+
Duration = TimeSpan.FromMilliseconds(100),
80+
};
81+
var scaleTransform = rect1.RenderTransform as ScaleTransform;
82+
scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, doubleAnimation);
83+
scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, doubleAnimation);
84+
};
85+
rect.MouseLeave += (sender, ex) =>
86+
{
87+
var rect1 = sender as Rectangle;
88+
Panel.SetZIndex(rect1, 0);
89+
var scaleTransform = rect1.RenderTransform as ScaleTransform;
90+
var doubleAnimation = new DoubleAnimation
91+
{
92+
To = 1,
93+
Duration = TimeSpan.FromMilliseconds(100)
94+
};
95+
scaleTransform.BeginAnimation(ScaleTransform.ScaleXProperty, doubleAnimation);
96+
scaleTransform.BeginAnimation(ScaleTransform.ScaleYProperty, doubleAnimation);
97+
};
98+
_uniformGrid.Children.Add(rect);
99+
}
100+
}
101+
}
102+
}
103+
}

src/WPFDevelopers/Generic.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/RainbowButtons.xaml"/>
3131
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/RoundPicker.xaml"/>
3232
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/SnowCanvas.xaml"/>
33+
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/CropControl.xaml"/>
3334
</ResourceDictionary.MergedDictionaries>
3435
</ResourceDictionary>

0 commit comments

Comments
 (0)