I want to use TryCreateFromDisplayId(DisplayId) to get the GraphicsCaptureItem,I can't find any way to get the DisplayId or WindowId!Please show me the way....
Simple UWP code (I tried to use handle to get GraphicsCaptureItem):
CS code:
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Graphics.Capture;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class MainPage : Page
{
private bool Set=false;
public MainPage()
{
this.InitializeComponent();
}
private void TextBlock_SelectionChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (!Set) HandleInput.Text = "";
Set = true;
Enter.IsEnabled = true;
}
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Set = false;
var hd = int.Parse(HandleInput.Text, NumberStyles.HexNumber);
IntPtr hwd = new IntPtr(hd);
var item = CaptureItemHelper.CreateItemForWindow(hwd);
ItemList.ItemsSource = item.ToString();
Enter.IsEnabled=false;
}
}
public static class CaptureItemHelper
{
static Guid GraphicsCaptureItemGuid = new Guid("79C3F95B-31F7-4EC2-A464-632EF5D30760");
[ComImport]
[Guid("3628E81B-3CAC-4C60-B7F4-23CE0E0C3356")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComVisible(true)]
interface IGraphicsCaptureItemInterop
{
IntPtr CreateForWindow(
[In] IntPtr window,
[In] ref Guid iid);
IntPtr CreateForMonitor(
[In] IntPtr monitor,
[In] ref Guid iid);
}
public static GraphicsCaptureItem CreateItemForWindow(IntPtr hwnd)
{
var factory = WindowsRuntimeMarshal.GetActivationFactory(typeof(GraphicsCaptureItem));
var interop = (IGraphicsCaptureItemInterop)factory;
//var temp = typeof(GraphicsCaptureItem);
var itemPointer = interop.CreateForWindow(hwnd, GraphicsCaptureItemGuid);
var item = Marshal.GetObjectForIUnknown(itemPointer) as GraphicsCaptureItem;
Marshal.Release(itemPointer);
return item;
}
}
}
XAML code:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="40">
<TextBox x:Name="HandleInput" FontSize="20" Text="Please enter a hexadecimal handle!"
SelectionChanged="TextBlock_SelectionChanged" Margin="0,0,40,0"/>
<Button x:Name="Enter" FontSize="20" Content="Enter" IsEnabled="False" Click="Button_Click" />
<ListView x:Name="ItemList" Margin="20" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</Page>