C# C-如何在不使用winform的情况下在命令行exe中单击鼠标

C# C-如何在不使用winform的情况下在命令行exe中单击鼠标,c#,.net,windows,C#,.net,Windows,我看到一些类似的问题。但答案总是要求提问者使用winform。我需要一个100%的控制台应用程序,它可以连接到Windows消息队列并提供鼠标点击点。鼠标单击可以在窗口中的任何位置发生 我所做的:我完美地使用winforms制作了这个。实际上,我从一个博客上复制了大部分代码。它正在工作。但我目前的项目是自动化测试。在这里,我们必须以控制台应用程序的形式启动大多数应用程序。否则操作会变得一团糟。我尝试使用IMessageFilter,然后我知道它需要表单 有人能指引我正确的方向吗 注意:我使用的是

我看到一些类似的问题。但答案总是要求提问者使用winform。我需要一个100%的控制台应用程序,它可以连接到Windows消息队列并提供鼠标点击点。鼠标单击可以在窗口中的任何位置发生

我所做的:我完美地使用winforms制作了这个。实际上,我从一个博客上复制了大部分代码。它正在工作。但我目前的项目是自动化测试。在这里,我们必须以控制台应用程序的形式启动大多数应用程序。否则操作会变得一团糟。我尝试使用IMessageFilter,然后我知道它需要表单

有人能指引我正确的方向吗

注意:我使用的是Windows7、.Net4.5、Visual Studio Express-2012

编辑:


我一点也不在乎控制台。我的目标是在屏幕的任何位置获得鼠标点击的协调。这意味着首先我将从控制台启动程序,然后我将在屏幕上进行一些单击。控制台应该立即打印出这些鼠标点击的坐标。

用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();
}

在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();
}

这是我对你需要做的事情的看法,尽管我还不清楚我是否理解这个问题

创建一个普通的控制台应用程序。 安装一个鼠标挂钩,而不是鼠标。 您可以随意处理来自钩子的鼠标消息,例如在控制台上输出信息。
这是我对你需要做的事情的看法,尽管我还不清楚我是否理解这个问题

创建一个普通的控制台应用程序。 安装一个鼠标挂钩,而不是鼠标。 您可以随意处理来自钩子的鼠标消息,例如在控制台上输出信息。
我不知道这个问题问的是什么他想在控制台应用程序I中获得鼠标点击位置think@Paddy,David,请看编辑我不知道这个问题是问什么他想在控制台应用程序中获得鼠标点击位置think@Paddy,David请看编辑这将导致两个活动进程连接到同一个控制台这是一个众所周知的灾难配方这将导致两个活动进程连接到同一个控制台这是一个众所周知的灾难配方这一编辑让我相信这确实是他试图做的。他的编辑让我相信这确实是他想要做的。