Skip to content

Commit dab5cdb

Browse files
Merge pull request #62 from WPFDevelopersOrg/dev
master pull request dev
2 parents d033b41 + cbb102c commit dab5cdb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1777
-191
lines changed

src/WPFDevelopers.Net40/Themes/Generic.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<Setter.Value>
2828
<ControlTemplate TargetType="{x:Type wd:Window}">
2929
<Border
30+
Name="PART_Border"
3031
Background="{TemplateBinding Background}"
3132
BorderBrush="{TemplateBinding BorderBrush}"
3233
BorderThickness="{TemplateBinding BorderThickness}"
@@ -161,7 +162,7 @@
161162
<Trigger Property="WindowState" Value="Maximized">
162163
<Setter TargetName="PART_RestoreButton" Property="Visibility" Value="Visible" />
163164
<Setter TargetName="PART_MaximizeButton" Property="Visibility" Value="Collapsed" />
164-
<Setter TargetName="LayoutRoot" Property="Margin" Value="7" />
165+
<Setter TargetName="PART_Border" Property="Margin" Value="7" />
165166
</Trigger>
166167
<Trigger Property="WindowStyle" Value="ToolWindow">
167168
<Setter TargetName="PART_MinAndMax" Property="Visibility" Value="Collapsed" />

src/WPFDevelopers.Net40/Themes/Theme.xaml

Lines changed: 245 additions & 42 deletions
Large diffs are not rendered by default.

src/WPFDevelopers.Net45x/Themes/Generic.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<Setter.Value>
3434
<ControlTemplate TargetType="{x:Type wd:Window}">
3535
<Border
36+
Name="PART_Border"
3637
Background="{TemplateBinding Background}"
3738
BorderBrush="{TemplateBinding BorderBrush}"
3839
BorderThickness="{TemplateBinding BorderThickness}"
@@ -167,7 +168,7 @@
167168
<Trigger Property="WindowState" Value="Maximized">
168169
<Setter TargetName="PART_RestoreButton" Property="Visibility" Value="Visible" />
169170
<Setter TargetName="PART_MaximizeButton" Property="Visibility" Value="Collapsed" />
170-
<Setter TargetName="LayoutRoot" Property="Margin" Value="7" />
171+
<Setter TargetName="PART_Border" Property="Margin" Value="7" />
171172
</Trigger>
172173
<Trigger Property="WindowStyle" Value="ToolWindow">
173174
<Setter TargetName="PART_MinAndMax" Property="Visibility" Value="Collapsed" />

src/WPFDevelopers.Net45x/Themes/Theme.xaml

Lines changed: 245 additions & 42 deletions
Large diffs are not rendered by default.

src/WPFDevelopers.Samples.Shared/App.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<!-- 需要注意ws:Resources 必须再配色主题后,Theme="Dark" 为黑色皮肤 -->
1414
<wd:Resources Theme="Light" />
1515
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers;component/Themes/Theme.xaml" />
16+
<!-- 下方不用拷贝 -->
1617
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers.Samples;component/ExampleViews/LoginWindow/Generic.xaml" />
1718
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers.Samples;component/Controls/CodeViewer/CodeViewer.xaml" />
1819
<ResourceDictionary Source="pack://application:,,,/WPFDevelopers.Samples;component/Controls/TimeLine/TimeLine.xaml" />
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<UserControl
2+
x:Class="WPFDevelopers.Samples.ExampleViews.ColorPickerExample"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:controls="clr-namespace:WPFDevelopers.Samples.Controls"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
xmlns:wd="https://github.com/WPFDevelopersOrg/WPFDevelopers"
10+
d:DesignHeight="450"
11+
d:DesignWidth="800"
12+
mc:Ignorable="d">
13+
<controls:CodeViewer>
14+
<wd:ColorPicker />
15+
<controls:CodeViewer.SourceCodes>
16+
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ColorPickerExample.xaml" CodeType="Xaml" />
17+
<controls:SourceCodeModel CodeSource="/WPFDevelopers.SamplesCode;component/ExampleViews/ColorPickerExample.xaml.cs" CodeType="CSharp" />
18+
</controls:CodeViewer.SourceCodes>
19+
</controls:CodeViewer>
20+
</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+
/// BarrageExample.xaml 的交互逻辑
7+
/// </summary>
8+
public partial class ColorPickerExample : UserControl
9+
{
10+
public ColorPickerExample()
11+
{
12+
InitializeComponent();
13+
}
14+
}
15+
}

src/WPFDevelopers.Samples.Shared/ExampleViews/NumericBoxExample.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
Rows="1">
1818
<wd:NumericBox
1919
Width="100"
20+
Height="30"
2021
Maximum="100"
2122
Minimum="0" />
2223
<wd:NumericBox
2324
Width="100"
25+
Height="40"
2426
Margin="10,0"
2527
Maximum="1000"
2628
Minimum="100"

src/WPFDevelopers.Samples.Shared/Helpers/MenuEnum.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public enum MenuEnum
7676
Message,
7777
PathIcon,
7878
NumericBox,
79+
ColorPicker,
7980
VirtualizingWrapPanel,
8081
AcrylicBlur,
8182
TaskbarInfo

src/WPFDevelopers.Samples.Shared/ViewModels/MainVM.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ void MenuItemSelection(string _menuName)
306306
case MenuEnum.NumericBox:
307307
ControlPanel = new NumericBoxExample();
308308
break;
309+
case MenuEnum.ColorPicker:
310+
ControlPanel = new ColorPickerExample();
311+
break;
309312
case MenuEnum.VirtualizingWrapPanel:
310313
ControlPanel = new VirtualizingWrapPanel();
311314
new VirtualizingWrapPanelExample().MaskShowDialog();

src/WPFDevelopers.Samples.Shared/WPFDevelopers.Samples.Shared.projitems

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
<SubType>Code</SubType>
115115
<DependentUpon>CircularProgressBarExample.xaml</DependentUpon>
116116
</Compile>
117+
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\ColorPickerExample.xaml.cs">
118+
<DependentUpon>ColorPickerExample.xaml</DependentUpon>
119+
</Compile>
117120
<Compile Include="$(MSBuildThisFileDirectory)ExampleViews\CountdownTimerExample.xaml.cs">
118121
<SubType>Code</SubType>
119122
<DependentUpon>CountdownTimerExample.xaml</DependentUpon>
@@ -556,6 +559,10 @@
556559
<Generator>MSBuild:Compile</Generator>
557560
<SubType>Designer</SubType>
558561
</Page>
562+
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\ColorPickerExample.xaml">
563+
<Generator>MSBuild:Compile</Generator>
564+
<SubType>Designer</SubType>
565+
</Page>
559566
<Page Include="$(MSBuildThisFileDirectory)ExampleViews\CountdownTimerExample.xaml">
560567
<Generator>MSBuild:Compile</Generator>
561568
<SubType>Designer</SubType>

src/WPFDevelopers.Samples40/WPFDevelopers.Samples40.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</BootstrapperPackage>
3333
</ItemGroup>
3434
<ItemGroup>
35-
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview1" />
35+
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview2" />
3636
</ItemGroup>
3737
<ItemGroup>
3838
<Resource Include="..\WPFDevelopers.Samples.Shared\WPFDevelopers.ico">

src/WPFDevelopers.Samples45x/WPFDevelopers.Samples45x.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<ApplicationIcon>..\WPFDevelopers.Samples.Shared\WPFDevelopers.ico</ApplicationIcon>
1414
</PropertyGroup>
1515
<ItemGroup>
16-
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview1" />
16+
<PackageReference Include="WPFDevelopers" Version="1.1.0.2-preview2" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

src/WPFDevelopers.SamplesCode/WPFDevelopers.SamplesCode.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@
127127
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ScrollViewer\ScrollViewerAnimation.cs" Link="Controls\ScrollViewer\ScrollViewerAnimation.cs" />
128128
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ScrollViewer\ScrollViewerBehavior.cs" Link="Controls\ScrollViewer\ScrollViewerBehavior.cs" />
129129
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\SongWords\SongWords.cs" Link="Controls\SongWords\SongWords.cs" />
130+
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CountdownTimer\CountdownTimer.cs" Link="Controls\CountdownTimer\CountdownTimer.cs" />
130131
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ElementAdorner.cs" Link="Controls\ElementAdorner\ElementAdorner.cs" />
131132
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\ElementAdorner\ThumbAngle.cs" Link="Controls\ElementAdorner\ThumbAngle.cs" />
132133
<Resource Include="..\WPFDevelopers.Samples.Shared\Controls\CropControl\CropControl.cs" Link="Controls\CropControl\CropControl.cs" />
@@ -137,6 +138,7 @@
137138
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\NoneNoChromeWindow.xaml.cs" Link="ExampleViews\NoneNoChromeWindow.xaml.cs" DependentUpon="NoneNoChromeWindow.xaml" />
138139
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\PathIconExample.xaml.cs" Link="ExampleViews\PathIconExample.xaml.cs" DependentUpon="PathIconExample.xaml" />
139140
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\NumericBoxExample.xaml.cs" Link="ExampleViews\NumericBoxExample.xaml.cs" DependentUpon="NumericBoxExample.xaml" />
141+
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\ColorPickerExample.xaml.cs" Link="ExampleViews\ColorPickerExample.xaml.cs" DependentUpon="ColorPickerExample.xaml" />
140142
</ItemGroup>
141143
<ItemGroup>
142144
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\AboutWindow.xaml">
@@ -463,5 +465,8 @@
463465
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\NumericBoxExample.xaml">
464466
<Link>ExampleViews\NumericBoxExample.xaml</Link>
465467
</Resource>
468+
<Resource Include="..\WPFDevelopers.Samples.Shared\ExampleViews\ColorPickerExample.xaml">
469+
<Link>ExampleViews\ColorPickerExample.xaml</Link>
470+
</Resource>
466471
</ItemGroup>
467472
</Project>
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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 WPFDevelopers.Utilities;
8+
9+
namespace WPFDevelopers.Controls
10+
{
11+
[TemplatePart(Name = HueSliderColorTemplateName, Type = typeof(Slider))]
12+
[TemplatePart(Name = CanvasTemplateName, Type = typeof(Canvas))]
13+
[TemplatePart(Name = ThumbTemplateName, Type = typeof(Thumb))]
14+
[TemplatePart(Name = ButtonTemplateName, Type = typeof(Button))]
15+
public class ColorPicker : Control
16+
{
17+
private const string HueSliderColorTemplateName = "PART_HueSlider";
18+
19+
private const string CanvasTemplateName = "PART_Canvas";
20+
21+
private const string ThumbTemplateName = "PART_Thumb";
22+
23+
private const string ButtonTemplateName = "PART_Button";
24+
25+
private static readonly DependencyPropertyKey HueColorPropertyKey =
26+
DependencyProperty.RegisterReadOnly("HueColor", typeof(Color), typeof(ColorPicker),
27+
new PropertyMetadata(Colors.Red));
28+
29+
public static readonly DependencyProperty HueColorProperty = HueColorPropertyKey.DependencyProperty;
30+
31+
public static readonly DependencyProperty SelectedColorProperty =
32+
DependencyProperty.Register("SelectedColor", typeof(Color), typeof(ColorPicker),
33+
new FrameworkPropertyMetadata(Colors.Red, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
34+
OnSelectedColorChanged));
35+
36+
private static readonly DependencyPropertyKey HSBPropertyKey =
37+
DependencyProperty.RegisterReadOnly("HSB", typeof(HSB), typeof(ColorPicker),
38+
new PropertyMetadata(new HSB()));
39+
40+
public static readonly DependencyProperty HSBHProperty = HSBPropertyKey.DependencyProperty;
41+
42+
public static readonly DependencyProperty ColorTypeProperty =
43+
DependencyProperty.Register("ColorType", typeof(ColorTypeEnum), typeof(ColorPicker),
44+
new PropertyMetadata(ColorTypeEnum.RGB));
45+
46+
private Button _button;
47+
48+
private Canvas _canvas;
49+
50+
private Slider _hueSliderColor;
51+
52+
private bool _isInnerUpdateSelectedColor;
53+
54+
private Thumb _thumb;
55+
56+
private ColorTypeEnum[] colorTypeEnums;
57+
58+
private int currentGridStateIndex;
59+
60+
61+
static ColorPicker()
62+
{
63+
DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker),
64+
new FrameworkPropertyMetadata(typeof(ColorPicker)));
65+
}
66+
67+
public Color HueColor => (Color) GetValue(HueColorProperty);
68+
69+
public Color SelectedColor
70+
{
71+
get => (Color) GetValue(SelectedColorProperty);
72+
set => SetValue(SelectedColorProperty, value);
73+
}
74+
75+
public HSB HSB => (HSB) GetValue(HSBHProperty);
76+
77+
78+
public ColorTypeEnum ColorType
79+
{
80+
get => (ColorTypeEnum) GetValue(ColorTypeProperty);
81+
set => SetValue(ColorTypeProperty, value);
82+
}
83+
84+
private static void OnSelectedColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
85+
{
86+
var ctrl = d as ColorPicker;
87+
if (ctrl._isInnerUpdateSelectedColor)
88+
{
89+
ctrl._isInnerUpdateSelectedColor = false;
90+
return;
91+
}
92+
93+
var color = (Color) e.NewValue;
94+
double h = 0, s = 0, b = 0;
95+
ColorUtil.HsbFromColor(color, ref h, ref s, ref b);
96+
var hsb = new HSB {H = h, S = s, B = b};
97+
ctrl.SetValue(HueColorPropertyKey, ColorUtil.ColorFromHsb(hsb.H, 1, 1));
98+
ctrl.SetValue(HSBPropertyKey, hsb);
99+
Canvas.SetLeft(ctrl._thumb, s * ctrl._canvas.ActualWidth - ctrl._thumb.ActualWidth / 2);
100+
Canvas.SetTop(ctrl._thumb, (1 - b) * ctrl._canvas.ActualHeight - ctrl._thumb.ActualHeight / 2);
101+
ctrl._hueSliderColor.Value = 1 - h;
102+
}
103+
104+
public override void OnApplyTemplate()
105+
{
106+
base.OnApplyTemplate();
107+
if (_hueSliderColor != null)
108+
_hueSliderColor.ValueChanged -= HueSliderColor_OnValueChanged;
109+
_canvas = GetTemplateChild(CanvasTemplateName) as Canvas;
110+
if (_canvas != null)
111+
{
112+
_canvas.Loaded += Canvas_Loaded;
113+
_canvas.MouseUp += Canvas_MouseUp;
114+
}
115+
116+
_thumb = GetTemplateChild(ThumbTemplateName) as Thumb;
117+
if (_thumb != null)
118+
_thumb.DragDelta += Thumb_DragDelta;
119+
_hueSliderColor = GetTemplateChild(HueSliderColorTemplateName) as Slider;
120+
if (_hueSliderColor != null)
121+
_hueSliderColor.ValueChanged += HueSliderColor_OnValueChanged;
122+
123+
_button = GetTemplateChild(ButtonTemplateName) as Button;
124+
currentGridStateIndex = 0;
125+
colorTypeEnums = (ColorTypeEnum[]) Enum.GetValues(typeof(ColorTypeEnum));
126+
if (_button != null)
127+
_button.Click += Button_Click;
128+
}
129+
130+
private void Button_Click(object sender, RoutedEventArgs e)
131+
{
132+
currentGridStateIndex = (currentGridStateIndex + 1) % colorTypeEnums.Length;
133+
ColorType = colorTypeEnums[currentGridStateIndex];
134+
}
135+
136+
private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
137+
{
138+
var canvasPosition = e.GetPosition(_canvas);
139+
GetHSB(canvasPosition);
140+
}
141+
142+
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
143+
{
144+
var canvasPosition = e.GetPosition(_canvas);
145+
GetHSB(canvasPosition);
146+
}
147+
148+
private void GetHSB(Point point, bool isMove = true)
149+
{
150+
var newLeft = point.X - _thumb.ActualWidth / 2;
151+
var newTop = point.Y - _thumb.ActualHeight / 2;
152+
var thumbW = _thumb.ActualWidth / 2;
153+
var thumbH = _thumb.ActualHeight / 2;
154+
var canvasRight = _canvas.ActualWidth - thumbW;
155+
var canvasBottom = _canvas.ActualHeight - thumbH;
156+
if (newLeft < -thumbW)
157+
newLeft = -thumbW;
158+
else if (newLeft > canvasRight)
159+
newLeft = canvasRight;
160+
if (newTop < -thumbH)
161+
newTop = -thumbH;
162+
else if (newTop > canvasBottom)
163+
newTop = canvasBottom;
164+
165+
if (isMove)
166+
{
167+
Canvas.SetLeft(_thumb, newLeft);
168+
Canvas.SetTop(_thumb, newTop);
169+
}
170+
171+
var hsb = new HSB
172+
{
173+
H = HSB.H, S = (newLeft + thumbW) / _canvas.ActualWidth,
174+
B = 1 - (newTop + thumbH) / _canvas.ActualHeight
175+
};
176+
SetValue(HSBPropertyKey, hsb);
177+
var currentColor = ColorUtil.ColorFromAhsb(1, HSB.H, HSB.S, HSB.B);
178+
if (SelectedColor != currentColor)
179+
{
180+
_isInnerUpdateSelectedColor = true;
181+
SelectedColor = currentColor;
182+
}
183+
}
184+
185+
private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
186+
{
187+
var point = Mouse.GetPosition(_canvas);
188+
GetHSB(point);
189+
}
190+
191+
private void Canvas_Loaded(object sender, RoutedEventArgs e)
192+
{
193+
var width = (int) _canvas.ActualWidth;
194+
var height = (int) _canvas.ActualHeight;
195+
var point = new Point(width - _thumb.ActualWidth / 2, -_thumb.ActualHeight / 2);
196+
Canvas.SetLeft(_thumb, point.X);
197+
Canvas.SetTop(_thumb, point.Y);
198+
var hsb = new HSB {H = _hueSliderColor.Value, S = HSB.S, B = HSB.B};
199+
SetValue(HSBPropertyKey, hsb);
200+
}
201+
202+
private void HueSliderColor_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
203+
{
204+
if (DoubleUtil.AreClose(HSB.H, e.NewValue))
205+
return;
206+
var hsb = new HSB {H = 1 - e.NewValue, S = HSB.S, B = HSB.B};
207+
SetValue(HSBPropertyKey, hsb);
208+
SetValue(HueColorPropertyKey, ColorUtil.ColorFromHsb(HSB.H, 1, 1));
209+
210+
var newLeft = Canvas.GetLeft(_thumb);
211+
var newTop = Canvas.GetTop(_thumb);
212+
var point = new Point(newLeft, newTop);
213+
GetHSB(point, false);
214+
}
215+
}
216+
217+
public enum ColorTypeEnum
218+
{
219+
RGB,
220+
HSL,
221+
HEX
222+
}
223+
}

0 commit comments

Comments
 (0)