@@ -13,6 +13,7 @@ public class CaptureWebcam : ISampleGrabberCB, IDisposable
1313 #region Fields
1414 readonly Filter _videoDevice ;
1515 readonly IntPtr _previewWindow ;
16+ IntPtr _currentOwner ;
1617 readonly DummyForm _form ;
1718 readonly Action _onClick ;
1819 readonly object _lock = new object ( ) ;
@@ -50,6 +51,7 @@ public CaptureWebcam(Filter VideoDevice, Action OnClick, IntPtr PreviewWindow)
5051 _form . Click += ( s , e ) => OnClick ? . Invoke ( ) ;
5152
5253 _previewWindow = PreviewWindow != IntPtr . Zero ? PreviewWindow : _form . Handle ;
54+ _currentOwner = _previewWindow ;
5355
5456 BuildGraph ( ) ;
5557 }
@@ -502,6 +504,7 @@ void SetupVideoWindow()
502504 _videoWindow = null ;
503505 return ;
504506 }
507+ _currentOwner = _previewWindow ;
505508 }
506509 catch
507510 {
@@ -555,6 +558,48 @@ public void OnPreviewWindowResize(int X, int Y, int Width, int Height)
555558 }
556559 }
557560
561+ public void SetPreviewVisibility ( bool isVisible )
562+ {
563+ lock ( _lock )
564+ {
565+ if ( _videoWindow == null )
566+ return ;
567+
568+ try
569+ {
570+ _videoWindow . put_Visible ( isVisible ? OABool . True : OABool . False ) ;
571+ }
572+ catch { }
573+ }
574+ }
575+
576+ public void UpdatePreviewWindow ( IntPtr ownerHandle , Rectangle location )
577+ {
578+ lock ( _lock )
579+ {
580+ if ( _videoWindow != null )
581+ {
582+ try
583+ {
584+ if ( ownerHandle != IntPtr . Zero && _currentOwner != ownerHandle )
585+ {
586+ // Switch owner without rebuilding the graph
587+ _videoWindow . put_Visible ( OABool . False ) ;
588+ _videoWindow . put_Owner ( ownerHandle ) ;
589+ _currentOwner = ownerHandle ;
590+ _videoWindow . put_Visible ( OABool . True ) ;
591+ }
592+
593+ _videoWindow . SetWindowPosition ( location . X , location . Y , location . Width , location . Height ) ;
594+ }
595+ catch
596+ {
597+ // Ignore errors
598+ }
599+ }
600+ }
601+ }
602+
558603 #endregion
559604
560605 #region Frame Capture
0 commit comments