在c#或vb.net中,在Windows表单下,我想知道如何将外部命令行接口(CLI)窗口嵌入面板或其他类型的主机窗口中,我可以在其中渲染外部CLI的内容我的表格内的窗户。
请注意,我不会假装重定向并打印 stdout 自己。我只是想将窗户嵌入我的表格中,然后将其嵌入。
我已经尝试了 setParent 函数效果,但它似乎对非刻板用户-interface Windows不起作用,因为因为设置父窗口时,CLI窗口会从屏幕上消失,并且不会在父(面板)窗口中渲染。
看来这可以在WPF中进行,如所建议的在这里,但是我不知道如何在Windows表单下进行此操作。
我正在寻找可以以这样的方式使用的解决方法:
using (var p = new Process()) {
p.StartInfo.FileName = @".\cli-process.exe";
p.StartInfo.Arguments = @"...";
p.StartInfo.CreateNoWindow = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
Thread.Sleep(2000);
EmbedToWindow(p.MainWindowHandle, this.Panel1.Handle);
p.WaitForExit(TimeOut.Infinite);
}
}
In C# or VB.NET, under Windows Forms, I would like to know how can I embed a external command-line interface (CLI) window, into a panel or other kind of host window where I can render the contents of the external CLI window inside my form.
Please note that I don't pretend to redirect and print the StdOut stream by myself. I just would like to embed the window into my form and let it be.
I've tried SetParent function as suggested, but it does not seem to work for non-graphical user-interface windows, because when setting the parent window, the CLI window disappear from screen and it is not rendered in the parent (a panel) window.
It seems this can be done in WPF as suggested here, but I'm not aware how to do this under Windows Forms.
I'm looking for some workaround that I could use in a way like this:
using (var p = new Process()) {
p.StartInfo.FileName = @".\cli-process.exe";
p.StartInfo.Arguments = @"...";
p.StartInfo.CreateNoWindow = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
Thread.Sleep(2000);
EmbedToWindow(p.MainWindowHandle, this.Panel1.Handle);
p.WaitForExit(TimeOut.Infinite);
}
}
发布评论
评论(1)
我刚刚在vb.net中写了一个简单的助手课,它将使我轻松设置和释放父窗口。
至少在我所需的情况下,它至少可以按预期进行。
感谢@jimi,@rbmm和@Remy Lebeau的帮助以及我需要知道的技巧,以找出如何做到这一点。
但是,由于缺少类型,我在这里分享的内容不起作用,但是您可以理解这个想法。抱歉,但它需要超过此帖子的最大角色限制,也许复制&如果我是唯一有兴趣完成此任务的
人,请粘贴并适应在这里向他们展示的东西...但是,如果您想弄清楚如何使此类功能正常缺少
nativemethods
类定义,然后替换呼叫 window> window> window> win32/api/hinuser/nf-winuser-getWindowInfo“ rel =“ nofollow noreferrer”> getWindowInfo (以及 window> windownfo struct),否则 getClientRect + getwindowlongptr /code>或用于从 safehandlehemandleminusoneisinisinvalid 。用法示例:
窗口parrenting类:
它缺少P/Invokes的Win32错误处理。也许其他事情可以得到改善。
I just wrote a simple helper class in VB.NET that will serve me to set and release a parent window with ease.
It seems to work as expected at least as far as I have tested it in my required scenarios.
Thanks to @Jimi, @RbMm and @Remy Lebeau for their help and their tips that I need to know in order to figure how to do this.
However, what I share here is not functional due to missing types, but you could get the idea. Sorry but it would require to exceed the maximum character limit of this post, and maybe it's too much effort of copy & paste and adapt things to show them here if I'm the only one interested to accomplish this task...
But if you want to figure how to make this class functional then simply add the missing P/Invoke members of which I use in the missing
NativeMethods
class definition, and replace the missingWindowInfo
type for a call to GetWindowInfo (and the WINDOWINFO struct) or else GetClientRect + GetWindowLongPtr functions, and replace missingSafeWindowHandle
type forIntPtr
or for a custom class derived from SafeHandleZeroOrMinusOneIsInvalid.Usage Example:
WindowParenting Class:
It lacks a bit of win32 error-handling for P/invokes. And maybe other things could be improved.