-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
HwndHost.cs
1209 lines (1040 loc) · 49.5 KB
/
HwndHost.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
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Input;
using System.Collections;
using MS.Win32;
using MS.Internal;
using MS.Internal.Interop;
using System.Security;
using Microsoft.Win32;
using System.Windows.Media;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Windows.Threading;
using System.Diagnostics.CodeAnalysis;
// Disable pragma warnings to enable PREsharp pragmas
#pragma warning disable 1634, 1691
namespace System.Windows.Interop
{
/// <summary>
/// The HwndHost class hosts an HWND inside of an Avalon tree.
/// </summary>
///<remarks> Subclassing requires unmanaged code permission</remarks>
public abstract class HwndHost : FrameworkElement, IDisposable, IWin32Window, IKeyboardInputSink
{
static HwndHost()
{
FocusableProperty.OverrideMetadata(typeof(HwndHost), new FrameworkPropertyMetadata(true));
HwndHost.DpiChangedEvent = Window.DpiChangedEvent.AddOwner(typeof(HwndHost));
}
/// <summary>
/// Constructs an instance of the HwndHost class.
/// </summary>
///<remarks> Not available in Internet zone</remarks>
protected HwndHost()
{
Initialize( false ) ;
}
internal HwndHost(bool fTrusted )
{
Initialize( fTrusted ) ;
}
/// <summary>
/// Because we own an HWND, we implement a finalizer to make sure that we destroy it.
/// </summary>
~HwndHost()
{
Dispose(false);
}
/// <summary>
/// Disposes this object.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// The Win32 handle of the hosted window.
/// </summary>
/// <remarks>
/// Callers must have UnmanagedCode permission to call this API.
/// </remarks>
public IntPtr Handle
{
get
{
return CriticalHandle;
}
}
/// <summary>
/// An event that is notified of all unhandled messages received
/// by the hosted window.
/// </summary>
public event HwndSourceHook MessageHook
{
add
{
if(_hooks == null)
{
_hooks = new ArrayList(8);
}
_hooks.Add(value);
}
remove
{
if(_hooks != null)
{
_hooks.Remove(value);
if(_hooks.Count == 0)
{
_hooks = null;
}
}
}
}
/// <summary>
/// This event is raised after the DPI of the screen on which the HwndHost is displayed, changes.
/// </summary>
public event DpiChangedEventHandler DpiChanged
{
add { AddHandler(HwndHost.DpiChangedEvent, value); }
remove { RemoveHandler(HwndHost.DpiChangedEvent, value); }
}
/// <summary>
/// RoutedEvent for when DPI of the screen the HwndHost is on, changes.
/// </summary>
public static readonly RoutedEvent DpiChangedEvent;
/// <summary>
/// </summary>
/// <param name="e"></param>
protected override void OnKeyUp(KeyEventArgs e)
{
MSG msg;
if (_fTrusted)
{
msg = ComponentDispatcher.UnsecureCurrentKeyboardMessage;
}
else
{
msg = ComponentDispatcher.CurrentKeyboardMessage;
}
ModifierKeys modifiers = HwndKeyboardInputProvider.GetSystemModifierKeys();
bool handled = ((IKeyboardInputSink)this).TranslateAccelerator(ref msg, modifiers);
if(handled)
e.Handled = handled;
base.OnKeyUp(e);
}
/// <summary>
/// OnDpiChanged is called when the DPI at which this HwndHost is rendered, changes.
/// </summary>
protected override void OnDpiChanged(DpiScale oldDpi, DpiScale newDpi)
{
RaiseEvent(new DpiChangedEventArgs(oldDpi, newDpi, HwndHost.DpiChangedEvent, this));
UpdateWindowPos();
}
/// <summary>
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
MSG msg;
if (_fTrusted)
{
msg = ComponentDispatcher.UnsecureCurrentKeyboardMessage;
}
else
{
msg = ComponentDispatcher.CurrentKeyboardMessage;
}
ModifierKeys modifiers = HwndKeyboardInputProvider.GetSystemModifierKeys();
bool handled = ((IKeyboardInputSink)this).TranslateAccelerator(ref msg, modifiers);
if(handled)
e.Handled = handled;
base.OnKeyDown(e);
}
#region IKeyboardInputSink
// General security note on the implementation pattern of this interface. In Dev10 it was chosen
// to expose the interface implementation for overriding to customers. We did so by keeping the
// explicit interface implementations (that do have the property of being hidden from the public
// contract, which limits IntelliSense on derived types like WebBrowser) while sticking protected
// virtuals next to them. Those virtuals contain our base implementation, while the explicit
// interface implementation methods do call trivially into the virtuals.
//
// This comment outlines the security rationale applied to those methods.
//
// <SecurityNote Name="IKeyboardInputSink_Implementation">
// The security attributes on the virtual methods within this region mirror the corresponding
// IKeyboardInputSink methods; customers can override those methods, so we insert a LinkDemand
// to encourage them to have a LinkDemand too (via FxCop).
/// <summary>
/// Registers a IKeyboardInputSink with the HwndSource in order
/// to retreive a unique IKeyboardInputSite for it.
/// </summary>
protected virtual IKeyboardInputSite RegisterKeyboardInputSinkCore(IKeyboardInputSink sink)
{
throw new InvalidOperationException(SR.HwndHostDoesNotSupportChildKeyboardSinks);
}
IKeyboardInputSite IKeyboardInputSink.RegisterKeyboardInputSink(IKeyboardInputSink sink)
{
return RegisterKeyboardInputSinkCore(sink);
}
/// <summary>
/// Gives the component a chance to process keyboard input.
/// Return value is true if handled, false if not. Components
/// will generally call a child component's TranslateAccelerator
/// if they can't handle the input themselves. The message must
/// either be WM_KEYDOWN or WM_SYSKEYDOWN. It is illegal to
/// modify the MSG structure, it's passed by reference only as
/// a performance optimization.
/// </summary>
protected virtual bool TranslateAcceleratorCore(ref MSG msg, ModifierKeys modifiers)
{
return false;
}
bool IKeyboardInputSink.TranslateAccelerator(ref MSG msg, ModifierKeys modifiers)
{
return TranslateAcceleratorCore(ref msg, modifiers);
}
/// <summary>
/// Set focus to the first or last tab stop (according to the
/// TraversalRequest). If it can't, because it has no tab stops,
/// the return value is false.
/// </summary>
protected virtual bool TabIntoCore(TraversalRequest request)
{
return false;
}
bool IKeyboardInputSink.TabInto(TraversalRequest request)
{
return TabIntoCore(request);
}
/// <summary>
/// The property should start with a null value. The component's
/// container will set this property to a non-null value before
/// any other methods are called. It may be set multiple times,
/// and should be set to null before disposal.
/// </summary>
IKeyboardInputSite IKeyboardInputSink.KeyboardInputSite { get; set; }
/// <summary>
/// This method is called whenever one of the component's
/// mnemonics is invoked. The message must either be WM_KEYDOWN
/// or WM_SYSKEYDOWN. It's illegal to modify the MSG structrure,
/// it's passed by reference only as a performance optimization.
/// If this component contains child components, the container
/// OnMnemonic will need to call the child's OnMnemonic method.
/// </summary>
protected virtual bool OnMnemonicCore(ref MSG msg, ModifierKeys modifiers)
{
return false;
}
bool IKeyboardInputSink.OnMnemonic(ref MSG msg, ModifierKeys modifiers)
{
return OnMnemonicCore(ref msg, modifiers);
}
/// <summary>
/// Gives the component a chance to process keyboard input messages
/// WM_CHAR, WM_SYSCHAR, WM_DEADCHAR or WM_SYSDEADCHAR before calling OnMnemonic.
/// Will return true if "handled" meaning don't pass it to OnMnemonic.
/// The message must be WM_CHAR, WM_SYSCHAR, WM_DEADCHAR or WM_SYSDEADCHAR.
/// It is illegal to modify the MSG structure, it's passed by reference
/// only as a performance optimization.
/// </summary>
protected virtual bool TranslateCharCore(ref MSG msg, ModifierKeys modifiers)
{
return false;
}
bool IKeyboardInputSink.TranslateChar(ref MSG msg, ModifierKeys modifiers)
{
return TranslateCharCore(ref msg, modifiers);
}
/// <summary>
/// This returns true if the sink, or a child of it, has focus. And false otherwise.
/// </summary>
protected virtual bool HasFocusWithinCore()
{
HandleRef hwndFocus = new HandleRef(this, UnsafeNativeMethods.GetFocus());
if (Handle != IntPtr.Zero && (hwndFocus.Handle == _hwnd.Handle || UnsafeNativeMethods.IsChild(_hwnd, hwndFocus)))
{
return true;
}
return false;
}
bool IKeyboardInputSink.HasFocusWithin()
{
return HasFocusWithinCore();
}
#endregion IKeyboardInputSink
/// <summary>
/// Updates the child window to reflect the state of this element.
/// </summary>
/// <remarks>
/// This includes the size of the window, the position of the
/// window, and the visibility of the window.
/// </remarks>
///<remarks> Not available in Internet zone</remarks>
public void UpdateWindowPos()
{
// Verify the thread has access to the context.
// VerifyAccess();
if (_isDisposed)
{
return;
}
// Position the child HWND where layout put it. To do this we
// have to get coordinates relative to the parent window.
PresentationSource source = null;
CompositionTarget vt = null;
if (( CriticalHandle != IntPtr.Zero) && IsVisible)
{
source = PresentationSource.CriticalFromVisual(this, false /* enable2DTo3DTransition */);
if(source != null)
{
vt = source.CompositionTarget;
}
}
if(vt != null && vt.RootVisual != null)
{
// Translate the layout information assigned to us from the co-ordinate
// space of this element, through the root visual, to the Win32 client
// co-ordinate space
NativeMethods.RECT rcClientRTLAdjusted = CalculateAssignedRC(source);
// Set the Win32 position for the child window.
//
// Note, we can't check the existing position because we use
// SWP_ASYNCWINDOWPOS, which means we could have pending position
// change requests that haven't been applied yet. If we need
// this functionality (to avoid the extra SetWindowPos calls),
// we'll have to track the last RECT we sent Win32 ourselves.
//
Rect rectClientRTLAdjusted = PointUtil.ToRect(rcClientRTLAdjusted);
OnWindowPositionChanged(rectClientRTLAdjusted);
// Show the window
// Based on Dwayne, the reason we also show/hide window in UpdateWindowPos is for the
// following kind of scenario: When applying RenderTransform to HwndHost, the hwnd
// will be left behind. Developer can workaround by hide the hwnd first using pinvoke.
// After the RenderTransform is applied to the HwndHost, call UpdateWindowPos to sync up
// the hwnd's location, size and visibility with WPF.
UnsafeNativeMethods.ShowWindowAsync(_hwnd, NativeMethods.SW_SHOW);
}
else
{
// For some reason we shouldn't be displayed: either we don't
// have a parent, or the parent no longer has a root visual,
// or we are marked as not being visible.
//
// Just hide the window to get it out of the way.
UnsafeNativeMethods.ShowWindowAsync(_hwnd, NativeMethods.SW_HIDE);
}
}
// Translate the layout information assigned to us from the co-ordinate
// space of this element, through the root visual, to the Win32 client
// co-ordinate space
private NativeMethods.RECT CalculateAssignedRC(PresentationSource source)
{
Rect rectElement = new Rect(RenderSize);
Rect rectRoot = PointUtil.ElementToRoot(rectElement, this, source);
Rect rectClient = PointUtil.RootToClient(rectRoot, source);
// Adjust for Right-To-Left oriented windows
IntPtr hwndParent = UnsafeNativeMethods.GetParent(_hwnd);
NativeMethods.RECT rcClient = PointUtil.FromRect(rectClient);
NativeMethods.RECT rcClientRTLAdjusted = PointUtil.AdjustForRightToLeft(rcClient, new HandleRef(null, hwndParent));
if (!CoreAppContextSwitches.DoNotUsePresentationDpiCapabilityTier2OrGreater)
{
//Adjust for differences in DPI between _hwnd and hwndParent
rcClientRTLAdjusted = AdjustRectForDpi(rcClientRTLAdjusted);
}
return rcClientRTLAdjusted;
}
/// <summary>
/// Gets the ratio of the DPI between the parent of <see cref="_hwnd"/>
/// and <see cref="_hwnd"/>. Normally, this ratio is 1.
/// </summary>
private double DpiParentToChildRatio
{
get
{
if (!_hasDpiAwarenessContextTransition) return 1;
DpiScale2 dpi = DpiUtil.GetWindowDpi(Handle, fallbackToNearestMonitorHeuristic: false);
DpiScale2 dpiParent = DpiUtil.GetWindowDpi(UnsafeNativeMethods.GetParent(_hwnd), fallbackToNearestMonitorHeuristic: false);
if (dpi == null || dpiParent == null)
{
// if DPI of the window can not be queried directly, then the platform
// is too old to support mixed mode DPI. The DPI ratio is 1.0
return 1.0d;
}
return dpiParent.DpiScaleX / dpi.DpiScaleX;
}
}
/// <summary>
/// Adjusts a rectangle to factor in the differences in DPI between
/// the parent of <see cref="_hwnd"/> and <see cref="_hwnd"/>
/// </summary>
/// <param name="rcRect">The rectangle to adjust</param>
/// <returns>The adjusted rectangle</returns>
private NativeMethods.RECT AdjustRectForDpi(NativeMethods.RECT rcRect)
{
if (_hasDpiAwarenessContextTransition)
{
double dpiRatio = DpiParentToChildRatio;
rcRect.left = (int)(rcRect.left / dpiRatio);
rcRect.top = (int)(rcRect.top / dpiRatio);
rcRect.right = (int)(rcRect.right / dpiRatio);
rcRect.bottom = (int)(rcRect.bottom / dpiRatio);
}
return rcRect;
}
/// <summary>
/// Disposes this object.
/// </summary>
/// <param name="disposing">
/// true if called from explisit Dispose; and we free all objects managed and un-managed.
/// false if called from the finalizer; and we free only un-managed objects.
/// </param>
/// <remarks>
/// Derived classes should override this if they have additional
/// cleanup to do. The base class implementation should be called.
/// Note that the calling thread must be the dispatcher thread.
/// If a window is being hosted, that window is destroyed.
/// </remarks>
protected virtual void Dispose(bool disposing)
{
if (_isDisposed == true)
{
return;
}
if(disposing)
{
// Verify the thread has access to the context.
#pragma warning suppress 6519
VerifyAccess();
// Remove our subclass. Even if this fails, it will be forcably removed
// when the window is destroyed.
if (_hwndSubclass != null)
{
// Check if it is trusted (WebOC and AddInHost), call CriticalDetach to avoid the Demand.
if (_fTrusted == true)
{
_hwndSubclass.CriticalDetach(false);
}
else
{
_hwndSubclass.RequestDetach(false);
}
_hwndSubclass = null;
}
// Drop the hooks so that they can be garbage-collected.
_hooks = null;
// We no longer need to know about the source changing.
PresentationSource.RemoveSourceChangedHandler(this, new SourceChangedEventHandler(OnSourceChanged));
}
if (_weakEventDispatcherShutdown != null) // Can be null if the static ctor failed ... see WebBrowser.
{
_weakEventDispatcherShutdown.Dispose();
_weakEventDispatcherShutdown = null;
}
DestroyWindow();
_isDisposed = true;
}
private void OnDispatcherShutdown(object sender, EventArgs e)
{
Dispose();
}
/// <summary>
/// Derived classes override this method to actually build the
/// window being hosted.
/// </summary>
/// <param name="hwndParent">
/// The parent HWND for the child window.
/// </param>
/// <returns>
/// The HWND handle to the child window that was created.
/// </returns>
/// <remarks>
/// The window that is returned must be a child window of the
/// specified parent window.
/// <para/>
/// In addition, the child window will only be subclassed if
/// the window is owned by the calling thread.
/// </remarks>
protected abstract HandleRef BuildWindowCore(HandleRef hwndParent);
/// <summary>
/// Derived classes override this method to destroy the
/// window being hosted.
/// </summary>
protected abstract void DestroyWindowCore(HandleRef hwnd);
/// <summary>
/// A protected override for accessing the window proc of the
/// hosted child window.
/// </summary>
///<remarks> Not available in Internet zone</remarks>
protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
DemandIfUntrusted();
switch ((WindowMessage)msg)
{
case WindowMessage.WM_NCDESTROY:
_hwnd = new HandleRef(null, IntPtr.Zero);
break;
// When layout happens, we first calculate the right size/location then call SetWindowPos.
// We only allow the changes that are coming from Avalon layout. The hwnd is not allowed to change by itself.
// So the size of the hwnd should always be RenderSize and the position be where layout puts it.
case WindowMessage.WM_WINDOWPOSCHANGING:
PresentationSource source = PresentationSource.CriticalFromVisual(this, false /* enable2DTo3DTransition */);
if (source != null)
{
// Get the rect assigned by layout to us.
NativeMethods.RECT assignedRC = CalculateAssignedRC(source);
// The lParam is a pointer to a WINDOWPOS structure
// that contains information about the size and
// position that the window is changing to. Note that
// modifying this structure during WM_WINDOWPOSCHANGING
// will change what happens to the window.
unsafe
{
NativeMethods.WINDOWPOS * windowPos = (NativeMethods.WINDOWPOS *)lParam;
// Always force the size of the window to be the
// size of our assigned rectangle. Note that we
// have to always clear the SWP_NOSIZE flag.
windowPos->cx = assignedRC.right - assignedRC.left;
windowPos->cy = assignedRC.bottom - assignedRC.top;
windowPos->flags &= ~NativeMethods.SWP_NOSIZE;
// Always force the position of the window to be
// the upper-left corner of our assigned rectangle.
// Note that we have to always clear the
// SWP_NOMOVE flag.
windowPos->x = assignedRC.left;
windowPos->y = assignedRC.top;
windowPos->flags &= ~NativeMethods.SWP_NOMOVE;
// Windows has an optimization to copy pixels
// around to reduce the amount of repainting
// needed when moving or resizing a window.
// Unfortunately, this is not compatible with WPF
// in many cases due to our use of DirectX for
// rendering from our rendering thread.
// To be safe, we disable this optimization and
// pay the cost of repainting.
windowPos->flags |= NativeMethods.SWP_NOCOPYBITS;
}
}
break;
case WindowMessage.WM_GETOBJECT:
handled = true;
return OnWmGetObject(wParam, lParam);
}
return IntPtr.Zero ;
}
#region Automation
/// <summary>
/// Creates AutomationPeer (<see cref="UIElement.OnCreateAutomationPeer"/>)
/// </summary>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new HwndHostAutomationPeer(this);
}
private IntPtr OnWmGetObject(IntPtr wparam, IntPtr lparam)
{
IntPtr result = IntPtr.Zero;
AutomationPeer containerPeer = UIElementAutomationPeer.CreatePeerForElement(this);
if (containerPeer != null)
{
// get the element proxy
IRawElementProviderSimple el = containerPeer.GetInteropChild();
result = AutomationInteropProvider.ReturnRawElementProvider(CriticalHandle, wparam, lparam, el);
}
return result;
}
#endregion Automation
// Make this protected virtual when enabling the WebOC code.
//NEEDS final signoff from the owning team.
/// <summary>
/// Called when the window rect changes. Subclasses can override this to
/// update child window's Rect using these new coordinates.
/// </summary>
/// <param name="rcBoundingBox"></param>
protected virtual void OnWindowPositionChanged(Rect rcBoundingBox)
{
if (_isDisposed)
{
return;
}
UnsafeNativeMethods.SetWindowPos(_hwnd,
new HandleRef(null, IntPtr.Zero),
(int)rcBoundingBox.X,
(int)rcBoundingBox.Y,
(int)rcBoundingBox.Width,
(int)rcBoundingBox.Height,
NativeMethods.SWP_ASYNCWINDOWPOS
| NativeMethods.SWP_NOZORDER
| NativeMethods.SWP_NOCOPYBITS
| NativeMethods.SWP_NOACTIVATE);
}
/// <summary>
/// Return the desired size of the HWND.
/// </summary>
/// <remarks>
/// HWNDs usually expect a very simplisitic layout model where
/// a window gets to be whatever size it wants to be. To respect
/// this we request the initial size that the window was created
/// at. A window created with a 0 dimension will adopt whatever
/// size the containing layout wants it to be. Layouts are free
/// to actually size the window to whatever they want, and the
/// child window will always be sized accordingly.
/// <para/>
/// Derived classes should only override this method if they
/// have special knowlege about the size the window wants to be.
/// Examples of such may be special HWND types like combo boxes.
/// In such cases, the base class must still be called, but the
/// return value can be changed appropriately.
/// </remarks>
///<remarks> Not available in Internet zone</remarks>
protected override Size MeasureOverride(Size constraint)
{
DemandIfUntrusted();
Size desiredSize = new Size(0,0);
// Measure to our desired size. If we have a 0-length dimension,
// the system will assume we don't care about that dimension.
if(CriticalHandle != IntPtr.Zero)
{
desiredSize.Width = Math.Min(_desiredSize.Width, constraint.Width);
desiredSize.Height = Math.Min(_desiredSize.Height, constraint.Height);
}
return desiredSize;
}
/// <summary>
/// GetDrawing - Returns the drawing content of this Visual.
/// </summary>
/// <remarks>
/// This returns a bitmap obtained by calling the PrintWindow Win32 API.
/// </remarks>
internal override DrawingGroup GetDrawing()
{
return GetDrawingHelper();
}
/// <summary>
/// Returns the bounding box of the content.
/// </summary>
internal override Rect GetContentBounds()
{
return new Rect(RenderSize);
}
private DrawingGroup GetDrawingHelper()
{
DrawingGroup drawingGroup = null;
if(Handle != IntPtr.Zero)
{
NativeMethods.RECT rc = new NativeMethods.RECT();
SafeNativeMethods.GetWindowRect(_hwnd, ref rc);
int width = rc.right - rc.left;
int height = rc.bottom - rc.top;
HandleRef hdcScreen = new HandleRef(this, UnsafeNativeMethods.GetDC(new HandleRef(this, IntPtr.Zero)));
if(hdcScreen.Handle != IntPtr.Zero)
{
HandleRef hdcBitmap = new HandleRef(this, IntPtr.Zero);
HandleRef hBitmap = new HandleRef(this, IntPtr.Zero);
try
{
hdcBitmap = new HandleRef(this, UnsafeNativeMethods.CriticalCreateCompatibleDC(hdcScreen));
if(hdcBitmap.Handle != IntPtr.Zero)
{
hBitmap = new HandleRef(this, UnsafeNativeMethods.CriticalCreateCompatibleBitmap(hdcScreen, width, height));
if(hBitmap.Handle != IntPtr.Zero)
{
// Select the bitmap into the DC so that we draw to it.
IntPtr hOldBitmap = UnsafeNativeMethods.CriticalSelectObject(hdcBitmap, hBitmap.Handle);
try
{
// Clear the bitmap to white (so we don't waste toner printing a black bitmap something fails).
NativeMethods.RECT rcPaint = new NativeMethods.RECT(0,0,width, height);
IntPtr hbrWhite = UnsafeNativeMethods.CriticalGetStockObject(NativeMethods.WHITE_BRUSH);
UnsafeNativeMethods.CriticalFillRect(hdcBitmap.Handle, ref rcPaint, hbrWhite);
// First try to use the PrintWindow API.
bool result = UnsafeNativeMethods.CriticalPrintWindow(_hwnd, hdcBitmap, 0);
if(result == false)
{
// Fall back to sending a WM_PRINT message to the window.
//
// Note: there are known cases where WM_PRINT is not implemented
// to provide visual parity with WM_PAINT. However, since the
// GetDrawing method is virtual, the derived class can override
// this default implementation and provide a better implementation.
UnsafeNativeMethods.SendMessage(_hwnd.Handle, WindowMessage.WM_PRINT, hdcBitmap.Handle, (IntPtr) (NativeMethods.PRF_CHILDREN | NativeMethods.PRF_CLIENT | NativeMethods.PRF_ERASEBKGND | NativeMethods.PRF_NONCLIENT));
}
else
{
// There is a know issue where calling PrintWindow on a window will
// clear all dirty regions (but since it is redirected, the screen
// won't be updated). As a result we can leave unpainted pixels on
// the screen if PrintWindow is called when the window was dirty.
//
// To fix this, we just force the child window to repaint.
//
UnsafeNativeMethods.CriticalRedrawWindow(_hwnd, IntPtr.Zero, IntPtr.Zero, NativeMethods.RDW_INVALIDATE | NativeMethods.RDW_ALLCHILDREN);
}
// Create a DrawingGroup that only contains an ImageDrawing that wraps the bitmap.
drawingGroup = new DrawingGroup();
System.Windows.Media.Imaging.BitmapSource bitmapSource = Imaging.CriticalCreateBitmapSourceFromHBitmap(hBitmap.Handle, IntPtr.Zero, Int32Rect.Empty, null, WICBitmapAlphaChannelOption.WICBitmapIgnoreAlpha);
Rect rectElement = new Rect(RenderSize);
drawingGroup.Children.Add(new ImageDrawing(bitmapSource, rectElement));
drawingGroup.Freeze();
}
finally
{
// Put the old bitmap back into the DC.
UnsafeNativeMethods.CriticalSelectObject(hdcBitmap, hOldBitmap);
}
}
}
}
finally
{
UnsafeNativeMethods.ReleaseDC(new HandleRef(this, IntPtr.Zero), hdcScreen);
hdcScreen = new HandleRef(null, IntPtr.Zero);
if(hBitmap.Handle != IntPtr.Zero)
{
UnsafeNativeMethods.DeleteObject(hBitmap);
hBitmap = new HandleRef(this, IntPtr.Zero);
}
if(hdcBitmap.Handle != IntPtr.Zero)
{
UnsafeNativeMethods.CriticalDeleteDC(hdcBitmap);
hdcBitmap = new HandleRef(this, IntPtr.Zero);
}
}
}
}
return drawingGroup;
}
private void Initialize( bool fTrusted )
{
_fTrusted = fTrusted;
_hwndSubclassHook = new HwndWrapperHook(SubclassWndProc);
_handlerLayoutUpdated = new EventHandler(OnLayoutUpdated);
_handlerEnabledChanged = new DependencyPropertyChangedEventHandler(OnEnabledChanged);
_handlerVisibleChanged = new DependencyPropertyChangedEventHandler(OnVisibleChanged);
PresentationSource.AddSourceChangedHandler(this, new SourceChangedEventHandler(OnSourceChanged));
_weakEventDispatcherShutdown = new WeakEventDispatcherShutdown(this, this.Dispatcher);
}
///<summary>
/// Use this method as a defense-in-depth measure only.
///</summary>
private void DemandIfUntrusted()
{
if ( ! _fTrusted )
{
}
}
private void OnSourceChanged(object sender, SourceChangedEventArgs e)
{
// Remove ourselves as an IKeyboardInputSinks child of our previous
// containing window.
IKeyboardInputSite keyboardInputSite = ((IKeyboardInputSink)this).KeyboardInputSite;
if (keyboardInputSite != null)
{
// Derived classes that implement IKeyboardInputSink should support setting it to null.
((IKeyboardInputSink)this).KeyboardInputSite = null;
keyboardInputSite.Unregister();
}
// Add ourselves as an IKeyboardInputSinks child of our containing window.
IKeyboardInputSink source = PresentationSource.CriticalFromVisual(this, false /* enable2DTo3DTransition */) as IKeyboardInputSink;
if(source != null)
{
((IKeyboardInputSink)this).KeyboardInputSite = source.RegisterKeyboardInputSink(this);
}
BuildOrReparentWindow();
}
private void OnLayoutUpdated(object sender, EventArgs e)
{
UpdateWindowPos();
}
private void OnEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_isDisposed)
{
return;
}
bool boolNewValue = (bool)e.NewValue;
UnsafeNativeMethods.EnableWindow(_hwnd, boolNewValue);
}
private void OnVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_isDisposed)
{
return;
}
bool vis = (bool)e.NewValue;
// BUG 148548 HwndHost does not always repaint on restore from minimize.
// We used to call ShowWindow here and ShowWindowAsync in other places (UpdateWindowPos).
// The inconsistent sync/async showing window causes the repainting bug.
// There was recollection from Dwayne that ShowWindow sync might cause rereentrancy issues.
// So change here to show async to be consistent with everywhere else (instead of changing everywhere else
// to show window sync).
if(vis)
UnsafeNativeMethods.ShowWindowAsync(_hwnd, NativeMethods.SW_SHOWNA);
else
UnsafeNativeMethods.ShowWindowAsync(_hwnd, NativeMethods.SW_HIDE);
}
// This routine handles the following cases:
// 1) a parent window is present, build the child window
// 2) a parent is present, reparent the child window to it
// 3) a parent window is not present, hide the child window by parenting it to SystemResources.Hwnd window.
private void BuildOrReparentWindow()
{
DemandIfUntrusted();
// Verify the thread has access to the context.
// VerifyAccess();
// Prevent reentry while building a child window,
// also prevent the reconstruction of Disposed objects.
if(_isBuildingWindow || _isDisposed)
{
return;
}
_isBuildingWindow = true;
// Find the source window, this must be the parent window of
// the child window.
IntPtr hwndParent = IntPtr.Zero;
PresentationSource source = PresentationSource.CriticalFromVisual(this, false /* enable2DTo3DTransition */);
if(source != null)
{
HwndSource hwndSource = source as HwndSource ;
if(hwndSource != null)
{
hwndParent = hwndSource.CriticalHandle;
}
}
else
{
// attempt to also walk through 3D - if we get a non-null result then we know we are inside of
// a 3D scene which is not supported
PresentationSource goingThrough3DSource = PresentationSource.CriticalFromVisual(this, true /* enable2DTo3DTransition */);
if (goingThrough3DSource != null)
{
if (TraceHwndHost.IsEnabled)
{
TraceHwndHost.Trace(TraceEventType.Warning, TraceHwndHost.HwndHostIn3D);
}
}
}
try
{
if(hwndParent != IntPtr.Zero)
{
if(_hwnd.Handle == IntPtr.Zero)
{
// We now have a parent window, so we can create the child
// window.
BuildWindow(new HandleRef(null, hwndParent));
this.LayoutUpdated += _handlerLayoutUpdated;
this.IsEnabledChanged += _handlerEnabledChanged;
this.IsVisibleChanged += _handlerVisibleChanged;
}
else if(hwndParent != UnsafeNativeMethods.GetParent(_hwnd))
{
// We have a different parent window. Just reparent the
// child window under the new parent window.
UnsafeNativeMethods.SetParent(_hwnd, new HandleRef(null,hwndParent));
}
}
else if (Handle != IntPtr.Zero)
{
// Reparent the window to notification-only window provided by SystemResources
// This keeps the child window around, but it is not visible. We can reparent the
// window later when a new parent is available
var hwnd = SystemResources.GetDpiAwarenessCompatibleNotificationWindow(_hwnd);
Debug.Assert(hwnd != null);
if (hwnd != null)
{
UnsafeNativeMethods.SetParent(_hwnd, new HandleRef(null, hwnd.Handle));
// ...But we have a potential problem: If the SystemResources listener window gets
// destroyed ahead of the call to HwndHost.OnDispatcherShutdown(), the HwndHost's window
// will be destroyed too, before the "logical" Dispose has had a chance to do proper
// shutdown. This turns out to be very significant for WebBrowser/ActiveXHost, which shuts
// down the hosted control through the COM interfaces, and the control destroys its
// window internally. Evidently, the WebOC fails to do full, proper cleanup if its
// window is destroyed unexpectedly.
// To avoid this situation, we make sure SystemResources responds to the Dispatcher
// shutdown event after this HwndHost.
SystemResources.DelayHwndShutdown();
}
else
{
Trace.WriteLineIf(hwnd == null, $"- Warning - Notification Window is null\n{new System.Diagnostics.StackTrace(true).ToString()}");
}
}