├── .vs
└── DragControlHelperTest
│ └── v14
│ └── .suo
├── DragControlHelperTest.sln
├── DragControlHelperTest
├── App.config
├── App.xaml
├── App.xaml.cs
├── DragControlHelperTest.csproj
├── MainWindow.xaml
├── MainWindow.xaml.cs
└── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
├── README.md
├── UICommon
├── Controls
│ └── DragHelper
│ │ ├── CustomThumb.cs
│ │ ├── DragChangedEventArgs.cs
│ │ ├── DragControlHelper.cs
│ │ ├── DragDirection.cs
│ │ ├── DragHelperBase.cs
│ │ └── DragHelperBaseStyle.xaml
├── Properties
│ └── AssemblyInfo.cs
├── Themes
│ └── Generic.xaml
└── UICommon.csproj
└── 删除目标文件(用于打包备份).bat
/.vs/DragControlHelperTest/v14/.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YuandeChen/DragControlHelper/338002ecb11d44078f84358883e545dd6d0bb2fc/.vs/DragControlHelperTest/v14/.suo
--------------------------------------------------------------------------------
/DragControlHelperTest.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.24720.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DragControlHelperTest", "DragControlHelperTest\DragControlHelperTest.csproj", "{3AB0C88E-D60D-4D1E-B2BA-127C11AD4563}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UICommon", "UICommon\UICommon.csproj", "{FC3FAC70-A4BB-45EF-B7CA-4A74368E5D81}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {3AB0C88E-D60D-4D1E-B2BA-127C11AD4563}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {3AB0C88E-D60D-4D1E-B2BA-127C11AD4563}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {3AB0C88E-D60D-4D1E-B2BA-127C11AD4563}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {3AB0C88E-D60D-4D1E-B2BA-127C11AD4563}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {FC3FAC70-A4BB-45EF-B7CA-4A74368E5D81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {FC3FAC70-A4BB-45EF-B7CA-4A74368E5D81}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {FC3FAC70-A4BB-45EF-B7CA-4A74368E5D81}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {FC3FAC70-A4BB-45EF-B7CA-4A74368E5D81}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | EndGlobal
29 |
--------------------------------------------------------------------------------
/DragControlHelperTest/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/DragControlHelperTest/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/DragControlHelperTest/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace DragControlHelperTest
10 | {
11 | ///
12 | /// App.xaml 的交互逻辑
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/DragControlHelperTest/DragControlHelperTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {3AB0C88E-D60D-4D1E-B2BA-127C11AD4563}
8 | WinExe
9 | Properties
10 | DragControlHelperTest
11 | DragControlHelperTest
12 | v4.6
13 | 512
14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
15 | 4
16 | true
17 |
18 |
19 | AnyCPU
20 | true
21 | full
22 | false
23 | bin\Debug\
24 | DEBUG;TRACE
25 | prompt
26 | 4
27 |
28 |
29 | AnyCPU
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | 4.0
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | MSBuild:Compile
56 | Designer
57 |
58 |
59 | MSBuild:Compile
60 | Designer
61 |
62 |
63 | App.xaml
64 | Code
65 |
66 |
67 | MainWindow.xaml
68 | Code
69 |
70 |
71 |
72 |
73 | Code
74 |
75 |
76 | True
77 | True
78 | Resources.resx
79 |
80 |
81 | True
82 | Settings.settings
83 | True
84 |
85 |
86 | ResXFileCodeGenerator
87 | Resources.Designer.cs
88 |
89 |
90 | SettingsSingleFileGenerator
91 | Settings.Designer.cs
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | {fc3fac70-a4bb-45ef-b7ca-4a74368e5d81}
101 | UICommon
102 |
103 |
104 |
105 |
112 |
--------------------------------------------------------------------------------
/DragControlHelperTest/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/DragControlHelperTest/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows;
7 | using System.Windows.Controls;
8 | using System.Windows.Data;
9 | using System.Windows.Documents;
10 | using System.Windows.Input;
11 | using System.Windows.Media;
12 | using System.Windows.Media.Imaging;
13 | using System.Windows.Navigation;
14 | using System.Windows.Shapes;
15 |
16 | namespace DragControlHelperTest
17 | {
18 | ///
19 | /// MainWindow.xaml 的交互逻辑
20 | ///
21 | public partial class MainWindow : Window
22 | {
23 | public MainWindow()
24 | {
25 | InitializeComponent();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/DragControlHelperTest/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // 有关程序集的一般信息由以下
8 | // 控制。更改这些特性值可修改
9 | // 与程序集关联的信息。
10 | [assembly: AssemblyTitle("DragControlHelperTest")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("")]
14 | [assembly: AssemblyProduct("DragControlHelperTest")]
15 | [assembly: AssemblyCopyright("Copyright © 2016")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | //将 ComVisible 设置为 false 将使此程序集中的类型
20 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
21 | //请将此类型的 ComVisible 特性设置为 true。
22 | [assembly: ComVisible(false)]
23 |
24 | //若要开始生成可本地化的应用程序,请
25 | // 中的 .csproj 文件中
26 | //例如,如果您在源文件中使用的是美国英语,
27 | //使用的是美国英语,请将 设置为 en-US。 然后取消
28 | //对以下 NeutralResourceLanguage 特性的注释。 更新
29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置
36 | //(当资源未在页面
37 | //或应用程序资源字典中找到时使用)
38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
39 | //(当资源未在页面
40 | //、应用程序或任何主题专用资源字典中找到时使用)
41 | )]
42 |
43 |
44 | // 程序集的版本信息由下列四个值组成:
45 | //
46 | // 主版本
47 | // 次版本
48 | // 生成号
49 | // 修订号
50 | //
51 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
52 | // 方法是按如下所示使用“*”: :
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/DragControlHelperTest/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // 此代码由工具生成。
4 | // 运行时版本: 4.0.30319.42000
5 | //
6 | // 对此文件的更改可能导致不正确的行为,如果
7 | // 重新生成代码,则所做更改将丢失。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace DragControlHelperTest.Properties
12 | {
13 |
14 |
15 | ///
16 | /// 强类型资源类,用于查找本地化字符串等。
17 | ///
18 | // 此类是由 StronglyTypedResourceBuilder
19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
20 | // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen
21 | // (以 /str 作为命令选项),或重新生成 VS 项目。
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources
26 | {
27 |
28 | private static global::System.Resources.ResourceManager resourceMan;
29 |
30 | private static global::System.Globalization.CultureInfo resourceCulture;
31 |
32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33 | internal Resources()
34 | {
35 | }
36 |
37 | ///
38 | /// 返回此类使用的缓存 ResourceManager 实例。
39 | ///
40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41 | internal static global::System.Resources.ResourceManager ResourceManager
42 | {
43 | get
44 | {
45 | if ((resourceMan == null))
46 | {
47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DragControlHelperTest.Properties.Resources", typeof(Resources).Assembly);
48 | resourceMan = temp;
49 | }
50 | return resourceMan;
51 | }
52 | }
53 |
54 | ///
55 | /// 覆盖当前线程的 CurrentUICulture 属性
56 | /// 使用此强类型的资源类的资源查找。
57 | ///
58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
59 | internal static global::System.Globalization.CultureInfo Culture
60 | {
61 | get
62 | {
63 | return resourceCulture;
64 | }
65 | set
66 | {
67 | resourceCulture = value;
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/DragControlHelperTest/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/DragControlHelperTest/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace DragControlHelperTest.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/DragControlHelperTest/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 我的第一个GitHub项目
2 |
3 | 这是项目 [DragControlHelper](https://github.com/YuandeChen/DragControlHelper) ,
4 | 欢迎访问。
5 |
6 | 第一次使用github。。。。
7 | 这个项目是WPF的自定义控件,可以在Canvas中通过鼠标拖动、放大ShapeBase的控件;如果你愿意,也可以通过派生DragHelperBase来实现在其他控件中的拖放。
8 |
9 |
--------------------------------------------------------------------------------
/UICommon/Controls/DragHelper/CustomThumb.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Windows.Controls.Primitives;
7 |
8 | namespace UICommon.Controls
9 | {
10 | public class CustomThumb : Thumb
11 | {
12 | public DragDirection DragDirection { get; set; }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/UICommon/Controls/DragHelper/DragChangedEventArgs.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Windows;
3 | using System.Windows.Shapes;
4 | using System.Windows.Controls;
5 |
6 | namespace UICommon.Controls
7 | {
8 | public class DragChangedEventArgs : RoutedEventArgs
9 | {
10 | public DragChangedEventArgs(RoutedEvent Event, Rect NewBound, object Target = null) : base(Event)
11 | {
12 | this.NewBound = NewBound;
13 | DragTargetElement = Target;
14 | }
15 | public Rect NewBound { get; private set; }
16 |
17 | public object DragTargetElement { get; private set; }
18 | }
19 | public delegate void DragChangedEventHandler(object Sender, DragChangedEventArgs e);
20 | }
21 |
--------------------------------------------------------------------------------
/UICommon/Controls/DragHelper/DragControlHelper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Windows;
3 | using System.Windows.Input;
4 | using System.Windows.Shapes;
5 | using System.Windows.Controls;
6 |
7 | namespace UICommon.Controls
8 | {
9 | public class DragControlHelper : DragHelperBase
10 | {
11 | #region Cotr & Events
12 | public DragControlHelper()
13 | {
14 | this.Loaded += OnLoaded;
15 | this.Unloaded += OnUnloaded;
16 | }
17 |
18 | private void OnLoaded(object sender, RoutedEventArgs e)
19 | {
20 | AttachParentEvents();
21 | this.Loaded -= OnLoaded;
22 | }
23 | private void OnUnloaded(object sender, RoutedEventArgs e)
24 | {
25 | DetachParentEvents();
26 | this.Unloaded -= OnUnloaded;
27 | }
28 | #endregion
29 |
30 | #region DependencyProperty
31 |
32 | #region IsEditable
33 | public static bool GetIsEditable(DependencyObject obj)
34 | {
35 | return (bool)obj.GetValue(IsEditableProperty);
36 | }
37 |
38 | public static void SetIsEditable(DependencyObject obj, bool value)
39 | {
40 | obj.SetValue(IsEditableProperty, value);
41 | }
42 |
43 | // Using a DependencyProperty as the backing store for IsEditable. This enables animation, styling, binding, etc...
44 | public static readonly DependencyProperty IsEditableProperty =
45 | DependencyProperty.RegisterAttached("IsEditable", typeof(bool), typeof(DragHelperBase), new PropertyMetadata(false));
46 | #endregion
47 |
48 | #region IsSelectable
49 | public static bool GetIsSelectable(DependencyObject obj)
50 | {
51 | return (bool)obj.GetValue(IsSelectableProperty);
52 | }
53 |
54 | public static void SetIsSelectable(DependencyObject obj, bool value)
55 | {
56 | obj.SetValue(IsSelectableProperty, value);
57 | }
58 |
59 | // Using a DependencyProperty as the backing store for IsSelectable. This enables animation, styling, binding, etc...
60 | public static readonly DependencyProperty IsSelectableProperty =
61 | DependencyProperty.RegisterAttached("IsSelectable", typeof(bool), typeof(DragHelperBase), new PropertyMetadata(false));
62 | #endregion
63 |
64 | #region TargetElement
65 | public FrameworkElement TargetElement
66 | {
67 | get { return (FrameworkElement)GetValue(TargetElementProperty); }
68 | set { SetValue(TargetElementProperty, value); }
69 | }
70 |
71 | // Using a DependencyProperty as the backing store for TargetElement. This enables animation, styling, binding, etc...
72 | public static readonly DependencyProperty TargetElementProperty =
73 | DependencyProperty.Register("TargetElement", typeof(FrameworkElement), typeof(DragControlHelper),
74 | new FrameworkPropertyMetadata(default(FrameworkElement),
75 | new PropertyChangedCallback(TargetElementChanged)));
76 | #endregion
77 |
78 | #region TargetElementChanged
79 | private static void TargetElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
80 | {
81 | DragControlHelper Helper = d as DragControlHelper;
82 |
83 | if (Helper == null)
84 | {
85 | return;
86 | }
87 |
88 | FrameworkElement Target = e.OldValue as FrameworkElement;
89 |
90 | Helper.DetachTatgetEvents(Target);
91 |
92 | Target = e.NewValue as FrameworkElement;
93 |
94 | if (Target == null
95 | || Target is DragControlHelper
96 | || Target.RenderSize.IsEmpty
97 | || double.IsNaN(Target.RenderSize.Width)
98 | || double.IsNaN(Target.RenderSize.Height)
99 | || !GetIsSelectable(Target))
100 | {
101 | Helper.Visibility = Visibility.Collapsed;
102 | return;
103 | }
104 |
105 | Helper.AttachTatgetEvents(Target);
106 |
107 | int Zindex = Panel.GetZIndex(Target);
108 |
109 | if (Zindex >= Panel.GetZIndex(Helper))
110 | {
111 | Panel.SetZIndex(Helper, Zindex + 1);
112 | }
113 |
114 | double Y = CorrectDoubleValue(Canvas.GetTop(Target));
115 | double X = CorrectDoubleValue(Canvas.GetLeft(Target));
116 |
117 | Canvas.SetTop(Helper, Y);
118 | Canvas.SetLeft(Helper, X);
119 | Helper.Width = Target.ActualWidth;
120 | Helper.Height = Target.ActualHeight;
121 | }
122 | #endregion
123 |
124 | #endregion
125 |
126 | #region Parent Event Handler
127 |
128 | #region AttachParentEvents
129 | private void AttachParentEvents()
130 | {
131 | Canvas CanvasParent = Parent as Canvas;
132 |
133 | if (CanvasParent == null)
134 | {
135 | throw new Exception("DragControlHelper Must place into Canvas!");
136 | }
137 |
138 | CanvasParent.MouseLeftButtonDown += OnParentMouseLeftButtonDown;
139 | }
140 | #endregion
141 |
142 | #region DetachParentEvents
143 | private void DetachParentEvents()
144 | {
145 | Canvas CanvasParent = Parent as Canvas;
146 |
147 | if (CanvasParent != null)
148 | {
149 | CanvasParent.MouseLeftButtonDown -= OnParentMouseLeftButtonDown;
150 | }
151 | }
152 | #endregion
153 |
154 | #region OnParentMouseLeftButtonDown
155 | private void OnParentMouseLeftButtonDown(object Sender, MouseButtonEventArgs e)
156 | {
157 | FrameworkElement SelectedElement = e.OriginalSource as FrameworkElement;
158 |
159 | if (CheckTargetIsSelectable(SelectedElement))
160 | {
161 | TargetElement = SelectedElement;
162 | }
163 | else
164 | {
165 | TargetElement = null;
166 | }
167 |
168 | SelectedElement.Focus();
169 | }
170 | #endregion
171 |
172 | #region CheckTargetIsSelectable
173 | private bool CheckTargetIsSelectable(FrameworkElement Target)
174 | {
175 | return (Target != null) && !Target.Equals(Parent) && !Target.Equals(this) && GetIsSelectable(Target);
176 | }
177 | #endregion
178 |
179 | #endregion
180 |
181 | #region Target Event Handler
182 |
183 | #region AttachTatgetEvents
184 | private void AttachTatgetEvents(FrameworkElement Target)
185 | {
186 | if (Target == null)
187 | {
188 | throw new ArgumentNullException("Target");
189 | }
190 |
191 | Target.Focusable = true;
192 | Target.GotFocus += TargetElement_GotFocus;
193 | Target.LostFocus += TargetElement_LostFocus;
194 |
195 | double Thickness = 1.0;
196 |
197 | if (Target is Shape)
198 | {
199 | Thickness = (Target as Shape).StrokeThickness;
200 | }
201 |
202 | bool CanEdit = GetIsEditable(Target);
203 |
204 | SetupVisualPropertes(Thickness, CanEdit);
205 | }
206 | #endregion
207 |
208 | #region DetachTatgetEvents
209 | private void DetachTatgetEvents(FrameworkElement Target)
210 | {
211 | if (Target != null)
212 | {
213 | Target.GotFocus -= TargetElement_GotFocus;
214 | Target.LostFocus -= TargetElement_LostFocus;
215 | }
216 | }
217 | #endregion
218 |
219 | #region TargetElement_GotFocus
220 | private void TargetElement_GotFocus(object sender, RoutedEventArgs e)
221 | {
222 | Visibility = Visibility.Visible;
223 | }
224 | #endregion
225 |
226 | #region TargetElement_LostFocus
227 | private void TargetElement_LostFocus(object sender, RoutedEventArgs e)
228 | {
229 | TargetElement = null;
230 | Visibility = Visibility.Collapsed;
231 | }
232 | #endregion
233 |
234 | #endregion
235 |
236 | #region DragHelperBase Member
237 |
238 | #region GetTargetActualBound
239 | protected override Rect GetTargetActualBound()
240 | {
241 | if (TargetElement == null)
242 | {
243 | return Rect.Empty;
244 | }
245 |
246 | double Top = CorrectDoubleValue(Canvas.GetTop(TargetElement));
247 | double Left = CorrectDoubleValue(Canvas.GetLeft(TargetElement));
248 |
249 |
250 |
251 | return new Rect
252 | {
253 | X = Left,
254 | Y = Top,
255 | Width = TargetElement.ActualWidth,
256 | Height = TargetElement.ActualHeight
257 | };
258 | }
259 | #endregion
260 |
261 | #region SetTargetActualBound
262 | protected override void SetTargetActualBound(Rect NewBound)
263 | {
264 | if (TargetElement != null)
265 | {
266 | TargetElement.Width = NewBound.Width;
267 | TargetElement.Height = NewBound.Height;
268 | Canvas.SetTop(TargetElement, NewBound.Y);
269 | Canvas.SetLeft(TargetElement, NewBound.X);
270 | }
271 | }
272 | #endregion
273 |
274 | #region RaisenDragCompletedEvent
275 | protected override void RaisenDragCompletedEvent(Rect NewBound)
276 | {
277 | RaiseEvent(new DragChangedEventArgs(DragCompletedEvent, NewBound, TargetElement));
278 | }
279 |
280 | protected override void RaisenDragChangingEvent(Rect NewBound)
281 | {
282 | RaiseEvent(new DragChangedEventArgs(DragChangingEvent, NewBound, TargetElement));
283 | }
284 |
285 | protected override bool GetTargetIsEditable()
286 | {
287 | return GetIsEditable(TargetElement);
288 | }
289 | #endregion
290 |
291 | #endregion
292 | }
293 | }
294 |
--------------------------------------------------------------------------------
/UICommon/Controls/DragHelper/DragDirection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace UICommon.Controls
8 | {
9 | public enum DragDirection
10 | {
11 | TopLeft = 1,
12 | TopCenter = 2,
13 | TopRight = 4,
14 | MiddleLeft = 16,
15 | MiddleCenter = 32,
16 | MiddleRight = 64,
17 | BottomLeft = 256,
18 | BottomCenter = 512,
19 | BottomRight = 1024,
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/UICommon/Controls/DragHelper/DragHelperBase.cs:
--------------------------------------------------------------------------------
1 | using System.Windows;
2 | using System.ComponentModel;
3 | using System.Windows.Controls;
4 | using System.Windows.Controls.Primitives;
5 |
6 | namespace UICommon.Controls
7 | {
8 | [TemplatePart(Name = "PART_MainGrid", Type = typeof(Grid))]
9 | public abstract class DragHelperBase : ContentControl
10 | {
11 | #region Ctor
12 | static DragHelperBase()
13 | {
14 | DefaultStyleKeyProperty.OverrideMetadata(typeof(DragHelperBase),
15 | new FrameworkPropertyMetadata(typeof(DragHelperBase)));
16 | }
17 | public DragHelperBase()
18 | {
19 | SetResourceReference(StyleProperty, typeof(DragHelperBase));
20 | }
21 | #endregion
22 |
23 | #region Propertes
24 |
25 | #region CornerWidth
26 | public int CornerWidth
27 | {
28 | get { return (int)GetValue(CornerWidthProperty); }
29 | set { SetValue(CornerWidthProperty, value); }
30 | }
31 |
32 | // Using a DependencyProperty as the backing store for CornerWidth. This enables animation, styling, binding, etc...
33 | public static readonly DependencyProperty CornerWidthProperty =
34 | DependencyProperty.Register("CornerWidth", typeof(int), typeof(DragHelperBase), new PropertyMetadata(31));
35 | #endregion
36 |
37 | #region PART Object
38 |
39 | #region MainGrid
40 | [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
41 | private Grid MainGrid;
42 | #endregion
43 |
44 | #endregion
45 |
46 | #region DragHelperParent
47 | protected FrameworkElement DragHelperParent
48 | {
49 | get
50 | {
51 | return Parent as FrameworkElement;
52 | }
53 | }
54 | #endregion
55 |
56 | #endregion
57 |
58 | #region Drag & Resize
59 |
60 | #region DragElement
61 | private Rect DragElement(double HorizontalChange, double VerticalChange)
62 | {
63 | Rect TargetActualBound = GetTargetActualBound();
64 |
65 | double TopOld = CorrectDoubleValue(TargetActualBound.Y);
66 | double LeftOld = CorrectDoubleValue(TargetActualBound.X);
67 | double TopNew = CorrectDoubleValue(TopOld + VerticalChange);
68 | double LeftNew = CorrectDoubleValue(LeftOld + HorizontalChange);
69 |
70 | TopNew = CorrectNewTop(DragHelperParent, TopNew, TargetActualBound.Height);
71 | LeftNew = CorrectNewLeft(DragHelperParent, LeftNew, TargetActualBound.Width);
72 |
73 | Canvas.SetTop(this, TopNew);
74 | Canvas.SetLeft(this, LeftNew);
75 |
76 | return new Rect
77 | {
78 | Y = TopNew,
79 | X = LeftNew,
80 | Width = TargetActualBound.Width,
81 | Height = TargetActualBound.Height
82 | };
83 | }
84 | #endregion
85 |
86 | #region ResizeElement
87 | private Rect ResizeElement(CustomThumb HitedThumb, double HorizontalChange, double VerticalChange)
88 | {
89 | #region Get Old Value
90 |
91 | if (HitedThumb == null) return Rect.Empty;
92 |
93 |
94 | Rect TargetActualBound = GetTargetActualBound();
95 |
96 | double TopOld = CorrectDoubleValue(TargetActualBound.Y);
97 | double LeftOld = CorrectDoubleValue(TargetActualBound.X);
98 | double WidthOld = CorrectDoubleValue(TargetActualBound.Width);
99 | double HeightOld = CorrectDoubleValue(TargetActualBound.Height);
100 |
101 | double TopNew = TopOld;
102 | double LeftNew = LeftOld;
103 | double WidthNew = WidthOld;
104 | double HeightNew = HeightOld;
105 |
106 | #endregion
107 |
108 | if (HitedThumb.DragDirection == DragDirection.TopLeft
109 | || HitedThumb.DragDirection == DragDirection.MiddleLeft
110 | || HitedThumb.DragDirection == DragDirection.BottomLeft)
111 | {
112 | ResizeFromLeft(DragHelperParent, LeftOld, WidthOld, HorizontalChange, out LeftNew, out WidthNew);
113 | }
114 |
115 | if (HitedThumb.DragDirection == DragDirection.TopLeft
116 | || HitedThumb.DragDirection == DragDirection.TopCenter
117 | || HitedThumb.DragDirection == DragDirection.TopRight)
118 | {
119 | ResizeFromTop(DragHelperParent, TopOld, HeightOld, VerticalChange, out TopNew, out HeightNew);
120 | }
121 |
122 | if (HitedThumb.DragDirection == DragDirection.TopRight
123 | || HitedThumb.DragDirection == DragDirection.MiddleRight
124 | || HitedThumb.DragDirection == DragDirection.BottomRight)
125 | {
126 | ResizeFromRight(DragHelperParent, LeftOld, WidthOld, HorizontalChange, out WidthNew);
127 | }
128 |
129 | if (HitedThumb.DragDirection == DragDirection.BottomLeft
130 | || HitedThumb.DragDirection == DragDirection.BottomCenter
131 | || HitedThumb.DragDirection == DragDirection.BottomRight)
132 | {
133 | ResizeFromBottom(DragHelperParent, TopOld, HeightOld, VerticalChange, out HeightNew);
134 | }
135 |
136 | this.Width = WidthNew;
137 | this.Height = HeightNew;
138 | Canvas.SetTop(this, TopNew);
139 | Canvas.SetLeft(this, LeftNew);
140 |
141 | return new Rect
142 | {
143 | X = LeftNew,
144 | Y = TopNew,
145 | Width = WidthNew,
146 | Height = HeightNew
147 | };
148 | }
149 | #endregion
150 |
151 | #region Resize Base Methods
152 |
153 | #region ResizeFromTop
154 | private static void ResizeFromTop(FrameworkElement Parent, double TopOld, double HeightOld, double VerticalChange, out double TopNew, out double HeightNew)
155 | {
156 | double MiniHeight = 10;
157 |
158 | double top = TopOld + VerticalChange;
159 | TopNew = ((top + MiniHeight) > (HeightOld + TopOld)) ? HeightOld + TopOld - MiniHeight : top;
160 | TopNew = TopNew < 0 ? 0 : TopNew;
161 |
162 | HeightNew = HeightOld + TopOld - TopNew;
163 |
164 | HeightNew = CorrectNewHeight(Parent, TopNew, HeightNew);
165 | }
166 | #endregion
167 |
168 | #region ResizeFromLeft
169 | private static void ResizeFromLeft(FrameworkElement Parent, double LeftOld, double WidthOld, double HorizontalChange, out double LeftNew, out double WidthNew)
170 | {
171 | double MiniWidth = 10;
172 | double left = LeftOld + HorizontalChange;
173 |
174 | LeftNew = ((left + MiniWidth) > (WidthOld + LeftOld)) ? WidthOld + LeftOld - MiniWidth : left;
175 |
176 | LeftNew = LeftNew < 0 ? 0 : LeftNew;
177 |
178 | WidthNew = WidthOld + LeftOld - LeftNew;
179 |
180 | WidthNew = CorrectNewWidth(Parent, LeftNew, WidthNew);
181 | }
182 | #endregion
183 |
184 | #region ResizeFromRight
185 | private static void ResizeFromRight(FrameworkElement Parent, double LeftOld, double WidthOld, double HorizontalChange, out double WidthNew)
186 | {
187 | if (LeftOld + WidthOld + HorizontalChange < Parent.ActualWidth)
188 | {
189 | WidthNew = WidthOld + HorizontalChange;
190 | }
191 | else
192 | {
193 | WidthNew = Parent.ActualWidth - LeftOld;
194 | }
195 |
196 | WidthNew = WidthNew < 0 ? 0 : WidthNew;
197 | }
198 | #endregion
199 |
200 | #region ResizeFromBottom
201 | private static void ResizeFromBottom(FrameworkElement Parent, double TopOld, double HeightOld, double VerticalChange, out double HeightNew)
202 | {
203 | if (TopOld + HeightOld + VerticalChange < Parent.ActualWidth)
204 | {
205 | HeightNew = HeightOld + VerticalChange;
206 | }
207 | else
208 | {
209 | HeightNew = Parent.ActualWidth - TopOld;
210 | }
211 |
212 | HeightNew = HeightNew < 0 ? 0 : HeightNew;
213 | }
214 | #endregion
215 |
216 | #region CorrectNewTop
217 | private static double CorrectNewTop(FrameworkElement Parent, double Top, double Height)
218 | {
219 | double NewHeight = ((Top + Height) > Parent.ActualHeight) ? (Parent.ActualHeight - Height) : Top;
220 | return NewHeight < 0 ? 0 : NewHeight;
221 | }
222 | #endregion
223 |
224 | #region CorrectNewLeft
225 | private static double CorrectNewLeft(FrameworkElement Parent, double Left, double Width)
226 | {
227 | double NewLeft = ((Left + Width) > Parent.ActualWidth) ? (Parent.ActualWidth - Width) : Left;
228 |
229 | return NewLeft < 0 ? 0 : NewLeft;
230 | }
231 | #endregion
232 |
233 | #region CorrectNewWidth
234 | private static double CorrectNewWidth(FrameworkElement Parent, double Left, double WidthNewToCheck)
235 | {
236 | double Width = ((Left + WidthNewToCheck) > Parent.ActualWidth) ? (Parent.ActualWidth - Left) : WidthNewToCheck;
237 |
238 | return Width < 0 ? 0 : Width;
239 | }
240 | #endregion
241 |
242 | #region CorrectNewHeight
243 | private static double CorrectNewHeight(FrameworkElement Parent, double Top, double HeightNewToCheck)
244 | {
245 | double Height = ((Top + HeightNewToCheck) > Parent.ActualHeight) ? (Parent.ActualHeight - Top) : HeightNewToCheck;
246 | return Height < 0 ? 0 : Height;
247 | }
248 | #endregion
249 |
250 | #region CorrectDoubleValue
251 | protected static double CorrectDoubleValue(double Value)
252 | {
253 | return (double.IsNaN(Value) || (Value < 0.0)) ? 0 : Value;
254 | }
255 | #endregion
256 |
257 | #endregion
258 |
259 | protected abstract bool GetTargetIsEditable();
260 | protected abstract Rect GetTargetActualBound();
261 | protected abstract void SetTargetActualBound(Rect NewBound);
262 | protected abstract void RaisenDragChangingEvent(Rect NewBound);
263 | protected abstract void RaisenDragCompletedEvent(Rect NewBound);
264 |
265 | #endregion
266 |
267 | #region Layout & Display
268 |
269 | #region OnApplyTemplate
270 | public sealed override void OnApplyTemplate()
271 | {
272 | base.OnApplyTemplate();
273 |
274 | MainGrid = GetPartFormTemplate("PART_MainGrid");
275 |
276 | AddLogicalChild(MainGrid);
277 |
278 | AddHandler(Thumb.DragDeltaEvent, new DragDeltaEventHandler(OnDragDelta));
279 | AddHandler(Thumb.DragCompletedEvent, new RoutedEventHandler(OnDragCompleted));
280 |
281 | Visibility = Visibility.Collapsed;
282 | }
283 |
284 | #endregion
285 |
286 | #region GetPartFormTemplate
287 | private T GetPartFormTemplate(string name)
288 | {
289 | return (T)Template.FindName(name, this);
290 | }
291 | #endregion
292 |
293 | #region SetupVisualPropertes
294 | protected void SetupVisualPropertes(double TargetThickness, bool IsEditable)
295 | {
296 | Visibility IsCornerVisibe = IsEditable ? Visibility.Visible : Visibility.Collapsed;
297 |
298 | double ActualMargin = (CornerWidth - TargetThickness) / 2.0;
299 |
300 | MainGrid.Margin = new Thickness(0 - ActualMargin);
301 |
302 | foreach (CustomThumb item in MainGrid.Children)
303 | {
304 | if (item != null)
305 | {
306 | item.BorderThickness = new Thickness(TargetThickness);
307 |
308 | if (item.DragDirection == DragDirection.MiddleCenter)
309 | {
310 | item.Margin = new Thickness(ActualMargin);
311 | }
312 | else
313 | {
314 | item.Visibility = IsCornerVisibe;
315 | }
316 | }
317 | }
318 | }
319 | #endregion
320 |
321 | #endregion
322 |
323 | #region DragCompletedEvent
324 |
325 | public static readonly RoutedEvent DragChangingEvent
326 | = EventManager.RegisterRoutedEvent("DragChangingEvent", RoutingStrategy.Bubble, typeof(DragChangedEventHandler), typeof(DragHelperBase));
327 |
328 | public event DragChangedEventHandler DragChanging
329 | {
330 | add
331 | {
332 | AddHandler(DragChangingEvent, value);
333 | }
334 | remove
335 | {
336 | RemoveHandler(DragChangingEvent, value);
337 | }
338 | }
339 |
340 | public static readonly RoutedEvent DragCompletedEvent
341 | = EventManager.RegisterRoutedEvent("DragCompletedEvent", RoutingStrategy.Bubble, typeof(DragChangedEventHandler), typeof(DragHelperBase));
342 |
343 | public event DragChangedEventHandler DragCompleted
344 | {
345 | add
346 | {
347 | AddHandler(DragCompletedEvent, value);
348 | }
349 | remove
350 | {
351 | RemoveHandler(DragCompletedEvent, value);
352 | }
353 | }
354 | #endregion
355 |
356 | #region Drag Event Handler
357 | private void OnDragDelta(object sender, DragDeltaEventArgs e)
358 | {
359 | if(!GetTargetIsEditable())
360 | {
361 | e.Handled = true;
362 | return;
363 | }
364 |
365 | CustomThumb thumb = e.OriginalSource as CustomThumb;
366 |
367 | if (thumb == null)
368 | {
369 | return;
370 | }
371 |
372 | double VerticalChange = e.VerticalChange;
373 | double HorizontalChange = e.HorizontalChange;
374 |
375 | Rect NewBound = Rect.Empty;
376 |
377 | if (thumb.DragDirection == DragDirection.MiddleCenter)
378 | {
379 | NewBound = DragElement(HorizontalChange, VerticalChange);
380 | }
381 | else
382 | {
383 | NewBound = ResizeElement(thumb, HorizontalChange, VerticalChange);
384 | }
385 |
386 | RaisenDragChangingEvent(NewBound);
387 | SetTargetActualBound(NewBound);
388 |
389 | e.Handled = true;
390 | }
391 |
392 | private void OnDragCompleted(object sender, RoutedEventArgs e)
393 | {
394 | Rect NewBound = new Rect
395 | {
396 | Y = Canvas.GetTop(this),
397 | X = Canvas.GetLeft(this),
398 | Width = this.ActualWidth,
399 | Height = this.ActualHeight
400 | };
401 |
402 | RaisenDragCompletedEvent(NewBound);
403 |
404 | e.Handled = true;
405 | }
406 | #endregion
407 | }
408 | }
409 |
--------------------------------------------------------------------------------
/UICommon/Controls/DragHelper/DragHelperBaseStyle.xaml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
10 |
11 |
12 |
13 |
32 |
33 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
88 |
89 |
--------------------------------------------------------------------------------
/UICommon/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Windows;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 |
6 | // 有关程序集的一般信息由以下
7 | // 控制。更改这些特性值可修改
8 | // 与程序集关联的信息。
9 | [assembly: AssemblyTitle("UICommon")]
10 | [assembly: AssemblyDescription("")]
11 | [assembly: AssemblyConfiguration("")]
12 | [assembly: AssemblyCompany("")]
13 | [assembly: AssemblyProduct("UICommon")]
14 | [assembly: AssemblyCopyright("Copyright © 2016")]
15 | [assembly: AssemblyTrademark("")]
16 | [assembly: AssemblyCulture("")]
17 |
18 | //将 ComVisible 设置为 false 将使此程序集中的类型
19 | //对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
20 | //请将此类型的 ComVisible 特性设置为 true。
21 | [assembly: ComVisible(false)]
22 |
23 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
24 | [assembly: Guid("fc3fac70-a4bb-45ef-b7ca-4a74368e5d81")]
25 |
26 | // 程序集的版本信息由下列四个值组成:
27 | //
28 | // 主版本
29 | // 次版本
30 | // 生成号
31 | // 修订号
32 | //
33 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
34 | // 方法是按如下所示使用“*”: :
35 | // [assembly: AssemblyVersion("1.0.*")]
36 | [assembly: AssemblyVersion("1.0.0.0")]
37 | [assembly: AssemblyFileVersion("1.0.0.0")]
38 |
39 |
40 | [assembly: ThemeInfo(
41 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置
42 | //(当资源未在页面
43 | //或应用程序资源字典中找到时使用)
44 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
45 | //(当资源未在页面
46 | //、应用程序或任何主题专用资源字典中找到时使用)
47 | )]
48 |
--------------------------------------------------------------------------------
/UICommon/Themes/Generic.xaml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/UICommon/UICommon.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {FC3FAC70-A4BB-45EF-B7CA-4A74368E5D81}
8 | Library
9 | Properties
10 | UICommon
11 | UICommon
12 | v4.6
13 | 512
14 |
15 |
16 | true
17 | full
18 | false
19 | bin\Debug\
20 | DEBUG;TRACE
21 | prompt
22 | 4
23 |
24 |
25 | pdbonly
26 | true
27 | bin\Release\
28 | TRACE
29 | prompt
30 | 4
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | MSBuild:Compile
57 | Designer
58 |
59 |
60 | MSBuild:Compile
61 | Designer
62 |
63 |
64 |
65 |
72 |
--------------------------------------------------------------------------------
/删除目标文件(用于打包备份).bat:
--------------------------------------------------------------------------------
1 | dir /ad /b > sdk.txt
2 |
3 | del /f /s /q .\*.sdf
4 |
5 | for /r %%a in (obj bin x64 x86 ipch Debug) do rd /s /q "%%a"
6 |
7 | @PAUSE
8 | del sdk.txt
9 |
--------------------------------------------------------------------------------