与我的earlier question有关:
我想出了以下方法来创建此WinForms控件:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ConsoleWindow : Control
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AllocConsole();
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool FreeConsole();
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private static ConsoleWindow _theWindow;
public ConsoleWindow()
{
if (!DesignMode)
{
if (_theWindow != null)
{
throw new Exception("An application can have only one ConsoleWindow");
}
_theWindow = this;
AllocConsole();
var newOut = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true };
Console.SetOut(newOut);
Console.SetError(newOut);
var consoleHwnd = GetConsoleWindow();
SizeChanged += (sender, args) =>
{
SetWindowPos(consoleHwnd, IntPtr.Zero, 0, 0, Width, Height, 0);
};
SetWindowLong(consoleHwnd, -16 /*GWL_STYLE*/, 0x50000000 /* WS_CHILD|WS_VISIBLE*/);
SetParent(consoleHwnd, Handle);
SetWindowPos(consoleHwnd, IntPtr.Zero, 0, 0, 0, 0, 0);
}
}
protected override void Dispose(bool disposing)
{
if (disposing && _theWindow != null)
{
FreeConsole();
_theWindow = null;
}
base.Dispose(disposing);
}
}
...然后可以通过XAML在WPF应用程序中使用它,例如:
<WindowsFormsHost>
<WindowsFormsHost.Child>
<controls:ConsoleWindow></controls:ConsoleWindow>
</WindowsFormsHost.Child>
</WindowsFormsHost>
主要有效,但鼠标交互似乎受到了损害。当您创建一个控制台窗口(通常作为顶级窗口运行)时,您可以使用鼠标单击/拖动任意选择,但是像我一样将其作为子控件作为父控件后,该选项将不再起作用。我可以右键单击以调用控制台窗口的上下文菜单来选择/复制所有文本,但是我无法进行单击/拖动选择。
是否可以解决此问题(可能是样式丢失/样式错误或消息路由?),以便我可以按预期方式与控制台窗口进行交互,或者以这种方式为控制台窗口做父母有根本性的问题吗?
>答案 0 :(得分:1)
Dave Kerr编写了一个控件,可以解决您的问题。
使用此链接:https://www.codeproject.com/articles/335909/embedding-a-console-in-a-c-application
应该添加此内容,因为它使用了包含在链接中的名为ProcessInterface的类。
最后,这部分代码就是解决方案:
/// <summary>
/// Handles the KeyDown event of the richTextBoxConsole control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.KeyEventArgs"/> instance containing the event data.</param>
void richTextBoxConsole_KeyDown(object sender, KeyEventArgs e)
{
bool inReadOnlyZone = richTextBoxConsole.Selection.Start.CompareTo(inputStart) < 0;
// If we're at the input point and it's backspace, bail.
if (inReadOnlyZone && e.Key == Key.Back)
e.Handled = true;;
// Are we in the read-only zone?
if (inReadOnlyZone)
{
// Allow arrows and Ctrl-C.
if (!(e.Key == Key.Left ||
e.Key == Key.Right ||
e.Key == Key.Up ||
e.Key == Key.Down ||
(e.Key == Key.C && Keyboard.Modifiers.HasFlag(ModifierKeys.Control))))
{
e.Handled = true;
}
}
// Is it the return key?
if (e.Key == Key.Return)
{
// Get the input.
//todostring input = richTextBoxConsole.Text.Substring(inputStart, (richTextBoxConsole.SelectionStart) - inputStart);
// Write the input (without echoing).
//todoWriteInput(input, Colors.White, false);
}
}