创建非活动 C# WPF 窗口的缩略图 [英] Create a thumbnail of an inactive C# WPF Window

查看:17
本文介绍了创建非活动 C# WPF 窗口的缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里浏览了许多主题,并在谷歌上搜索了相关信息,但没有找到与我的问题相关的任何内容.

我想要做的是当用户启动应用程序时,主窗口(不是 MDI)打开,有四个图像框,每个图像框都显示当他们点击它时会打开的表单图像.打开所选表单并进行更改后,如果他们单击以最小化/关闭该表单,它将(似乎)最小化到图像框中,以缩略图视图显示表单外观的实时图像.

我的问题是,如何将表单制作成图像,以便我可以将图像用作图像框中的缩略图?

另外...有人可以向我指出一些资源的方向,以帮助我弄清楚如何将最小化"动画化到图像框中?

我没有要求任何人为我做我的工作,因为我想自己学习,但我有点卡住了.

最后,我不确定这涉及到什么,所以我不知道为这篇文章添加什么标签.我会在想出来的时候添加标签,以便其他人可以找到这些信息.

抱歉,它在 WPF 中.不确定会不会有什么不同.我在 WPF 方面仍然不是特别有经验.

解决方案

您可以使用 VisualBrush,这里是一个按钮的快速示例,其背景设置为缩小版本的堆栈面板.

 <StackPanel x:Name="myRect" ><TextBox Text="MyTexasdfasdfasdfasdfasdft" Height="50"/><CheckBox IsChecked="True"/><矩形填充="红色" 宽度="100" 高度="100"/></StackPanel><按钮><按钮.背景><VisualBrush TileMode="None" Viewport="0,0,1,1" Visual="{Binding ElementName=myRect}" ><VisualBrush.Transform><ScaleTransform ScaleX="0.3" ScaleY="0.3"/></VisualBrush.Transform></VisualBrush></Button.Background></按钮></DockPanel>

虽然此解决方案可以复制屏幕上的内容,但是当屏幕上的内容被隐藏或删除时,VisualBrush 也会如此.为了保持图像,需要将控件呈现为位图.这可以通过 RenderTargetBitMap 来完成

//CenterControl 是要渲染的目标,ShowControl 是将 CenterControl 渲染到的控件.var rtb = new RenderTargetBitmap((int)CenterControl.ActualWidth, (int)CenterControl.ActualHeight, 96, 96,PixelFormats.Pbgra32);rtb.Render(CenterControl);var bgBrush = new ImageBrush(rtb) {Transform = new ScaleTransform(0.1, 0.1)};ShowControl.Background = bgBrush;

I've looked through many topics here, and googled for the information, but I haven't found anything relating to my question.

What I want to do is have it so when a user starts the application, the main window (not an MDI) opens with four imageboxes, each showing an image of the form that would open when they click on it. Once the selected form is open, and changes are made, if they click to minimize/close the form, it will (seemingly) minimize into the imagebox showing a real-time image of what the form looks like in a thumbnail view.

My question is, how do I make a form into an image so I can use the image as a thumbnail in an imagebox?

Also... Can someone point me in the direction of some resources that will help me figure out how to animate the "minimizing" into the imagebox?

I'm not asking anyone to do my work for me, because I'd like to learn it myself, but I'm kinda stuck.

Lastly, I'm not sure what's involved in this, so I don't know what tags to put for this post. I'll add tags as I figure it out so others can find this information.

EDIT: Sorry, it is in WPF. Wasn't sure it would be any different. I'm still not particularly experienced in WPF.

解决方案

You can use the VisualBrush, here is a quick example of a button with a background set to a downscaled version of a stackpanel.

 <DockPanel>
        <StackPanel x:Name="myRect" >
            <TextBox Text="MyTexasdfasdfasdfasdfasdft" Height="50" />
            <CheckBox IsChecked="True"  />
            <Rectangle Fill="Red" Width="100" Height="100" />
        </StackPanel>


        <Button>
            <Button.Background>
                <VisualBrush TileMode="None"  Viewport="0,0,1,1" Visual="{Binding ElementName=myRect}" >
                    <VisualBrush.Transform>
                        <ScaleTransform ScaleX="0.3" ScaleY="0.3" />
                    </VisualBrush.Transform>
                </VisualBrush>
            </Button.Background>
        </Button>
    </DockPanel>

Edit: though this solution works to copy stuff that is on the screen, when the stuff on screen is hidden or removed, so will the VisualBrush. In order to persist the image, it is necessary to render the control to a bitmap. This can be done with the RenderTargetBitMap

// CenterControl is the target to render, ShowControl is the control to render the CenterControl onto.
var rtb = new RenderTargetBitmap((int)CenterControl.ActualWidth, (int)CenterControl.ActualHeight, 96, 96,
                                             PixelFormats.Pbgra32);
            rtb.Render(CenterControl);
            var bgBrush = new ImageBrush(rtb) {Transform = new ScaleTransform(0.1, 0.1)};
            ShowControl.Background = bgBrush;

这篇关于创建非活动 C# WPF 窗口的缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆