-
Notifications
You must be signed in to change notification settings - Fork 71
/
TitleBarTabs.cs
998 lines (836 loc) · 30.5 KB
/
TitleBarTabs.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.Taskbar;
using Win32Interop.Enums;
using Win32Interop.Methods;
using Win32Interop.Structs;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;
namespace EasyTabs
{
/// <summary>
/// Base class that contains the functionality to render tabs within a WinForms application's title bar area. This is done through a borderless overlay
/// window (<see cref="_overlay" />) rendered on top of the non-client area at the top of this window. All an implementing class will need to do is set
/// the <see cref="TabRenderer" /> property and begin adding tabs to <see cref="Tabs" />.
/// </summary>
public abstract partial class TitleBarTabs : Form
{
/// <summary>
/// Event delegate for <see cref="TitleBarTabs.TabDeselecting" /> and <see cref="TitleBarTabs.TabSelecting" /> that allows subscribers to cancel the
/// event and keep it from proceeding.
/// </summary>
/// <param name="sender">Object for which this event was raised.</param>
/// <param name="e">Data associated with the event.</param>
public delegate void TitleBarTabCancelEventHandler(object sender, TitleBarTabCancelEventArgs e);
/// <summary>Event delegate for <see cref="TitleBarTabs.TabSelected" /> and <see cref="TitleBarTabs.TabDeselected" />.</summary>
/// <param name="sender">Object for which this event was raised.</param>
/// <param name="e">Data associated with the event.</param>
public delegate void TitleBarTabEventHandler(object sender, TitleBarTabEventArgs e);
/// <summary>Flag indicating whether or not each tab has an Aero Peek entry allowing the user to switch between tabs from the taskbar.</summary>
protected bool _aeroPeekEnabled = true;
/// <summary>Height of the non-client area at the top of the window.</summary>
protected int _nonClientAreaHeight;
/// <summary>Borderless window that is rendered over top of the non-client area of this window.</summary>
protected internal TitleBarTabsOverlay _overlay;
/// <summary>The preview images for each tab used to display each tab when Aero Peek is activated.</summary>
protected Dictionary<Form, Bitmap> _previews = new Dictionary<Form, Bitmap>();
/// <summary>
/// When switching between tabs, this keeps track of the tab that was previously active so that, when it is switched away from, we can generate a fresh
/// Aero Peek preview image for it.
/// </summary>
protected TitleBarTab _previousActiveTab = null;
/// <summary>Maintains the previous window state so that we can respond properly to maximize/restore events in <see cref="OnSizeChanged" />.</summary>
protected FormWindowState? _previousWindowState;
/// <summary>Class responsible for actually rendering the tabs in <see cref="_overlay" />.</summary>
protected BaseTabRenderer _tabRenderer;
/// <summary>List of tabs to display for this window.</summary>
protected ListWithEvents<TitleBarTab> _tabs = new ListWithEvents<TitleBarTab>();
/// <summary>Default constructor.</summary>
protected TitleBarTabs()
{
FormClosing += new FormClosingEventHandler(ApplicationFormClosing);
_previousWindowState = null;
ExitOnLastTabClose = true;
InitializeComponent();
SetWindowThemeAttributes(WTNCA.NODRAWCAPTION | WTNCA.NODRAWICON);
_tabs.CollectionModified += _tabs_CollectionModified;
// Set the window style so that we take care of painting the non-client area, a redraw is triggered when the size of the window changes, and the
// window itself has a transparent background color (otherwise the non-client area will simply be black when the window is maximized)
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
Tooltip = new ToolTip
{
AutoPopDelay = 5000,
AutomaticDelay = 500
};
ShowTooltips = true;
}
/// <summary>Flag indicating whether composition is enabled on the desktop.</summary>
internal bool IsCompositionEnabled
{
get
{
// This tests that the OS will support what we want to do. Will be false on Windows XP and earlier, as well as on Vista and 7 with Aero Glass
// disabled.
bool hasComposition;
Dwmapi.DwmIsCompositionEnabled(out hasComposition);
return hasComposition;
}
}
/// <summary>Flag indicating whether or not each tab has an Aero Peek entry allowing the user to switch between tabs from the taskbar.</summary>
public bool AeroPeekEnabled
{
get
{
return _aeroPeekEnabled;
}
set
{
_aeroPeekEnabled = value;
// Clear out any previously generate thumbnails if we are no longer enabled
if (!_aeroPeekEnabled)
{
foreach (TitleBarTab tab in Tabs)
{
TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(tab.Content);
}
_previews.Clear();
}
else
{
foreach (TitleBarTab tab in Tabs)
{
CreateThumbnailPreview(tab);
}
if (SelectedTab != null)
{
TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(SelectedTab.Content);
}
}
}
}
/// <summary>Flag indicating whether a tooltip should be shown when hovering over a tab.</summary>
public bool ShowTooltips
{
get;
set;
}
/// <summary>Tooltip UI element to show when hovering over a tab.</summary>
public ToolTip Tooltip
{
get;
set;
}
/// <summary>List of tabs to display for this window.</summary>
public ListWithEvents<TitleBarTab> Tabs
{
get
{
return _tabs;
}
}
/// <summary>The renderer to use when drawing the tabs.</summary>
public BaseTabRenderer TabRenderer
{
get
{
return _tabRenderer;
}
set
{
_tabRenderer = value;
SetFrameSize();
}
}
/// <summary>The tab that is currently selected by the user.</summary>
public TitleBarTab SelectedTab
{
get
{
return Tabs.FirstOrDefault((TitleBarTab t) => t.Active);
}
set
{
SelectedTabIndex = Tabs.IndexOf(value);
}
}
/// <summary>Gets or sets the index of the tab that is currently selected by the user.</summary>
public int SelectedTabIndex
{
get
{
return Tabs.FindIndex((TitleBarTab t) => t.Active);
}
set
{
TitleBarTab selectedTab = SelectedTab;
int selectedTabIndex = SelectedTabIndex;
if (selectedTab != null && selectedTabIndex != value)
{
// Raise the TabDeselecting event
TitleBarTabCancelEventArgs e = new TitleBarTabCancelEventArgs
{
Action = TabControlAction.Deselecting,
Tab = selectedTab,
TabIndex = selectedTabIndex
};
OnTabDeselecting(e);
// If the subscribers to the event canceled it, return before we do anything else
if (e.Cancel)
{
return;
}
selectedTab.Active = false;
// Raise the TabDeselected event
OnTabDeselected(
new TitleBarTabEventArgs
{
Tab = selectedTab,
TabIndex = selectedTabIndex,
Action = TabControlAction.Deselected
});
}
if (value != -1)
{
// Raise the TabSelecting event
TitleBarTabCancelEventArgs e = new TitleBarTabCancelEventArgs
{
Action = TabControlAction.Selecting,
Tab = Tabs[value],
TabIndex = value
};
OnTabSelecting(e);
// If the subscribers to the event canceled it, return before we do anything else
if (e.Cancel)
{
return;
}
Tabs[value].Active = true;
// Raise the TabSelected event
OnTabSelected(
new TitleBarTabEventArgs
{
Tab = Tabs[value],
TabIndex = value,
Action = TabControlAction.Selected
});
}
if (_overlay != null)
{
_overlay.Render();
}
}
}
/// <summary>Flag indicating whether the application itself should exit when the last tab is closed.</summary>
public bool ExitOnLastTabClose
{
get;
set;
}
/// <summary>Flag indicating whether we are in the process of closing the window.</summary>
public bool IsClosing
{
get;
set;
}
/// <summary>Application context under which this particular window runs.</summary>
public TitleBarTabsApplicationContext ApplicationContext
{
get;
internal set;
}
/// <summary>Height of the "glassed" area of the window's non-client area.</summary>
public int NonClientAreaHeight
{
get
{
return _nonClientAreaHeight;
}
}
/// <summary>Area of the screen in which tabs can be dropped for this window.</summary>
public Rectangle TabDropArea
{
get
{
return _overlay.TabDropArea;
}
}
/// <summary>Calls <see cref="Uxtheme.SetWindowThemeAttribute" /> to set various attributes on the window.</summary>
/// <param name="attributes">Attributes to set on the window.</param>
private void SetWindowThemeAttributes(WTNCA attributes)
{
WTA_OPTIONS options = new WTA_OPTIONS
{
dwFlags = attributes,
dwMask = WTNCA.VALIDBITS
};
// The SetWindowThemeAttribute API call takes care of everything
Uxtheme.SetWindowThemeAttribute(Handle, WINDOWTHEMEATTRIBUTETYPE.WTA_NONCLIENT, ref options, (uint) Marshal.SizeOf(typeof (WTA_OPTIONS)));
}
/// <summary>
/// Event handler that is invoked when the <see cref="Form.Load" /> event is fired. Instantiates <see cref="_overlay" /> and clears out the window's
/// caption.
/// </summary>
/// <param name="e">Arguments associated with the event.</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_overlay = TitleBarTabsOverlay.GetInstance(this);
if (TabRenderer != null)
{
_overlay.MouseMove += TabRenderer.Overlay_MouseMove;
_overlay.MouseUp += TabRenderer.Overlay_MouseUp;
_overlay.MouseDown += TabRenderer.Overlay_MouseDown;
}
}
/// <summary>
/// When the window's state (maximized, minimized, or restored) changes, this sets the size of the non-client area at the top of the window properly so
/// that the tabs can be displayed.
/// </summary>
protected void SetFrameSize()
{
if (TabRenderer == null || WindowState == FormWindowState.Minimized)
{
return;
}
int topPadding;
if (WindowState == FormWindowState.Maximized || TabRenderer.RendersEntireTitleBar)
{
topPadding = TabRenderer.TabHeight - TabRenderer.TopPadding - SystemInformation.CaptionHeight;
}
else
{
topPadding = TabRenderer.TabHeight - SystemInformation.CaptionHeight;
}
if (!TabRenderer.IsWindows10 && WindowState == FormWindowState.Maximized)
{
topPadding += 1;
}
Padding = new Padding(
Padding.Left, topPadding > 0
? topPadding
: 0, Padding.Right, Padding.Bottom);
if (!TabRenderer.IsWindows10)
{
// Set the margins and extend the frame into the client area
MARGINS margins = new MARGINS
{
cxLeftWidth = 1,
cxRightWidth = 1,
cyBottomHeight = 1,
cyTopHeight = topPadding > 0
? topPadding
: 0
};
Dwmapi.DwmExtendFrameIntoClientArea(Handle, ref margins);
}
_nonClientAreaHeight = SystemInformation.CaptionHeight + (topPadding > 0
? topPadding
: 0);
if (AeroPeekEnabled)
{
foreach (
TabbedThumbnail preview in
Tabs.Select(tab => TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(tab.Content)).Where(preview => preview != null))
{
preview.PeekOffset = new Vector(Padding.Left, Padding.Top - 1);
}
}
}
/// <summary>Event that is raised immediately prior to a tab being deselected (<see cref="TabDeselected" />).</summary>
public event TitleBarTabCancelEventHandler TabDeselecting;
/// <summary>Event that is raised after a tab has been deselected.</summary>
public event TitleBarTabEventHandler TabDeselected;
/// <summary>Event that is raised immediately prior to a tab being selected (<see cref="TabSelected" />).</summary>
public event TitleBarTabCancelEventHandler TabSelecting;
/// <summary>Event that is raised after a tab has been selected.</summary>
public event TitleBarTabEventHandler TabSelected;
/// <summary>Event that is raised after a tab has been clicked.</summary>
public event TitleBarTabEventHandler TabClicked;
/// <summary>
/// Callback that should be implemented by the inheriting class that will create a new <see cref="TitleBarTab" /> object when the add button is
/// clicked.
/// </summary>
/// <returns>A newly created tab.</returns>
public abstract TitleBarTab CreateTab();
/// <summary>Callback for the <see cref="TabClicked" /> event.</summary>
/// <param name="e">Arguments associated with the event.</param>
protected internal void OnTabClicked(TitleBarTabEventArgs e)
{
if (TabClicked != null)
{
TabClicked(this, e);
}
}
/// <summary>
/// Callback for the <see cref="TabDeselecting" /> event. Called when a <see cref="TitleBarTab" /> is in the process of losing focus. Grabs an image of
/// the tab's content to be used when Aero Peek is activated.
/// </summary>
/// <param name="e">Arguments associated with the event.</param>
protected void OnTabDeselecting(TitleBarTabCancelEventArgs e)
{
if (_previousActiveTab != null && AeroPeekEnabled)
{
UpdateTabThumbnail(_previousActiveTab);
}
if (TabDeselecting != null)
{
TabDeselecting(this, e);
}
}
/// <summary>Generate a new thumbnail image for <paramref name="tab" />.</summary>
/// <param name="tab">Tab that we need to generate a thumbnail for.</param>
protected void UpdateTabThumbnail(TitleBarTab tab)
{
TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(tab.Content);
if (preview == null)
{
return;
}
Bitmap bitmap = tab.GetImage();
preview.SetImage(bitmap);
// If we already had a preview image for the tab, dispose of it
if (_previews.ContainsKey(tab.Content) && _previews[tab.Content] != null)
{
_previews[tab.Content].Dispose();
}
_previews[tab.Content] = bitmap;
}
/// <summary>Callback for the <see cref="TabDeselected" /> event.</summary>
/// <param name="e">Arguments associated with the event.</param>
protected void OnTabDeselected(TitleBarTabEventArgs e)
{
if (TabDeselected != null)
{
TabDeselected(this, e);
}
}
/// <summary>Callback for the <see cref="TabSelecting" /> event.</summary>
/// <param name="e">Arguments associated with the event.</param>
protected void OnTabSelecting(TitleBarTabCancelEventArgs e)
{
ResizeTabContents(e.Tab);
if (TabSelecting != null)
{
TabSelecting(this, e);
}
}
/// <summary>
/// Callback for the <see cref="TabSelected" /> event. Called when a <see cref="TitleBarTab" /> gains focus. Sets the active window in Aero Peek via a
/// call to <see cref="TabbedThumbnailManager.SetActiveTab(Control)" />.
/// </summary>
/// <param name="e">Arguments associated with the event.</param>
protected void OnTabSelected(TitleBarTabEventArgs e)
{
if (SelectedTabIndex != -1 && _previews.ContainsKey(SelectedTab.Content) && AeroPeekEnabled)
{
TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(SelectedTab.Content);
}
_previousActiveTab = SelectedTab;
if (TabSelected != null)
{
TabSelected(this, e);
}
}
/// <summary>
/// Handler method that's called when Aero Peek needs to display a thumbnail for a <see cref="TitleBarTab" />; finds the preview bitmap generated in
/// <see cref="TabDeselecting" /> and returns that.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void preview_TabbedThumbnailBitmapRequested(object sender, TabbedThumbnailBitmapRequestedEventArgs e)
{
foreach (
TitleBarTab rdcWindow in Tabs.Where(rdcWindow => rdcWindow.Content.Handle == e.WindowHandle && _previews.ContainsKey(rdcWindow.Content)))
{
TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(rdcWindow.Content);
preview.SetImage(_previews[rdcWindow.Content]);
break;
}
}
/// <summary>
/// Callback for the <see cref="Control.ClientSizeChanged" /> event that resizes the <see cref="TitleBarTab.Content" /> form of the currently selected
/// tab when the size of the client area for this window changes.
/// </summary>
/// <param name="e">Arguments associated with the event.</param>
protected override void OnClientSizeChanged(EventArgs e)
{
base.OnClientSizeChanged(e);
ResizeTabContents();
}
/// <summary>Resizes the <see cref="TitleBarTab.Content" /> form of the <paramref name="tab" /> to match the size of the client area for this window.</summary>
/// <param name="tab">Tab whose <see cref="TitleBarTab.Content" /> form we should resize; if not specified, we default to
/// <see cref="SelectedTab" />.</param>
public void ResizeTabContents(TitleBarTab tab = null)
{
if (tab == null)
{
tab = SelectedTab;
}
if (tab != null)
{
tab.Content.Location = new Point(0, Padding.Top - 1);
tab.Content.Size = new Size(ClientRectangle.Width, ClientRectangle.Height - Padding.Top + 1);
}
}
/// <summary>Override of the handler for the paint background event that is left blank so that code is never executed.</summary>
/// <param name="e">Arguments associated with the event.</param>
protected override void OnPaintBackground(PaintEventArgs e)
{
}
/// <summary>Forwards a message received by <see cref="TitleBarTabsOverlay" /> to the underlying window.</summary>
/// <param name="m">Message received by the overlay.</param>
internal void ForwardMessage(ref Message m)
{
m.HWnd = Handle;
WndProc(ref m);
}
/// <summary>
/// Handler method that's called when the user clicks on an Aero Peek preview thumbnail. Finds the tab associated with the thumbnail and
/// focuses on it.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void preview_TabbedThumbnailActivated(object sender, TabbedThumbnailEventArgs e)
{
foreach (TitleBarTab tab in Tabs.Where(tab => tab.Content.Handle == e.WindowHandle))
{
SelectedTabIndex = Tabs.IndexOf(tab);
TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(tab.Content);
break;
}
// Restore the window if it was minimized
if (WindowState == FormWindowState.Minimized)
{
User32.ShowWindow(Handle, 3);
}
else
{
Focus();
}
}
/// <summary>
/// Handler method that's called when the user clicks the close button in an Aero Peek preview thumbnail. Finds the window associated with the thumbnail
/// and calls <see cref="Form.Close" /> on it.
/// </summary>
/// <param name="sender">Object from which this event originated.</param>
/// <param name="e">Arguments associated with this event.</param>
private void preview_TabbedThumbnailClosed(object sender, TabbedThumbnailEventArgs e)
{
foreach (TitleBarTab tab in Tabs.Where(tab => tab.Content.Handle == e.WindowHandle))
{
CloseTab(tab);
break;
}
}
/// <summary>Callback that is invoked whenever anything is added or removed from <see cref="Tabs" /> so that we can trigger a redraw of the tabs.</summary>
/// <param name="sender">Object for which this event was raised.</param>
/// <param name="e">Arguments associated with the event.</param>
private void _tabs_CollectionModified(object sender, ListModificationEventArgs e)
{
SetFrameSize();
if (e.Modification == ListModification.ItemAdded || e.Modification == ListModification.RangeAdded)
{
for (int i = 0; i < e.Count; i++)
{
TitleBarTab currentTab = Tabs[i + e.StartIndex];
currentTab.Content.TextChanged += Content_TextChanged;
currentTab.Closing += TitleBarTabs_Closing;
if (AeroPeekEnabled)
{
TaskbarManager.Instance.TabbedThumbnail.SetActiveTab(CreateThumbnailPreview(currentTab));
}
}
}
if (_overlay != null)
{
_overlay.Render(true);
}
}
/// <summary>
/// Creates a new thumbnail for <paramref name="tab" /> when the application is initially enabled for AeroPeek or when it is turned on sometime during
/// execution.
/// </summary>
/// <param name="tab">Tab that we are to create the thumbnail for.</param>
/// <returns>Thumbnail created for <paramref name="tab" />.</returns>
protected virtual TabbedThumbnail CreateThumbnailPreview(TitleBarTab tab)
{
TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(tab.Content);
if (preview != null)
{
TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(tab.Content);
}
preview = new TabbedThumbnail(Handle, tab.Content)
{
Title = tab.Content.Text,
Tooltip = tab.Content.Text
};
preview.SetWindowIcon((Icon)tab.Content.Icon.Clone());
preview.TabbedThumbnailActivated += preview_TabbedThumbnailActivated;
preview.TabbedThumbnailClosed += preview_TabbedThumbnailClosed;
preview.TabbedThumbnailBitmapRequested += preview_TabbedThumbnailBitmapRequested;
preview.PeekOffset = new Vector(Padding.Left, Padding.Top - 1);
TaskbarManager.Instance.TabbedThumbnail.AddThumbnailPreview(preview);
return preview;
}
/// <summary>
/// When a child tab updates its <see cref="Form.Icon"/> property, it should call this method to update the icon in the AeroPeek preview.
/// </summary>
/// <param name="tab">Tab whose icon was updated.</param>
/// <param name="icon">The new icon to use. If this is left as null, we use <see cref="Form.Icon"/> on <paramref name="tab"/>.</param>
public virtual void UpdateThumbnailPreviewIcon(TitleBarTab tab, Icon icon = null)
{
if (!AeroPeekEnabled)
{
return;
}
TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(tab.Content);
if (preview == null)
{
return;
}
if (icon == null)
{
icon = tab.Content.Icon;
}
preview.SetWindowIcon((Icon)icon.Clone());
}
/// <summary>
/// Event handler that is called when a tab's <see cref="Form.Text" /> property is changed, which re-renders the tab text and updates the title of the
/// Aero Peek preview.
/// </summary>
/// <param name="sender">Object from which this event originated (the <see cref="TitleBarTab.Content" /> object in this case).</param>
/// <param name="e">Arguments associated with the event.</param>
private void Content_TextChanged(object sender, EventArgs e)
{
if (AeroPeekEnabled)
{
TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview((Form) sender);
if (preview != null)
{
preview.Title = (sender as Form).Text;
}
}
if (_overlay != null)
{
_overlay.Render(true);
}
}
/// <summary>
/// Event handler that is called when a tab's <see cref="TitleBarTab.Closing" /> event is fired, which removes the tab from <see cref="Tabs" /> and
/// re-renders <see cref="_overlay" />.
/// </summary>
/// <param name="sender">Object from which this event originated (the <see cref="TitleBarTab" /> in this case).</param>
/// <param name="e">Arguments associated with the event.</param>
private void TitleBarTabs_Closing(object sender, CancelEventArgs e)
{
if (e.Cancel) return;
TitleBarTab tab = (TitleBarTab) sender;
CloseTab(tab);
if (!tab.Content.IsDisposed && AeroPeekEnabled)
{
TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(tab.Content);
}
if (_overlay != null)
{
_overlay.Render(true);
}
}
/// <summary>
/// Calls <see cref="TitleBarTabsOverlay.Render(bool)"/> on <see cref="_overlay"/> to force a redrawing of the tabs.
/// </summary>
public void RedrawTabs()
{
if (_overlay != null)
{
_overlay.Render(true);
}
}
/// <summary>
/// Overrides the <see cref="Control.SizeChanged" /> handler so that we can detect when the user has maximized or restored the window and adjust the size
/// of the non-client area accordingly.
/// </summary>
/// <param name="e">Arguments associated with the event.</param>
protected override void OnSizeChanged(EventArgs e)
{
// If no tab renderer has been set yet or the window state hasn't changed, don't do anything
if (_previousWindowState != null && WindowState != _previousWindowState.Value)
{
SetFrameSize();
}
_previousWindowState = WindowState;
base.OnSizeChanged(e);
}
/// <summary>Overrides the message processor for the window so that we can respond to windows events to render and manipulate the tabs properly.</summary>
/// <param name="m">Message received by the pump.</param>
protected override void WndProc(ref Message m)
{
bool callDwp = true;
switch ((WM) m.Msg)
{
// When the window is activated, set the size of the non-client area appropriately
case WM.WM_ACTIVATE:
if ((m.WParam.ToInt64() & 0x0000FFFF) != 0)
{
SetFrameSize();
ResizeTabContents();
m.Result = IntPtr.Zero;
}
break;
case WM.WM_NCHITTEST:
// Call the base message handler to see where the user clicked in the window
base.WndProc(ref m);
HT hitResult = (HT) m.Result.ToInt32();
// If they were over the minimize/maximize/close buttons or the system menu, let the message pass
if (!(hitResult == HT.HTCLOSE || hitResult == HT.HTMINBUTTON || hitResult == HT.HTMAXBUTTON || hitResult == HT.HTMENU ||
hitResult == HT.HTSYSMENU))
{
m.Result = new IntPtr((int) HitTest(m));
}
callDwp = false;
break;
// Catch the case where the user is clicking the minimize button and use this opportunity to update the AeroPeek thumbnail for the current tab
case WM.WM_NCLBUTTONDOWN:
if (((HT) m.WParam.ToInt32()) == HT.HTMINBUTTON && AeroPeekEnabled && SelectedTab != null)
{
UpdateTabThumbnail(SelectedTab);
}
break;
}
if (callDwp)
{
base.WndProc(ref m);
}
}
/// <summary>Calls <see cref="CreateTab" />, adds the resulting tab to the <see cref="Tabs" /> collection, and activates it.</summary>
public virtual void AddNewTab()
{
TitleBarTab newTab = CreateTab();
Tabs.Add(newTab);
ResizeTabContents(newTab);
SelectedTabIndex = _tabs.Count - 1;
}
/// <summary>Removes <paramref name="closingTab" /> from <see cref="Tabs" /> and selects the next applicable tab in the list.</summary>
/// <param name="closingTab">Tab that is being closed.</param>
protected virtual void CloseTab(TitleBarTab closingTab)
{
int removeIndex = Tabs.IndexOf(closingTab);
int selectedTabIndex = SelectedTabIndex;
Tabs.Remove(closingTab);
if (selectedTabIndex > removeIndex)
{
SelectedTabIndex = selectedTabIndex - 1;
}
else if (selectedTabIndex == removeIndex)
{
SelectedTabIndex = Math.Min(selectedTabIndex, Tabs.Count - 1);
}
else
{
SelectedTabIndex = selectedTabIndex;
}
if (_previews.ContainsKey(closingTab.Content))
{
_previews[closingTab.Content].Dispose();
_previews.Remove(closingTab.Content);
}
if (_previousActiveTab != null && closingTab.Content == _previousActiveTab.Content)
{
_previousActiveTab = null;
}
if (Tabs.Count == 0 && ExitOnLastTabClose)
{
Close();
}
}
private HT HitTest(Message m)
{
// Get the point that the user clicked
int lParam = (int) m.LParam;
Point point = new Point(lParam & 0xffff, lParam >> 16);
return HitTest(point, m.HWnd);
}
/// <summary>Called when a <see cref="WM.WM_NCHITTEST" /> message is received to see where in the non-client area the user clicked.</summary>
/// <param name="point">Screen location that we are to test.</param>
/// <param name="windowHandle">Handle to the window for which we are performing the test.</param>
/// <returns>One of the <see cref="HT" /> values, depending on where the user clicked.</returns>
private HT HitTest(Point point, IntPtr windowHandle)
{
RECT rect;
User32.GetWindowRect(windowHandle, out rect);
Rectangle area = new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
int row = 1;
int column = 1;
bool onResizeBorder = false;
// Determine if we are on the top or bottom border
if (point.Y >= area.Top && point.Y < area.Top + SystemInformation.VerticalResizeBorderThickness + _nonClientAreaHeight - 2)
{
onResizeBorder = point.Y < (area.Top + SystemInformation.VerticalResizeBorderThickness);
row = 0;
}
else if (point.Y < area.Bottom && point.Y > area.Bottom - SystemInformation.VerticalResizeBorderThickness)
{
row = 2;
}
// Determine if we are on the left border or the right border
if (point.X >= area.Left && point.X < area.Left + SystemInformation.HorizontalResizeBorderThickness)
{
column = 0;
}
else if (point.X < area.Right && point.X >= area.Right - SystemInformation.HorizontalResizeBorderThickness)
{
column = 2;
}
HT[,] hitTests =
{
{
onResizeBorder
? HT.HTTOPLEFT
: HT.HTLEFT,
onResizeBorder
? HT.HTTOP
: HT.HTCAPTION,
onResizeBorder
? HT.HTTOPRIGHT
: HT.HTRIGHT
},
{
HT.HTLEFT, HT.HTNOWHERE, HT.HTRIGHT
},
{
HT.HTBOTTOMLEFT, HT.HTBOTTOM,
HT.HTBOTTOMRIGHT
}
};
return hitTests[row, column];
}
private void ApplicationFormClosing(object sender, FormClosingEventArgs e)
{
foreach (TitleBarTab tab in Tabs.ToArray())
{
if (tab.Content != null)
{
bool formClosed = false;
tab.Content.FormClosed += (_, __) =>
{
formClosed = true;
};
Invoke(new Action(() =>
{
tab.Content.Close();
}));
if (!formClosed)
{
e.Cancel = true;
break;
}
}
}
}
}
}