Just wondering if theres a function to get the window title of your browser.

Like if you were to visit www.myspace.com your window title would be: www.myspace.com - Mozilla Firefox

or w/e browser you happen to use.

Is there a way to get the title of a window?

Just wondering if theres a function to get the window title of your browser.

Like if you were to visit www.myspace.com your window title would be: www.myspace.com - Mozilla Firefox

or w/e browser you happen to use.

Is there a way to get the title of a window?

Use GetWindowText() once you have the handle to the correct window.

Call EnumWindows() to get a list of all the top-level windows so that you can get the handle to the browser window. The problem is -- how will you know which one is the browser?

> The problem is -- how will you know which one is the browser?
Simple :) You can use Spy++ to find out the browsers class name and then you can narrow down which windows are browsers. I found out these:

Firefox's Class:	MozillaUIWindowClass
IExplorer Class:	IEFrame

Now using AD's suggestion, using EnumWindows you can narrow it down only to the internet explorers.

Another helpful function you could try, FindWindow.

If you have firefox, test out this code and the title of the browser should change, if not simply change to class name to work with IExplorer.

#include<windows.h>

int main() {
   HWND firefox = FindWindow("MozillaUIWindowClass", NULL);
   SetWindowText(firefox, "Found it!!");
   return 0;
}

but this only works with one browser, so your probably better to stick with AD's technique.

Hope this helps.

Hey, in the past I've messed with those functions a bit, here're the results. Might help you a bit.

Finds a window given a caption:

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[]){
  HWND hWnd;
  char windowName[512];

  printf("Window to look for: ");
  fgets(windowName, 512, stdin);
  if (hWnd = FindWindow(NULL, windowName)){
    printf("hWnd : %Xh with caption:\n\t",hWnd );
    GetWindowText(hWnd, windowName, 512);
  }
  else printf("No window found with that caption!");
  
  return 0;
}

Enumerates all windows and displays some information about them. Then allows to hide a window.

#include <windows.h>
#include <stdio.h>

char windowName[63];

int DisplayWindows(HWND hWnd, LPARAM lParam);

int main(int argc, char *argv[]){
  EnumWindows((WNDENUMPROC) DisplayWindows,(LPARAM) NULL);

  printf("What window do you want to hide? (give the hWnd)\nhWnd: ");
  int hWnd;
  scanf("%d", &hWnd);

  if (!IsWindow((HWND)hWnd)){
    printf("Window doesn't exist!");
    return 0;
  }

  if(!ShowWindow((HWND) hWnd, SW_HIDE)){
    printf("Window was already hidden...");
  }

  return 0;
}

int DisplayWindows(HWND hWnd, LPARAM lParam){
  GetWindowText(hWnd, windowName, 63);
  if (windowName[0] == '\0') return 1;

  printf("hWnd: %d\nClass: ", hWnd);
  GetClassName(hWnd, windowName, 63);
  puts(windowName);
  printf("Caption:");
  GetWindowText(hWnd, windowName, 63);
  puts(windowName);
  printf("\n");
  return 1;
}

Your code works! But it includes those windows which are hidden or those programs without interface. In short, how to get the captions of windows that can be seen?

Firefox has a visible window, I would like to include it but my firewall, by your method, can be seen but has no visible window so I would like to exclude it.

Can anyone shed some light to this?

You can probably get the state of a window by using an API, have you tried looking for it? I don't know any from the top of my head. If there isn't any, you could try and hide the window, and if the function ShowWindow() returns 0, it was already in the state you tried to set it in. I think. Better check on that though.

thanks mitrmkar, that was really a help. I'm also glad that this thread is alive. I got my solution though. I used key strokes and mouse clicks to get the windows that are being used, although your method is useful too. I'll weigh in if which one will be more useful to my operation.

but another problem is here. i have this snippet in my code.

//from the top part of the code
char  lastwindow[MAX_PATH];
char  currentwindow[MAX_PATH];


/* this one is from the bottom, where it runs when a key or mouse button is pressed (a very common activity when using the pc) and eliminates the chance of getting hidden windows. FStrem is a text file to where it records rather than using the printf()  */

HWND mainwindow;
mainwindow = GetForegroundWindow();
GetWindowText(mainwindow,currentwindow,sizeof(currentwindow));
if(lastwindow==currentwindow)
{}
else
if(lastwindow!=currentwindow)
{        
strcpy (lastwindow,currentwindow);
FStream << "\n\n[" << currentwindow << "]\n\n";
}

summary is, when the active window changes, the currentwindow variable should change value, record the current active window title to a file and copy the value to the lastwindow variable. if the active window has not changed, it will do nothing and wait until mouseclicks or keypresses are done in another window for recording. But it seems that everytime i activate this part of the script, even though i did not change window, it still types in the window title, which it should not.

the values that are being recorded are window captions. is something wrong with the stored values? they seem to be the same when i try to printf them but come out different using this script.

Hey, in the past I've messed with those functions a bit, here're the results. Might help you a bit.

Finds a window given a caption:

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[]){
  HWND hWnd;
  char windowName[512];

  printf("Window to look for: ");
  fgets(windowName, 512, stdin);
  if (hWnd = FindWindow(NULL, windowName)){
    printf("hWnd : %Xh with caption:\n\t",hWnd );
    GetWindowText(hWnd, windowName, 512);
  }
  else printf("No window found with that caption!");
  
  return 0;
}

Enumerates all windows and displays some information about them. Then allows to hide a window.

#include <windows.h>
#include <stdio.h>

char windowName[63];

int DisplayWindows(HWND hWnd, LPARAM lParam);

int main(int argc, char *argv[]){
  EnumWindows((WNDENUMPROC) DisplayWindows,(LPARAM) NULL);

  printf("What window do you want to hide? (give the hWnd)\nhWnd: ");
  int hWnd;
  scanf("%d", &hWnd);

  if (!IsWindow((HWND)hWnd)){
    printf("Window doesn't exist!");
    return 0;
  }

  if(!ShowWindow((HWND) hWnd, SW_HIDE)){
    printf("Window was already hidden...");
  }

  return 0;
}

int DisplayWindows(HWND hWnd, LPARAM lParam){
  GetWindowText(hWnd, windowName, 63);
  if (windowName[0] == '\0') return 1;

  printf("hWnd: %d\nClass: ", hWnd);
  GetClassName(hWnd, windowName, 63);
  puts(windowName);
  printf("Caption:");
  GetWindowText(hWnd, windowName, 63);
  puts(windowName);
  printf("\n");
  return 1;
}

what does the %d value mean? what are those numbers?

The "numbers" are the size of the buffer, but you could've easily looked that one up on MSDN. "%d" means decimal. It's the format thingie for signed and unsigned decimal numbers I thought, might wanna check that.

it's not the process or module id right? the %d thing i mean

%d is just a format character for format strings used by print and scan functions. Check it out

In my code it's the window handle. A "window pointer" as it were, for Windows.

can anyone explain why this code does not do what it's supposed to? im new to this C++ thing and im just "shocked" of its broadness and stuff. this code is supposed to find a browser, in this case, firefox, get its text and for clarity and just to be sure if it was spelled right or something, outputted to a text file.

#include <windows.h>
#include <fstream>
#include <iostream>

char windowtitle[MAX_PATH];
char windowclass[MAX_PATH];
std::string Filename = "netlog.txt";
std::fstream FStream;

int DisplayWindows(HWND hWnd, LPARAM lParam);

int main(){

  FStream.open(Filename.c_str(), std::fstream::out | std::fstream::app);
  EnumWindows((WNDENUMPROC) DisplayWindows,(LPARAM) NULL);
  Sleep(1000);
  FStream.close();
  return 0;
}

int DisplayWindows(HWND hWnd, LPARAM lParam){
  GetClassName(hWnd, windowclass, MAX_PATH);
  if(windowclass=="MozillaUIWindowClass"){
  GetWindowText(hWnd, windowtitle, MAX_PATH);                                       
  FStream << windowtitle << "\n";
  }
  return 1;
}

i just copied the code from clockowl yet in the part where i try to find the firefox if(windowclass=="MozillaUIWindowClass") part, it cannot find firefox. i tested it with the "!=" in place of "==" and there was firefox along with the other classes not equal to MozillaUIWindowClass. it seems that there is some error in the comparison which i cannot get.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.