C# 使用WPF生成动态窗口预览
我有许多不同的窗户,它们都有不同的设计。在菜单中选择不同的窗口。菜单中每一行都有窗口的屏幕截图。我想找到一种方法,将以下步骤自动化:C# 使用WPF生成动态窗口预览,c#,wpf,C#,Wpf,我有许多不同的窗户,它们都有不同的设计。在菜单中选择不同的窗口。菜单中每一行都有窗口的屏幕截图。我想找到一种方法,将以下步骤自动化: 制作窗口的屏幕截图 在新窗口中插入图片 链接单击处理程序 所以,实际上我的问题是,是否有可能在运行时获得隐藏窗口的图像,这应该给出一个想法。我有一个用于windows的控件模板。此模板有一个VisualTarget元素,用于包装每个实例中的所有其他控件。所以下面的代码对我有用 class ThumbnailView { public Guid Window
所以,实际上我的问题是,是否有可能在运行时获得隐藏窗口的图像,这应该给出一个想法。我有一个用于windows的控件模板。此模板有一个VisualTarget元素,用于包装每个实例中的所有其他控件。所以下面的代码对我有用
class ThumbnailView
{
public Guid WindowGuid { get; set; }
public Window ApplicationWindowInstance { get; set; }
public Border ThumbnailVisual
{
get {
return (this.ApplicationWindowInstance.
Template.FindName("VisualTarget",
this.ApplicationWindowInstance) as Border);
}
}
}
<Border BorderThickness="0,0,0,0" Cursor="Hand">
<Border.Background>
<VisualBrush Visual="{Binding ThumbnailVisual}"/>
</Border.Background>
</Border>
类缩略图视图
{
公共Guid WindowGuid{get;set;}
公共窗口应用程序WindowInstance{get;set;}
公共边框缩略图
{
得到{
返回(this.ApplicationWindowInstance)。
Template.FindName(“VisualTarget”,
此.ApplicationWindowInstance)作为边框);
}
}
}
编辑:这里有一些更一般的东西
ObservableCollection<WindowInstance> _windows = new ObservableCollection<WindowInstance>();
class WindowInstance
{
public Window CurrentWindowInstance { get; set; }
public DependencyObject CurrentVisual {
get {
return VisualTreeHelper.GetChild(CurrentWindowInstance, 0);
}
}
}
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,0" Width="50" Height="50">
<Border.Background>
<VisualBrush Visual="{Binding CurrentVisual}"/>
</Border.Background>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
observetecollection\u windows=newobservetecollection();
类WindowInstance
{
公共窗口CurrentWindowInstance{get;set;}
公共依赖对象CurrentVisual{
得到{
返回VisualTreeHelper.GetChild(CurrentWindowInstance,0);
}
}
}
编辑:上面的例子是使用实时视觉画笔,这可能导致性能崩溃。下面是冻结窗口缩略图的答案
ObservableCollection<BitmapFrame> _windowCaptures = new ObservableCollection<BitmapFrame>();
TestWindow testWindow = new TestWindow();
RenderTargetBitmap bitmap = new RenderTargetBitmap((int)testWindow.Width, (int)testWindow.Height, 96, 96,
PixelFormats.Pbgra32);
bitmap.Render((Visual)VisualTreeHelper.GetChild(testWindow, 0));
_windowCaptures.Add(BitmapFrame.Create(bitmap));
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Height="100" Width="100" Source="{Binding}"></Image>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
ObservableCollection\u windowCaptures=新的ObservableCollection();
TestWindow TestWindow=新的TestWindow();
RenderTargetBitmap位图=新的RenderTargetBitmap((int)testWindow.Width,(int)testWindow.Height,96,96,
像素格式(Pbgra32);
Render((可视)VisualTreeHelper.GetChild(testWindow,0));
_windowCaptures.Add(BitmapFrame.Create(位图));