-4

I am trying to make an application get the mouse click location and titles(names ) of the windows on which the user clicks. For Ex. If i click on outlook which is open in task bar, then i need to store application name as "Microsoft Outlook" in database.

Thanks in advance....

  • 2
    There are commercial tools available to spy on your users. If you want to create one for your own, begin by reading the WinApi manual. You can start at [`WindowFromPoint()`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633558(v=vs.85).aspx). This question is too broad. – CodeCaster Sep 23 '14 at 11:39

2 Answers2

3

Check this code: How to get active window handle and title

Namespaces:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

Methods:

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

The Call:

// get handle
IntPtr handle = GetForegroundWindow();

// get title
const int count = 512;
var text = new StringBuilder(count);

if (GetWindowText(handle, text, count) > 0)
{
    MessageBox.Show(text.ToString());
}

I used this code a few times. Really easy to use. You could set up an timer which fires up every 10ms. Save up 2 variables. One with the active window and one with the last window that was focused. In pseudo-code said: If newWindow != oldWindow -> listView.Add(Window).

Could look like this:

public partial class Form1 : Form
    {
        // DECLARE GLOBALS //
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        public static string oldWindow = "";
        public static string currentWindow = "";

        // TIMER EVENT //
        private void timerCheckFocus_Tick(object sender, EventArgs e)
        {
            // get handle
            IntPtr handle = GetForegroundWindow();

            // get title
            const int count = 512;
            var currentWindow = new StringBuilder(count);

            if (currentWindow.ToString() != oldWindow)
            {
                // add your window to a listView //
                oldWindow = currentWindow.ToString();
            }
        }
C4d
  • 3,183
  • 4
  • 29
  • 50
  • my code same as above, I get application text but not getting its name. EX. for Visual studio i get "ApplicationMonitoring (Running) - Microsoft Visual Studio" but i need only "Microsoft Visual Studio" – Datta Kawade Sep 23 '14 at 12:59
  • You asked for the "window name". "ApplicationMonitoring (Running) - Microsoft Visual Studio" IS the window name. Looks like you are looking for the application name ;). – C4d Oct 24 '14 at 10:06
1

you could import the user32.dll

[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern IntPtr GetForegroundWindow();

and then e.g. use a timer to look for the active application.
process.MainWindowTitle gives you the name of the current window/application.

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    IntPtr activeWindow = GetForegroundWindow();
    List<String> strListProcesses = new List<string>();
    foreach (Process process in Process.GetProcesses())
    {
        try
        {
            if (activeWindow == process.MainWindowHandle)
            {
                newApplication = process.MainWindowTitle;
            }
        }
        catch (System.ComponentModel.Win32Exception ex)
        {

        }
    }
}
gottsche
  • 83
  • 4
  • my code same as above, I get application text but not getting its name. EX. for Visual studio i get "ApplicationMonitoring (Running) - Microsoft Visual Studio" but i need only "Microsoft Visual Studio" – Datta Kawade Sep 23 '14 at 13:00
  • So you are NOT searching for the window name. What you are looking for is the application-name. Edit your question! – C4d Sep 23 '14 at 13:41
  • Im currently at work. Ill try out editing my code when Im at home later. Peace. – C4d Sep 23 '14 at 14:05