嵌入一​​个控制台窗口WPF窗口内 [英] Embed a Console Window inside a WPF Window

查看:133
本文介绍了嵌入一​​个控制台窗口WPF窗口内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能嵌入一个控制台窗口WPF窗口里?

Is it possible to embed a console window inside a WPF window?

作为一个小的背景下,一开始我想从头开始实现一个控制台窗口在WPF中,这是成功的,除了一个大问题 - 它的极其缓慢。看到这里的问题是:
<一href="http://stackoverflow.com/questions/3219819/vt100-terminal-emulation-in-windows-wpf-or-silverlight">http://stackoverflow.com/questions/3219819/vt100-terminal-emulation-in-windows-wpf-or-silverlight

As a little background, at first I tried to implement a console window from scratch in WPF, which was successful except for one huge problem -- its extremely slow. See the question here:
http://stackoverflow.com/questions/3219819/vt100-terminal-emulation-in-windows-wpf-or-silverlight

由于这似乎不是一种选择,我不是看在我的WPF应用程序,这是我已经学会了如何做如下描述主机的实际控制台窗口:

Since that does not seem to be an option I am instead looking at hosting an actual console window in my WPF application, which I've learned how to do as described here:

<一个href="http://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application">http://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application

这是伟大的,由理想我想有一个控制台窗口看起来就像是WPF应用程序的其余部分。我知道这是可能的WinForms应用程序,因为我已经看到了它做的,使用的setparent Win32 API的涉及。你可以看到一个例子.NET项目,做它用这个命令栏项目中嵌入了一个控制台窗口,进入外壳:

And that's great, by ideally I'd like to have that console window look like it is a part of the rest of the WPF application. I know it is possible with a WinForms app as I've seen it done, involving using the SetParent Win32 API. You can see an example .NET project that does it with this CommandBar project that embeds a console window into the shell:

<一个href="http://www.$c$cproject.com/KB/cs/commandbar.aspx">http://www.$c$cproject.com/KB/cs/commandbar.aspx

所以,我希望它可以与WPF进行为好,但我不知道你如何做到这一点。帮助是非常AP preciated(还,如果您有任何辉煌的解决方案,我原来在WPF从头开始创建一个终端窗口的问题,因为这将解决我的需求也是如此)。

So I am hopeful it can be done with WPF as well, but I have no idea how you'd do that. Help is much appreciated (also, if you have any brilliant solutions to my original problem of creating a terminal window from scratch in WPF since that would solve my needs too).

更新:

通过帮助里德•科普塞的帮助下,我能得到的控制台窗口嵌入。但是,当然,它需要设置样式和感动,否则它只是看上去像一个WPF窗口内的常规控制台窗口。我需要删除标题栏和大边框。做研究,我想出了如何使用Win32 API来做到这一点是这样的:

With help Reed Copsey's help I was able to get the Console Window embedded. However, of course it needed to be styled and moved or else it just looked like a regular console window inside a WPF window. I need the title bar and large borders removed. Doing research I figured out how to use the Win32 APIs to do that like this:

uint style = GetWindowLong(ConsoleManager.ConsoleWindowHandle, GWL_STYLE);
style &= ~(uint)WindowStyles.WS_CAPTION;
style &= ~(uint)WindowStyles.WS_THICKFRAME;
style &= ~(uint)WindowStyles.WS_DLGFRAME;
style &= ~(uint)WindowStyles.WS_POPUP;
SetWindowLong(ConsoleManager.ConsoleWindowHandle, GWL_STYLE, style);
MoveWindow(ConsoleManager.ConsoleWindowHandle, 0, 0, (int)WindowsFormsHost.ActualWidth, (int)WindowsFormsHost.ActualHeight, true);

但是有一个很大的问题。出于某种原因,控制台窗口有一个渲染神器。这是因为如果它没有在左下角和右上角两侧重绘本身。神器的宽度类似于标题栏的宽度和粗边框,而事实上,如果我离开的粗边框的伪影的大小会下降。但是,简单地重新绘制它不会帮助,因为它会重新出现。我可以,例如,移动窗口关闭屏幕,然后再返回来修复它,但它很快就重新出现在自己的:

However there's one big problem. For some reason, the console window has a rendering artifact. It's as if it is not repainting itself on the bottom left and top right sides. The width of the artifact is similar to the width of the title bar and the thick border, and in fact if I leave the thick border in the size of the artifact goes down. But simply repainting it won't help since it reappears. I can, for example, move the window off the screen and back again to fix it, but it soon reappears on its own:

更新2:的作用是即使我没有父入WindowsFormsHost控件。所有我需要做的重现是启动控制台(用AllocConsole()),然后取下它与SetWindowLong函数标题栏。这是一个Win7的机器。

UPDATE 2: The effect happens even if I don't parent it into the WindowsFormsHost control. All I need to do to reproduce it is launch the console (using AllocConsole()) and then remove it's title bar with SetWindowLong. This is a win7 machine.

更新3:这样的不支持其他窗口似乎搞乱。控制台窗口计算其textarea的假设有一个标题,那么有没有办法解决这个问题。我想我唯一的选择,以获得在WPF控制台类似的行为将是编写自定义的WinForms控件,然后嵌入到WPF。

UPDATE 3: It seems 'messing' with other windows like this isn't supported. The console window calculates its textarea assuming there is a caption, so there's no way around this. I think my only option to get console-like behavior in WPF is going to be to write a custom WinForms control and then embed that into WPF.

推荐答案

您应该能够使用相同的技术作为Windows窗体应用程序,你表现出由重排根成的 HwndHost 。你甚至可以只适应Windows窗体code,并把这个直接进入<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx"相对=nofollow>的WindowsFormsHost 控制。

You should be able to use the same technique as the Windows Forms application you showed by reparenting into an HwndHost. You could even just adapt the Windows Forms code, and put this directly into WindowsFormsHost control.

这篇关于嵌入一​​个控制台窗口WPF窗口内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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