Repository navigation
Expand file tree
/
Copy pathCentreInParentCommand.cs
More file actions
163 lines (126 loc) · 5.28 KB
/
Copy pathCentreInParentCommand.cs
File metadata and controls
163 lines (126 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
57
58
59
60
61
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;
namespace DesignerControlHelper
{
internal sealed class CentreInParentCommand
{
public const int CommandId = 0x0100;
public static readonly Guid CommandSet = new Guid("d83a21fc-5c40-49f1-9596-7a3bbe48d31e");
private readonly AsyncPackage package;
private CentreInParentCommand(AsyncPackage package, OleMenuCommandService commandService)
{
this.package = package;
CommandID CommandID = new CommandID(CommandSet, CommandId);
MenuCommand Command = new MenuCommand(this.Execute, CommandID);
commandService.AddCommand(Command);
}
public static CentreInParentCommand Instance { get; private set; }
public static async Task InitializeAsync(AsyncPackage package)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
OleMenuCommandService CommandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
if (CommandService != null)
{
Instance = new CentreInParentCommand(package, CommandService);
}
}
private void Execute(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
object SelectedControl = this.GetSelectedControl();
if (SelectedControl != null)
{
this.CentreControl(SelectedControl);
}
}
private object GetSelectedControl()
{
object SelectedControl = null;
IVsMonitorSelection Monitor = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;
if (Monitor != null)
{
SelectedControl = this.GetSelection(Monitor);
}
return SelectedControl;
}
private object GetSelection(IVsMonitorSelection Monitor)
{
object SelectedControl = null;
IntPtr Hierarchy = IntPtr.Zero;
IntPtr ContainerPointer = IntPtr.Zero;
try
{
uint ItemID;
IVsMultiItemSelect MultiSelection;
int Result = Monitor.GetCurrentSelection(out Hierarchy, out ItemID, out MultiSelection, out ContainerPointer);
if (Result == 0 && ContainerPointer != IntPtr.Zero)
{
ISelectionContainer Container = Marshal.GetObjectForIUnknown(ContainerPointer) as ISelectionContainer;
if (Container != null)
{
SelectedControl = this.GetContainerSelection(Container);
}
}
}
finally
{
if (ContainerPointer != IntPtr.Zero)
{
Marshal.Release(ContainerPointer);
}
if (Hierarchy != IntPtr.Zero)
{
Marshal.Release(Hierarchy);
}
}
return SelectedControl;
}
private object GetContainerSelection(ISelectionContainer Container)
{
object SelectedControl = null;
uint Count;
Container.CountObjects(1, out Count);
if (Count == 1)
{
object[] SelectedObjects = new object[1];
Container.GetObjects(1, 1, SelectedObjects);
SelectedControl = SelectedObjects[0];
}
return SelectedControl;
}
private void CentreControl(object SelectedControl)
{
PropertyDescriptorCollection Properties = TypeDescriptor.GetProperties(SelectedControl);
PropertyDescriptor ParentProperty = Properties["Parent"];
PropertyDescriptor SizeProperty = Properties["Size"];
PropertyDescriptor LocationProperty = Properties["Location"];
if (ParentProperty != null && SizeProperty != null && LocationProperty != null)
{
object Parent = ParentProperty.GetValue(SelectedControl);
if (Parent != null)
{
this.CentreWithinParent(SelectedControl, Parent, SizeProperty, LocationProperty);
}
}
}
private void CentreWithinParent(object SelectedControl, object Parent, PropertyDescriptor SizeProperty, PropertyDescriptor LocationProperty)
{
PropertyDescriptor ParentSizeProperty = TypeDescriptor.GetProperties(Parent)["ClientSize"];
if (ParentSizeProperty != null)
{
Size ParentSize = (Size)ParentSizeProperty.GetValue(Parent);
Size ControlSize = (Size)SizeProperty.GetValue(SelectedControl);
Point Location = (Point)LocationProperty.GetValue(SelectedControl);
Location.X = (ParentSize.Width - ControlSize.Width) / 2;
LocationProperty.SetValue(SelectedControl, Location);
}
}
}
}