Skip to content

Commit 27b3fad

Browse files
committed
TaskBar is aware of resizes.
1 parent 0cac6df commit 27b3fad

7 files changed

Lines changed: 55 additions & 11 deletions

File tree

.vs/TaskbarHook/v15/.suo

26.5 KB
Binary file not shown.

TaskbarHook/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
3333
// indem Sie "*" wie unten gezeigt eingeben:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.1.0.0")]
36+
[assembly: AssemblyFileVersion("1.1.0.0")]

TaskbarHook/TaskBarFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ public static class TaskBarFactory
2121
/// <exception cref="PlatformNotSupportedException">No TaskBar could be obtained.</exception>
2222
public static Taskbar GetTaskbar()
2323
{
24+
if (Taskbar.Instance != null)
25+
return Taskbar.Instance;
26+
2427
IntPtr taskbarHandle = TaskBarHandle;
2528

2629
if (taskbarHandle == IntPtr.Zero)
2730
throw new PlatformNotSupportedException($"The TaskBar cound't be obtained.");
2831

29-
return new Taskbar(taskbarHandle);
32+
Taskbar.CreateAndInitialize(taskbarHandle);
33+
return GetTaskbar();
3034
}
3135
}
3236
}

TaskbarHook/Taskbar.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,30 @@
44

55
namespace TaskbarHook
66
{
7-
public class Taskbar
7+
public class Taskbar : IDisposable
88
{
9-
public Taskbar(IntPtr handle)
10-
{
11-
Handle = handle;
9+
private static WinEventDelegate eventDelegate = new WinEventDelegate(TaskBarResizeEvent);
1210

13-
Initialize();
14-
}
11+
private Taskbar() { }
1512

16-
private void Initialize()
13+
internal static void CreateAndInitialize(IntPtr handle)
1714
{
18-
Rectangle = User32.GetWindowRectangle(Handle);
15+
Instance = new Taskbar();
16+
Instance.Handle = handle;
17+
Instance.Rectangle = User32.GetWindowRectangle(Instance.Handle);
18+
Instance.ResizeHandle = User32.RegisterWindowSizeChangeEvent(Instance.Handle, eventDelegate);
1919
}
2020

21+
internal static Taskbar Instance { get; private set; }
22+
2123
public IntPtr Handle { get; private set; }
2224

25+
private IntPtr ResizeHandle { get; set; }
26+
2327
public Rectangle Rectangle { get; private set; }
2428

29+
public Action SizeChanged { get; set; }
30+
2531
public async Task<TaskbarElement> AddToTaskbar() => await AddToTaskbar(Process.GetCurrentProcess());
2632

2733
public async Task<TaskbarElement> AddToTaskbar(Process process)
@@ -34,5 +40,9 @@ public async Task<TaskbarElement> AddToTaskbar(Process process)
3440

3541
return taskbarElement;
3642
}
43+
44+
private static void TaskBarResizeEvent(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) => Instance?.SizeChanged();
45+
46+
public void Dispose() => User32.UnRegisterWindowSizeChangeEvent(ResizeHandle);
3747
}
3848
}

TaskbarHook/TaskbarHook.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="Rectangle.cs" />
5353
<Compile Include="Taskbar.cs" />
5454
<Compile Include="User32.cs" />
55+
<Compile Include="WinEventDelegate.cs" />
5556
</ItemGroup>
5657
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5758
</Project>

TaskbarHook/User32.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace TaskbarHook
55
{
66
internal static class User32
77
{
8+
private const uint EVENT_SYSTEM_MOVESIZESTART = 0x000A;
9+
private const uint EVENT_SYSTEM_MOVESIZEEND = 0x000B;
10+
private const uint WINEVENT_OUTOFCONTEXT = 0;
11+
812
#region DLLImports
913
[DllImport("user32.dll", SetLastError = true)]
1014
private static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
@@ -26,6 +30,15 @@ internal static class User32
2630

2731
[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
2832
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
33+
34+
[DllImport("user32.dll")]
35+
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
36+
37+
[DllImport("user32.dll")]
38+
private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
39+
40+
[DllImport("user32.dll", SetLastError = true)]
41+
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
2942
#endregion
3043

3144
internal static bool SetWindowPosition(IntPtr handle, int x, int y) => SetWindowPos(handle, 0, x, y, 0, 0, 0x0001) != IntPtr.Zero;
@@ -38,6 +51,16 @@ internal static class User32
3851

3952
internal static bool SetVisibilityState(IntPtr handle, int state) => ShowWindow(handle, state);
4053

54+
internal static IntPtr RegisterWindowSizeChangeEvent(IntPtr handle, WinEventDelegate delegateCallback)
55+
{
56+
uint process, thread = 0;
57+
thread = GetWindowThreadProcessId(handle, out process);
58+
59+
return SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART, EVENT_SYSTEM_MOVESIZEEND, IntPtr.Zero, delegateCallback, process, thread, WINEVENT_OUTOFCONTEXT);
60+
}
61+
62+
internal static bool UnRegisterWindowSizeChangeEvent(IntPtr hoockHandle) => UnhookWinEvent(hoockHandle);
63+
4164
internal static IntPtr GetWindowParent(IntPtr handle) => GetParent(handle);
4265

4366
internal static IntPtr GetWindow(string className) => GetWindow(IntPtr.Zero, className);

TaskbarHook/WinEventDelegate.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using System;
2+
3+
namespace TaskbarHook
4+
{
5+
internal delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
6+
}

0 commit comments

Comments
 (0)