6

I want to write an X-Chat plugin where users will be able to perform a CTCP request to my client, whereby the plugin/X-Chat will respond with my current active window title.

This would be really cool for fellow IRC users to see what I'm up to to allow them to determine what I'm doing if I'm full screen (playing a game, watching a video etc).

Plugins for X-Chat are written in C, so I need a way of determining the current active Window title using Windows API calls from C. Can anyone advise on how this might be done?

Thanks.

deed02392
  • 4,799
  • 2
  • 31
  • 48

2 Answers2

18

I think you can use GetForegroundWindow() to get a handle to the window the user is using and then use GetWindowText() to get the title:

HWND foreground = GetForegroundWindow();
if (foreground)
{
    char window_title[256];
    GetWindowText(foreground, window_title, 256);
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 5
    Use `GetForegroundWindow` if you're looking for the current active window among *all* processes, or use `GetActiveWindow` if you're looking for the active window in just *your* process. See [Eventually, nothing is special anymore](http://blogs.msdn.com/b/oldnewthing/archive/2008/10/06/8969399.aspx). – Adam Rosenfield May 24 '12 at 15:24
1

Here are the APIs to use:

GetActiveWindow()

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646292(v=vs.85).aspx

GetWindowText()

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57