1

I saw some "alike" questions. But the answers were always asking the questioner to use winform. I am in the need of 100% console aplplication which can hook into Windows message queue and give the mouse click points. The mouse click can happen any where in the window.

What I did: I made this perfectly using winforms. Actually I copied most of the code from one blog. It is working. But my current project is "Automating the tests". Here We have to launch most of the applications as a console application. Otherwise the operation will become a mess. I tried with IMessageFilter, then I came to know that it requires form.

Can anybody guide me in proper direction?

Note: I am using Windows7, .Net4.5, Visual Studio Express - 2012

EDIT:

I am not at all caring the console. My target is getting the mouse click coordinates(any where in the screen). That means first I will lauch the program from console, then I will make some clicks on the screen. The console should print out the coordinates of those mouse clicks instantly.

prabhakaran
  • 5,126
  • 17
  • 71
  • 107

2 Answers2

3

This is my take on what you need to do, although I'm still a little hazy on whether or not I understand the question.

  1. Create a normal console application.
  2. Install a mouse hook, WH_MOUSE_LL.
  3. Deal with mouse messages from the hook as you please, for example by outputting information on your console.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Write your program in WinForm but make a invisible app.

Then, attach this app to the parent console and write what you want in it :

NativeMethods.AttachConsole(NativeMethods.ATTACH_PARENT_PROCESS);
Console.WriteLine("Coordinate : " + mouse.X);

Use this class to do this :

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();
}
Xaruth
  • 4,034
  • 3
  • 19
  • 26