【问题标题】:Disable capturing screen in WPF c# for child windows在 WPF c# 中为子窗口禁用捕获屏幕
【发布时间】:2020-09-14 11:35:51
【问题描述】:

我正在尝试禁用捕获屏幕,它正在与我的主窗口一起使用 -

[DllImport("user32.dll")]
        public static extern uint SetWindowDisplayAffinity(IntPtr hwnd, uint dwAffinity);

var mainWindowHandle = new WindowInteropHelper(this).Handle;
                const uint WDA_NONE = 0;
                const uint WDA_MONITOR = 1;
                SetWindowDisplayAffinity(mainWindowHandle, WDA_MONITOR);

但是当我打开一个弹出/子窗口时它不起作用。有什么方法可以与子窗口一起使用吗? 任何帮助将不胜感激提前谢谢。

【问题讨论】:

  • 您是否从每个窗体(包括子窗口)运行此代码?
  • @emoreau99 是的,我在子窗口以及 onloaded 事件中使用此代码。

标签: c# .net wpf desktop-application


【解决方案1】:

Deepankshee,我不确定你的问题是什么。它对我有用,而且我完全按照您的指示进行操作。

之前(两个窗口中都没有 OnLoaded 事件):

之后(两个窗口中都有 OnLoaded 事件):

这是我的代码...

MainWindow.xaml:

<Window x:Class="CaptureScreenTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CaptureScreenTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <TextBlock FontSize="64" Text="What up? I'm a Main Window." HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Margin="5"/>
            <Button Content="Open Child" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="15,5" Margin="15"/>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace CaptureScreenTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        public static extern uint SetWindowDisplayAffinity(IntPtr hwnd, uint dwAffinity);

        public MainWindow()
        {
            InitializeComponent();
            //this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var mainWindowHandle = new WindowInteropHelper(this).Handle;
            const uint WDA_NONE = 0;
            const uint WDA_MONITOR = 1;
            SetWindowDisplayAffinity(mainWindowHandle, WDA_MONITOR);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var child = new ChildWindow();
            child.Show();
        }
    }
}

ChildWindow.xaml:

<Window x:Class="CaptureScreenTest.ChildWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CaptureScreenTest"
        mc:Ignorable="d"
        Title="ChildWindow" Height="450" Width="800">
    <Grid>
        <TextBlock FontSize="64" Text="I'm a Child Window." HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"/>
    </Grid>
</Window>

ChildWindow.xaml.cs:

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace CaptureScreenTest
{
    /// <summary>
    /// Interaction logic for ChildWindow.xaml
    /// </summary>
    public partial class ChildWindow : Window
    {
        [DllImport("user32.dll")]
        public static extern uint SetWindowDisplayAffinity(IntPtr hwnd, uint dwAffinity);
        public ChildWindow()
        {
            InitializeComponent();
            //this.Loaded += ChildWindow_Loaded;
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var mainWindowHandle = new WindowInteropHelper(this).Handle;
            const uint WDA_NONE = 0;
            const uint WDA_MONITOR = 1;
            SetWindowDisplayAffinity(mainWindowHandle, WDA_MONITOR);
        }
    }
}

【讨论】:

    【解决方案2】:

    如果您希望在应用程序的所有窗口上全局发生这种情况,您最好预先定义自己的自定义窗口类并将其放入其中。然后从您的类与默认窗口派生所有窗口......类似

    using System;
    using System.Windows;
    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    
    
    namespace CaptureScreenTest
    {
       public class YourBaseWindow : Window
       {
          public YourBaseWindow()
          {  
             Loaded += MyWindowLoaded;
          }
    
          [DllImport("user32.dll")]
          public static extern uint SetWindowDisplayAffinity(IntPtr hwnd, uint dwAffinity);
    
          private void MyWindowLoaded(object sender, RoutedEventArgs e)
          {
             var _curWindHandle = new WindowInteropHelper(this).Handle;
             // const uint WDA_NONE = 0;
             const uint WDA_MONITOR = 1;
             SetWindowDisplayAffinity(_curWindHandle, WDA_MONITOR);
          }
       }
    }
    

    然后,在您的所有窗口中,由于“本地”引用您的 CaptureScreenTest 命名空间,因此只需从 ex 更改您的所有窗口定义:

    <Window x:Class="CaptureScreenTest.MainWindow"
       … etc...
       xmlns:local="clr-namespace:CaptureScreenTest"
    

    <local:YourBaseWindow x:Class="CaptureScreenTest.MainWindow"
       … etc...
       xmlns:local="clr-namespace:CaptureScreenTest"
    

    那么您就不必在每个窗口上都这样做了。您甚至可以参数化您的窗口以确定是否要启用它。我为我的 WPF 应用程序做了类似的基线窗口声明,所有表单都从它派生。我的核心窗口类中还有其他捕获/恒定的功能,这一切都是为了顺风顺水,我永远不必忘记在其他窗口上实现。

    现在,由于您未来的窗口声明继承自此,如果它们也有“已加载”事件调用,则父类仍将自己做,派生类也将继续做他们需要做的事情。

    【讨论】:

      【解决方案3】:

      经过大量分析,在我的xaml文件的子窗口中找到了解决方案-

      <Window x:Class="Test.Popup"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:local="clr-namespace:AllenClassRoom"
              xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
              mc:Ignorable="d"
              Title="Popup" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" AllowsTransparency="True" WindowStyle="None" Loaded="Window_Loaded">
          <Grid>
      

      AllowsTransparency="True" 此属性设置为 true。由于此属性,此函数 (SetWindowDisplayAffinity(mainWindowHandle, WDA_MONITOR);) 总是返回 false。 删除此属性并开始工作。 希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 2021-02-01
        • 1970-01-01
        相关资源
        最近更新 更多