11using System ;
2+ using System . ComponentModel ;
23using System . Diagnostics ;
34using System . Windows ;
45using 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}
0 commit comments