我正在尝试将文本发送到控制台,当它需要Console.Readline()输入时,来自不同的线程(带有文本框的Windows窗体)。问题是我似乎无法弄清楚如何发送文本并让它完全像我写的那样出现。
示例:“test TEST 123”将在控制台中显示为:test test&é“ 图片:http://puu.sh/gIjW8/e9f4d94bd5.png
using System;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace ConsoleApplication1
{
public partial class Form1 : Form
{
[DllImport("User32.Dll", EntryPoint = "PostMessageA")]
private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, int lParam);
[DllImport("user32.dll")]
static extern short VkKeyScan(char ch);
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetConsoleWindow();
public static IntPtr hWnd = GetConsoleWindow();
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
TextBox tb = (sender as TextBox);
/*for (int i = 0; i < tb.Text.Length; i++)
{
PostMessage(hWnd, 0x100, (IntPtr)VkKeyScan(tb.Text[i]), 0); -> This crap doesn't support UPPERCASE and numbers <.<
}*/
PostMessage(hWnd, 0x100, (IntPtr)Keys.Enter, 0);
tb.Clear();
}
SendMessage(hWnd, 0x000C, 0, "HerpityDerp"); // -> This crap only changes the console title -_-
}
}
}
我尝试使用SendMessage,但它只更改了控制台的标题:/
如果你们能提出解决方案,我将非常感激
答案 0 :(得分:0)
控制台应用程序通常没有Windows消息泵,您可能想看看如何挂钩控制台的标准输出...