1

im trying to get the name of each window i click on. I have working code but its using system timers instead of timers on the form. i will post code so its eaiser to see what im doing wrong. its also not letting me refer back to my text box, i think i need to bring it into the function.

heres the Dll imports and variables

    private static string LastActiveWindow = "";
    private static string ActiveWindowName = "";
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

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

and then i have my timer running 10 times a second which will run the active window function:

    private void TimerActiveWindow_Tick(object sender, EventArgs e)
    {
        ActiveWindow();
    }

    private static void ActiveWindow(object Obj, EventArgs e)
    {
        IntPtr hwnd = GetForegroundWindow();
        const int Capacity = 512;
        var text = new StringBuilder(Capacity);

        try
        {
            if (GetWindowText(hwnd, text, Capacity) > 0)
            {
                if (ActiveWindowName != text.ToString())
                {
                    if (!LastActiveWindow.Equals(text.ToString()))
                    {
                    //    TxtBody.text += "<br><font color = purple> Current Window - [" + text.ToString() + "]</font><br>";
                        LastActiveWindow = text.ToString();
                        MessageBox.Show(text.ToString());
                    }
                }
            }

        }
        catch { }
    }

The only difference is the timer which on my program which uses the system timer looks like this

        System.Timers.Timer TimerActiveWindow = new System.Timers.Timer();
        TimerActiveWindow.Elapsed += new ElapsedEventHandler(Program.ActiveWindow);
        TimerActiveWindow.AutoReset = true;
        TimerActiveWindow.Interval = 100;
Jason
  • 233
  • 4
  • 17
  • I am not sure what is the problem you face, but you may be very interested in http://stackoverflow.com/questions/1330074/how-does-spy-find-out-what-is-the-window-at-a-certain-point-on-the-screen – Eugene Podskal Mar 12 '16 at 17:48
  • its when i call the activewindow function, i dont have anything in the brackets which gives me an error, im not sure what to put there, also i cant refer to my textbox inside that function – Jason Mar 12 '16 at 17:51
  • You can't access `TxtBody` because this function is static, while `TxtBody` is probably an instance level field. You should make `ActiveWindow` an instance level method too. – Eugene Podskal Mar 12 '16 at 17:57
  • ok i changed that but what about the event handler i used in the system timer, how do i get that using a form timer – Jason Mar 12 '16 at 18:04
  • Windows.Timer is a bit different beast from Timers.Timer, but I am quite sure that can you do basically the same things with it. – Eugene Podskal Mar 12 '16 at 18:07

0 Answers0