在 WinForm 中编写程序,但制作一个不可见的应用程序。
然后,将此应用程序附加到父控制台并在其中写入您想要的内容:
NativeMethods.AttachConsole(NativeMethods.ATTACH_PARENT_PROCESS);
Console.WriteLine("Coordinate : " + mouse.X);
使用这个类来做到这一点:
internal static class NativeMethods
{
internal const int ATTACH_PARENT_PROCESS = -1;
/// <summary>
/// Allocates a new console for the calling process.
/// </summary>
/// <returns>nonzero if the function succeeds; otherwise, zero.</returns>
/// <remarks>
/// A process can be associated with only one console,
/// so the function fails if the calling process already has a console.
/// http://msdn.microsoft.com/en-us/library/ms681944(VS.85).aspx
/// </remarks>
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int AllocConsole();
[DllImport("kernel32.dll")]
internal static extern bool AttachConsole(int dwProcessId);
/// <summary>
/// Detaches the calling process from its console.
/// </summary>
/// <returns>nonzero if the function succeeds; otherwise, zero.</returns>
/// <remarks>
/// If the calling process is not already attached to a console,
/// the error code returned is ERROR_INVALID_PARAMETER (87).
/// http://msdn.microsoft.com/en-us/library/ms683150(VS.85).aspx
/// </remarks>
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern int FreeConsole();
}