Getting Active Window Title

Hi All,
I dont know anything about windows programming yet.I am trying to Get the active windows title someone advised me to check in windows programming somebody please help me with this.

What I Actually Want ?

I want to write a function that gives me active window title (For example if i open Chrome it should give me its title if i open some other application it should give me title of that window)
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>
#include <string>
using std::string;

string GetActiveWindowTitle()
{
 char wnd_title[256];
 HWND hwnd=GetForegroundWindow(); // get handle of currently active window
 GetWindowText(hwnd,wnd_title,sizeof(wnd_title));
 return wnd_title;
}


GetForegroundWindow(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx
GetWindowText()
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633520(v=vs.85).aspx
Last edited on
Thanks Null for your help.. Btw i like your user name :)
I have one more question.

Actually i want to save the window title in a file. The function is returning and i am able to save it to file .

But what if i want to save every window title of window opened on runtime to be saved to file .Any Idea ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// Callback function. It will be called by EnumWindows function
//  as many times as there are windows
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
 if(IsWindowVisible(hwnd)) // check whether window is visible
 {
     char wnd_title[256];
     GetWindowText(hwnd,wnd_title,sizeof(wnd_title));
     cout <<wnd_title<<endl;
 }
 return true; // function must return true if you want to continue enumeration
}

int main()
{
    EnumWindows(EnumWindowsProc,0);

}

This code get titles of all visible (including minimized) windows on the desktop. Although there's a small problem with this code - it will also show things like "start" "program manager" or "View Available Networks (Tooltip)"
Last edited on
Topic archived. No new replies allowed.