我正在尝试禁用捕获屏幕,并且它正在与我的主窗口一起使用-
[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);
但是当我打开弹出/子窗口时,它不起作用。它也可以与子窗口一起使用吗?任何帮助将不胜感激,谢谢。
Deepankshee,我不确定您的问题是什么。它对我有用,我完全按照您的指示进行。
这是我的代码...
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);
}
}
}
如果希望在应用程序中的所有窗口上全局进行此操作,则最好预先定义自己的自定义窗口类并将其放在其中。然后从您的类和默认窗口派生所有窗口...类似
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"
to
<local:YourBaseWindow x:Class="CaptureScreenTest.MainWindow"
… etc...
xmlns:local="clr-namespace:CaptureScreenTest"
然后,您不必在每个窗口上都这样做。您甚至可以参数化窗口以标识是否要启用它。我为WPF应用程序做了类似的基准窗口声明,所有表单都从该窗口派生。我在核心窗口类中还有其他捕获/常量功能,所有这些都伴随着旅程而来,我永远不必忘记在其他窗口上实现。
现在,由于您将来的窗口声明将从此继承,因此,如果它们也具有“ Loaded”事件调用,则父类将仍然自行完成它,派生类也将继续执行他们需要的操作。
经过大量分析,在我的子窗口的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。删除了此属性,它开始工作。希望对您有所帮助