Skip to content

Commit 4332911

Browse files
changed a few ways it updates, added a checkbox for showing the taskbar
1 parent 5e819d1 commit 4332911

File tree

3 files changed

+102
-69
lines changed

3 files changed

+102
-69
lines changed

TaskbarCustomizer/MainWindow.xaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
mc:Ignorable="d"
88
Title="TaskbarCustomizer Settings" Height="350" Width="525" WindowState="Normal" WindowStyle="SingleBorderWindow" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing" StateChanged="Window_StateChanged">
99
<Grid>
10-
<Frame Content="Show/Hide Elements" HorizontalAlignment="Left" Height="101" Margin="10,81,0,0" VerticalAlignment="Top" Width="156" />
11-
<CheckBox x:Name="chkHideStart" Content="Hide Start" HorizontalAlignment="Left" Margin="21,101,0,0" VerticalAlignment="Top" Height="17" Width="145" Checked="chkHideStart_Checked" Unchecked="chkHideStart_Unchecked" />
12-
<CheckBox x:Name="chkHideShowDesk" Content="Hide Show Desktop" HorizontalAlignment="Left" Margin="21,117,0,0" VerticalAlignment="Top" Height="17" Width="145" Checked="chkHideShowDesk_Checked" Unchecked="chkHideShowDesk_Unchecked" />
13-
<Label Content="Taskbar Width:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" />
14-
<Slider x:Name="sliderTaskWidth" HorizontalAlignment="Left" Minimum="600" Maximum="800" Width="200" Margin="103,16,0,0" VerticalAlignment="Top" ValueChanged="sliderTaskWidth_ValueChanged" />
15-
<Label Content="Taskbar Opacity:" HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top" />
16-
<Slider x:Name="sliderTaskOpacity" HorizontalAlignment="Left" Minimum="0" Maximum="255" Width="200" Margin="103,42,0,0" VerticalAlignment="Top" ValueChanged="sliderTaskOpacity_ValueChanged" />
10+
<Label Content="Dummy Taskbar Settings" FontWeight="Bold" FontSize="16" HorizontalAlignment="Left" Margin="8,-2,0,0" VerticalAlignment="Top" />
11+
<Label Content="Width:" HorizontalAlignment="Left" Margin="21,19,0,0" VerticalAlignment="Top" />
12+
<Slider x:Name="sliderTaskWidth" HorizontalAlignment="Left" Minimum="600" Maximum="800" Width="200" Margin="78,25,0,0" VerticalAlignment="Top" ValueChanged="sliderTaskWidth_ValueChanged" />
13+
<Label Content="Opacity:" HorizontalAlignment="Left" Margin="21,40,0,0" VerticalAlignment="Top" />
14+
<Slider x:Name="sliderTaskOpacity" HorizontalAlignment="Left" Minimum="0" Maximum="255" Width="200" Margin="78,46,0,0" VerticalAlignment="Top" ValueChanged="sliderTaskOpacity_ValueChanged" />
15+
<CheckBox x:Name="chkTaskbarVisible" Content="Show Taskbar" HorizontalAlignment="Left" Margin="26,97,0,0" VerticalAlignment="Top" Click="chkTaskbarVisible_Click" />
16+
<Label Content="Show/Hide Elements" FontWeight="Bold" FontSize="16" HorizontalAlignment="Left" Margin="8,72,0,0" VerticalAlignment="Top" />
17+
<CheckBox x:Name="chkHideStart" Content="Hide Start" HorizontalAlignment="Left" Margin="26,113,0,0" VerticalAlignment="Top" Height="17" Width="145" Checked="chkHideStart_Checked" Unchecked="chkHideStart_Unchecked" />
18+
<CheckBox x:Name="chkHideShowDesk" Content="Hide Show Desktop" HorizontalAlignment="Left" Margin="26,129,0,0" VerticalAlignment="Top" Height="17" Width="145" Checked="chkHideShowDesk_Checked" Unchecked="chkHideShowDesk_Unchecked" />
19+
<Button x:Name="btnStart" Content="Start" Margin="434,276,10,10" Click="btnStart_Click" />
20+
<Button x:Name="btnStop" Content="Stop" Margin="434,276,10,10" Visibility="Hidden" Click="btnStop_Click" />
1721
</Grid>
1822
</Window>

TaskbarCustomizer/MainWindow.xaml.cs

Lines changed: 88 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Diagnostics;
34
using System.Windows;
45
using System.Windows.Interop;
@@ -12,6 +13,8 @@ namespace TaskbarCustomizer {
1213
/// Interaction logic for MainWindow.xaml
1314
/// </summary>
1415
public partial class MainWindow : Window {
16+
private bool _running = false;
17+
private BackgroundWorker _bgWorker;
1518
private System.Windows.Forms.NotifyIcon _trayIcon;
1619

1720
private TaskbarElement _taskbar;
@@ -27,9 +30,6 @@ public partial class MainWindow : Window {
2730

2831
private int _taskBarWidth => (int)sliderTaskWidth.Value;
2932

30-
private IntPtr _winHook;
31-
private Utility.WinEventDelegate procDelegate;
32-
3333
public MainWindow() {
3434
InitializeComponent();
3535

@@ -71,35 +71,39 @@ public MainWindow() {
7171
_dummyTaskbar.ShowInTaskbar = false;
7272
_dummyTaskbar.Hide();
7373

74-
// create wineventhook
75-
procDelegate = new Utility.WinEventDelegate(WinEventProc);
76-
}
77-
78-
private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) {
79-
if (eventType == 0x0008 || eventType == 0x0009 || eventType == 0x0005 || eventType == 0x0003 || eventType == 0x0010 || eventType == 0x0011 || eventType == 0x8005) {
80-
applyStyle();
81-
System.Threading.Thread.Sleep(80);
82-
}
74+
// set up background worker
75+
_bgWorker = new BackgroundWorker {
76+
WorkerSupportsCancellation = true
77+
};
8378

84-
Debug.WriteLine(eventType);
79+
_bgWorker.DoWork += _bgWorker_DoWork;
8580
}
8681

87-
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
88-
if (msg == Utility.WM_DWMCOLORIZATIONCOLORCHANGED) {
89-
System.Threading.Thread.Sleep(10);
82+
private void _bgWorker_DoWork(object sender, DoWorkEventArgs e) {
83+
while (_running) {
84+
Dispatcher.Invoke(() => {
85+
applyStyle();
86+
});
9087

91-
// make taskbar transparent
92-
_taskbar.AccentPolicy.AccentState = Helpers.Utility.AccentState.ACCENT_INVALID_STATE;
93-
_taskbar.ApplyAccentPolicy();
88+
System.Threading.Thread.Sleep(100);
9489
}
9590

96-
return IntPtr.Zero;
91+
Dispatcher.Invoke(() => {
92+
resetStyle();
93+
});
9794
}
9895

9996
private void applyStyle() {
10097
// make taskbar transparent
101-
_taskbar.AccentPolicy.AccentState = Helpers.Utility.AccentState.ACCENT_INVALID_STATE;
102-
_taskbar.ApplyAccentPolicy();
98+
if (chkTaskbarVisible.IsChecked == true) {
99+
// show the taskbar
100+
_taskbar.AccentPolicy.AccentState = Utility.AccentState.ACCENT_DISABLED;
101+
_taskbar.ApplyAccentPolicy();
102+
} else {
103+
// hide the taskbar
104+
_taskbar.AccentPolicy.AccentState = Utility.AccentState.ACCENT_INVALID_STATE;
105+
_taskbar.ApplyAccentPolicy();
106+
}
103107

104108
// make sure the dummy taskbar maintains position
105109
_dummyTaskbar.Top = _taskbar.Top;
@@ -126,12 +130,36 @@ private void applyStyle() {
126130
// move the start menu into the correct position
127131
_startMenu.MoveElement((int)_dummyTaskbar.Left, (int)_dummyTaskbar.Height);
128132
_cortanaSearchMenu.MoveElement((int)_dummyTaskbar.Left, (int)_dummyTaskbar.Height);
129-
//_networkMenu.MoveElement((int)_dummyTaskbar.Left + (int)_dummyTaskbar.Width - _networkMenu.Width, (int)_dummyTaskbar.Height);
130133

131134
// move the tray icon container into position
132135
_trayIconContainer.MoveElement((int)_dummyTaskbar.Left + (int)_dummyTaskbar.Width - _trayIconContainer.Width);
136+
}
133137

134-
//System.Threading.Thread.Sleep(10);
138+
private void resetStyle() {
139+
// get the offsets of buttons that may or may not be visible
140+
int offset = (_startButton.IsElementVisible() ? _startButton.Width : 0) +
141+
(_cortanaButton.IsElementVisible() ? _cortanaButton.Width : 0);
142+
143+
// fix the taskbar opacity
144+
_taskbar.AccentPolicy.AccentState = Utility.AccentState.ACCENT_DISABLED;
145+
_taskbar.ApplyAccentPolicy();
146+
147+
// return things back to normal
148+
_startButton.ShowElement();
149+
_startButton.MoveElement(0);
150+
151+
_startMenu.MoveElement(0);
152+
153+
if (_cortanaButton.IsElementVisible())
154+
_cortanaButton.MoveElement(offset - _cortanaButton.Width);
155+
156+
_showDesktopButton.ShowElement();
157+
_mainAppContainer.MoveElement(offset);
158+
_mainAppContainer.ResizeElement(_taskbar.Width - _trayIconContainer.Width - offset);
159+
_trayIconContainer.MoveElement(_taskbar.Width - _trayIconContainer.Width);
160+
161+
// get rid of the system tray icon
162+
_trayIcon.Dispose();
135163
}
136164

137165
private void chkHideStart_Checked(object sender, RoutedEventArgs e) {
@@ -151,59 +179,28 @@ private void chkHideShowDesk_Unchecked(object sender, RoutedEventArgs e) {
151179
}
152180

153181
private void Window_Loaded(object sender, RoutedEventArgs e) {
154-
_dummyTaskbar.Show();
182+
_dummyTaskbar?.Show();
155183

156184
this.Focus();
157-
158-
applyStyle();
159-
160-
IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
161-
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
162-
mainWindowSrc.AddHook(WndProc);
163-
164-
_winHook = Utility.SetWinEventHook(Utility.EVENT_MIN, Utility.EVENT_MAX, IntPtr.Zero, procDelegate, 0, 0, Utility.WINEVENT_OUTOFCONTEXT);
165-
166-
sliderTaskWidth.Value = sliderTaskWidth.Maximum - (sliderTaskWidth.Maximum / 2);
167-
sliderTaskOpacity.Value = sliderTaskOpacity.Maximum / 2;
168185
}
169186

170-
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
171-
// get the offsets of buttons that may or may not be visible
172-
int offset = (_startButton.IsElementVisible() ? _startButton.Width : 0) +
173-
(_cortanaButton.IsElementVisible() ? _cortanaButton.Width : 0);
174-
175-
Utility.UnhookWinEvent(_winHook);
176-
177-
// fix the taskbar
178-
_taskbar.AccentPolicy.AccentState = Helpers.Utility.AccentState.ACCENT_DISABLED;
179-
_taskbar.ApplyAccentPolicy();
187+
private void Window_Closing(object sender, CancelEventArgs e) {
188+
// stop the background worker
189+
_running = false;
180190

191+
// kill the dummy taskbar
181192
if (_dummyTaskbar != null)
182193
_dummyTaskbar.Close();
183194

184-
// return things back to normal
185-
_startButton.ShowElement();
186-
_startButton.MoveElement(0);
187-
_startMenu.MoveElement(0);
188-
189-
if (_cortanaButton.IsElementVisible())
190-
_cortanaButton.MoveElement(offset - _cortanaButton.Width);
191-
192-
_showDesktopButton.ShowElement();
193-
_mainAppContainer.MoveElement(offset);
194-
_mainAppContainer.ResizeElement(_taskbar.Width - _trayIconContainer.Width - offset);
195-
_trayIconContainer.MoveElement(_taskbar.Width - _trayIconContainer.Width);
196-
197-
// get rid of the system tray icon
198-
_trayIcon.Dispose();
195+
resetStyle();
199196
}
200197

201198
private void sliderTaskOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
202199
_dummyTaskbar.Background = new SolidColorBrush(Color.FromArgb((byte)sliderTaskOpacity.Value, 0, 0, 0));
203200
}
204201

205202
private void sliderTaskWidth_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
206-
if (this.Visibility == Visibility.Visible)
203+
if (_dummyTaskbar?.Visibility == Visibility.Visible)
207204
applyStyle();
208205
}
209206

@@ -214,5 +211,34 @@ private void Window_StateChanged(object sender, EventArgs e) {
214211
return;
215212
}
216213
}
214+
215+
private void btnStart_Click(object sender, RoutedEventArgs e) {
216+
if (!_bgWorker.IsBusy) {
217+
_running = true;
218+
_bgWorker.RunWorkerAsync();
219+
}
220+
221+
btnStart.Visibility = Visibility.Hidden;
222+
btnStop.Visibility = Visibility.Visible;
223+
}
224+
225+
private void btnStop_Click(object sender, RoutedEventArgs e) {
226+
_running = false;
227+
228+
btnStart.Visibility = Visibility.Visible;
229+
btnStop.Visibility = Visibility.Hidden;
230+
}
231+
232+
private void chkTaskbarVisible_Click(object sender, RoutedEventArgs e) {
233+
if (chkTaskbarVisible.IsChecked == true) {
234+
// show the taskbar
235+
_taskbar.AccentPolicy.AccentState = Utility.AccentState.ACCENT_DISABLED;
236+
_taskbar.ApplyAccentPolicy();
237+
} else {
238+
// // hide the taskbar
239+
_taskbar.AccentPolicy.AccentState = Utility.AccentState.ACCENT_INVALID_STATE;
240+
_taskbar.ApplyAccentPolicy();
241+
}
242+
}
217243
}
218244
}

TaskbarCustomizer/TaskbarCustomizer.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
<ErrorReport>prompt</ErrorReport>
4949
<WarningLevel>4</WarningLevel>
5050
</PropertyGroup>
51+
<PropertyGroup>
52+
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
53+
</PropertyGroup>
5154
<ItemGroup>
5255
<Reference Include="System" />
5356
<Reference Include="System.Data" />

0 commit comments

Comments
 (0)